Files
plex-auto-skip/content.js
T

62 lines
1.8 KiB
JavaScript

(function () {
let settings = DEFAULT_SETTINGS;
const recentClicks = new WeakMap();
const CLICK_COOLDOWN_MS = 1500;
function isSkipIntroButton(text) {
return text.indexOf("skip intro") !== -1;
}
function isSkipCreditsButton(text) {
return text.indexOf("skip credit") !== -1;
}
function isVisible(el) {
const style = window.getComputedStyle(el);
return style.display !== "none" && style.visibility !== "hidden" && style.opacity !== "0";
}
function canClick(el) {
const last = recentClicks.get(el);
if (last && Date.now() - last < CLICK_COOLDOWN_MS) return false;
recentClicks.set(el, Date.now());
return true;
}
function scanAndClick() {
if (!settings.skipIntro && !settings.skipCredits) return;
if (!hostMatchesConfig(window.location.hostname, settings.plexUrl)) return;
const nodes = document.querySelectorAll('button, [role="button"]');
nodes.forEach((el) => {
const text = (el.innerText || el.textContent || "").trim().toLowerCase();
if (!text) return;
if (!isVisible(el)) return;
const wantsIntroSkip = settings.skipIntro && isSkipIntroButton(text);
const wantsCreditsSkip = settings.skipCredits && isSkipCreditsButton(text);
if (!wantsIntroSkip && !wantsCreditsSkip) return;
if (canClick(el)) el.click();
});
}
function start() {
const observer = new MutationObserver(scanAndClick);
observer.observe(document.documentElement, { childList: true, subtree: true });
scanAndClick();
}
chrome.storage.sync.get(DEFAULT_SETTINGS, (loaded) => {
settings = loaded;
start();
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "sync") return;
Object.keys(changes).forEach((key) => {
settings[key] = changes[key].newValue;
});
});
})();