Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically skip intro #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 142 additions & 25 deletions src/content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,70 @@
"use strict";

// todo: fix this
let enhanceotronAudioCtx, compressor, source, compressorActive, libraryType;
let enhanceotronAudioCtx, compressor, libraryType;
let introObserver = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.addedNodes.length <= 0 || !mutation.addedNodes[0] || mutation.addedNodes[0].innerText !== "SKIP INTRO") continue;

// Intro button shows up early sometimes so wait 2 seconds before clicking it
setTimeout(() => { mutation.addedNodes[0].firstChild.click() }, 2000);
return;
}
});

const enhanceotronOptions = {
_skipIntroEnabled: false,
_audioCompressorEnabled: false,
_source: null,

// Skip Intro getter/setter
get SkipIntroEnabled() {
return this._skipIntroEnabled;
},
set SkipIntroEnabled(value) {
this._skipIntroEnabled = value;

if (value) {
introObserver.observe(document.getElementById('plex'), {
childList: true,
attributes: false,
subtree: true
});
} else {
introObserver.disconnect();
}
},

// Audio Compressor getter/setter
get AudioCompressorEnabled() {
return this._audioCompressorEnabled;
},
set AudioCompressorEnabled(value) {
this._audioCompressorEnabled = value;
audioCompressorChange();
},

// Source getter/setter
get Source() {
return this._source;
},
set Source(value) {
this._source = value;
if(value) setTimeout(function () { audioCompressorChange(); }, 500);
}
};

const audioCompressorChange = function () {
if (enhanceotronOptions.Source && enhanceotronOptions.AudioCompressorEnabled) {
enhanceotronOptions.Source.disconnect(enhanceotronAudioCtx.destination);
enhanceotronOptions.Source.connect(compressor);
compressor.connect(enhanceotronAudioCtx.destination);
} else if (enhanceotronOptions.Source && !enhanceotronOptions.AudioCompressorEnabled) {
enhanceotronOptions.Source.disconnect(compressor);
compressor.disconnect(enhanceotronAudioCtx.destination);
enhanceotronOptions.Source.connect(enhanceotronAudioCtx.destination);
}
}

// TRAILERS //

Expand Down Expand Up @@ -228,11 +291,9 @@ function createZoomElem() {
function createCompressor() {
let compressorBtn = document.createElement('button');
compressorBtn.setAttribute("id","enhanceotron-compressor");
//compressorBtn.setAttribute('data-active', 'false');
compressorBtn.setAttribute('title', 'Volume Compressor');

//compressorBtn.style.marginLeft = "10px";
compressorBtn.style.opacity = compressorActive ? "1" : "0.5";
compressorBtn.style.opacity = enhanceotronOptions.AudioCompressorEnabled ? "1" : "0.5";

compressorBtn.style.marginLeft = "5px";
compressorBtn.style.fontSize = "18px";
Expand Down Expand Up @@ -261,22 +322,10 @@ function createCompressor() {
compressorBtn.appendChild(compressorIcon);

compressorBtn.onclick = function () {
//const active = compressorBtn.getAttribute('data-active');
if (source && !compressorActive) {
//compressorBtn.setAttribute('data-active', 'true');
compressorActive = true
compressorBtn.style.opacity = "1";
source.disconnect(enhanceotronAudioCtx.destination);
source.connect(compressor);
compressor.connect(enhanceotronAudioCtx.destination);
} else if (source && compressorActive) {
//compressorBtn.setAttribute('data-active', 'false');
compressorActive = false
compressorBtn.style.opacity = "0.5";
source.disconnect(compressor);
compressor.disconnect(enhanceotronAudioCtx.destination);
source.connect(enhanceotronAudioCtx.destination);
}
enhanceotronOptions.AudioCompressorEnabled = !enhanceotronOptions.AudioCompressorEnabled;
compressorBtn.style.opacity = enhanceotronOptions.AudioCompressorEnabled ? "1" : "0.5";
chrome.storage.sync.set({ audioCompressor: enhanceotronOptions.AudioCompressorEnabled });

}

const rightControls = document.querySelector('[class*="PlayerControls-buttonGroupRight-"]');
Expand All @@ -286,6 +335,54 @@ function createCompressor() {
}
}

// SKIP INTRO //

function createSkipIntro() {

let skipIntroButton = document.createElement('button');

skipIntroButton.setAttribute("id", "enhanceotron-skipIntro");
skipIntroButton.setAttribute("title", "Automatically Skip Intros (Requires Plex Pass)");

skipIntroButton.style.opacity = enhanceotronOptions.SkipIntroEnabled ? "1" : ".5";
skipIntroButton.style.marginLeft = "5px";
skipIntroButton.style.fontSize = "18px";
skipIntroButton.style.height = "30px";
skipIntroButton.style.width = "30px";
skipIntroButton.style.background = "none";
skipIntroButton.style.border = "0";
skipIntroButton.style.cursor = "pointer";
skipIntroButton.style.outline = "none";
skipIntroButton.style.padding = "0";
skipIntroButton.style.textDecoration = "none";
skipIntroButton.style.touchAction = "manipulation";
skipIntroButton.style.transition = "color .2s";
skipIntroButton.style.userSelect = "none";

let skipIntroIcon = document.createElement("img");
skipIntroIcon.src = chrome.runtime.getURL("img/introSkip.svg");

skipIntroIcon.style.height = "1.2em";
skipIntroIcon.style.width = "1.2em";
skipIntroIcon.style.position = "relative";
skipIntroIcon.style.top = "-2px";
skipIntroIcon.style.verticalAlign = "middle";

skipIntroButton.appendChild(skipIntroIcon);

skipIntroButton.onclick = function () {
enhanceotronOptions.SkipIntroEnabled = !enhanceotronOptions.SkipIntroEnabled;
skipIntroButton.style.opacity = enhanceotronOptions.SkipIntroEnabled ? "1" : ".5";
chrome.storage.sync.set({ skipIntro: enhanceotronOptions.SkipIntroEnabled });
}

const rightControls = document.querySelector('[class*="PlayerControls-buttonGroupRight-"]');

if (rightControls) {
rightControls.insertBefore(skipIntroButton, rightControls.lastChild);
}
}

document.arrive('video[class*="HTMLMedia-mediaElement-"]', function() {
//const videoElem = document.querySelector('.HTMLMedia-mediaElement-2XwlNN');
const videoElem = document.querySelector('video[class*="HTMLMedia-mediaElement-"]');
Expand All @@ -300,8 +397,8 @@ document.arrive('video[class*="HTMLMedia-mediaElement-"]', function() {
const AudioContext = window.AudioContext || window.webkitAudioContext;
enhanceotronAudioCtx = new AudioContext();

if (!source) {
source = enhanceotronAudioCtx.createMediaElementSource(videoElem);
if (!enhanceotronOptions.Source) {
enhanceotronOptions.Source = enhanceotronAudioCtx.createMediaElementSource(videoElem);
}

compressor = enhanceotronAudioCtx.createDynamicsCompressor();
Expand All @@ -311,7 +408,7 @@ document.arrive('video[class*="HTMLMedia-mediaElement-"]', function() {
compressor.threshold.value = -50;
compressor.knee.value = 12;

source.connect(enhanceotronAudioCtx.destination);
enhanceotronOptions.Source.connect(enhanceotronAudioCtx.destination);
}
});
});
Expand All @@ -328,7 +425,17 @@ document.arrive("button[data-qa-id='volumeButton']", function() {
}

if (!document.getElementById('enhanceotron-compressor')) {
createCompressor();
chrome.storage.sync.get(['audioCompressor'], function (result) {
enhanceotronOptions.AudioCompressorEnabled = result.audioCompressor;
createCompressor();
});
}

if (!document.getElementById('enhanceotron-skipIntro')) {
chrome.storage.sync.get(['skipIntro'], function (result) {
enhanceotronOptions.SkipIntroEnabled = result.skipIntro;
createSkipIntro();
});
}
});

Expand All @@ -343,6 +450,16 @@ document.arrive("button[data-testid='volumeButton']", function() {
}

if (!document.getElementById('enhanceotron-compressor')) {
createCompressor();
chrome.storage.sync.get(['audioCompressor'], function (result) {
enhanceotronOptions.AudioCompressorEnabled = result.audioCompressor;
createCompressor();
});
}

if (!document.getElementById('enhanceotron-skipIntro')) {
chrome.storage.sync.get(['skipIntro'], function (result) {
enhanceotronOptions.SkipIntroEnabled = result.skipIntro;
createSkipIntro();
});
}
});
113 changes: 113 additions & 0 deletions src/img/introSkip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"*://app.plex.tv/*",
"*://localhost/*",
"*://127.0.0.1/*",
"activeTab"
"activeTab",
"storage"
],
"optional_permissions": [
"<all_urls>"
Expand All @@ -28,5 +29,5 @@
"48": "img/icon48.png",
"128": "img/icon128.png"
},
"web_accessible_resources": ["img/icon219.svg", "img/compress.svg"]
"web_accessible_resources": [ "img/icon219.svg", "img/compress.svg", "img/introSkip.svg" ]
}
Loading