From 8383bfa313bfa830297af95fb95e241172c2c8a4 Mon Sep 17 00:00:00 2001 From: Lawrence Hook Date: Sun, 6 Nov 2022 17:56:29 -0500 Subject: [PATCH] Add an option to disable autoplay for playlists --- src/js/background/main.js | 6 ++++++ src/js/content-script.js | 38 +++++++++++++++++++++++++++++++------- src/manifest.json | 2 +- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/js/background/main.js b/src/js/background/main.js index ba731b3..2b030b3 100644 --- a/src/js/background/main.js +++ b/src/js/background/main.js @@ -134,6 +134,11 @@ const SECTIONS = [ id: "disable_autoplay", defaultValue: true }, + { + name: "Disable autoplay for playlists", + id: "disable_playlist_autoplay", + defaultValue: true + }, { name: "Redirect shorts to the default viewer", tags: "Redirects, Basic", @@ -381,6 +386,7 @@ browser.runtime.onMessage.addListener((data, sender) => { const { frameId, tab } = sender; browser.storage.local.get(localSettings => { const settings = { ...DEFAULT_SETTINGS, ...localSettings }; + browser.tabs.sendMessage(tab.id, { settings }, {}); // Gray out browserAction diff --git a/src/js/content-script.js b/src/js/content-script.js index 66aa474..1a5f7d1 100644 --- a/src/js/content-script.js +++ b/src/js/content-script.js @@ -56,11 +56,9 @@ document.addEventListener("DOMContentLoaded", event => { counter = 0; theaterClicked = false; hyper = false; - originalPlayback = undefined; - originalMuted = undefined; onResultsPage = resultsPageRegex.test(location.href); - requestRunDynamicSettings() + handleNewPage(); }); // let lastRun = Date.now(); @@ -83,10 +81,8 @@ function runDynamicSettings() { url = location.href; theaterClicked = false; hyper = false; - originalPlayback = undefined; - originalMuted = undefined; onResultsPage = resultsPageRegex.test(location.href); - handleUrlChange(); + handleNewPage(); } // Mark the left nav bar sections @@ -246,7 +242,34 @@ function runDynamicSettings() { } -function handleUrlChange() { +function injectScripts() { + + // Disable playlist autoplay + if (cache['disable_playlist_autoplay']) { + const existingScript = document.querySelector('script[id="disable_playlist_autoplay"]') + if (existingScript) return; + + const script = document.createElement("script"); + script.id = 'disable_playlist_autoplay'; + script.type = "text/javascript"; + script.innerText = ` +(function() { +let pm; +function f() { + if (!pm) pm = document.querySelector('yt-playlist-manager'); + if (pm) pm.canAutoAdvance_ = false; +} +f(); +setInterval(f, 100); +})() +`; + document.body?.appendChild(script); + } + +} + + +function handleNewPage() { if (cache['global_enable'] !== true) return; dynamicIters = 0; @@ -275,6 +298,7 @@ function handleUrlChange() { location.replace(newUrl); } + injectScripts(); requestRunDynamicSettings(); } diff --git a/src/manifest.json b/src/manifest.json index 9ec6716..081d422 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -3,7 +3,7 @@ "description": "Spend less time on YouTube. Customize YouTube's user interface to be less engaging.", "homepage_url": "https://github.com/lawrencehook/remove-youtube-suggestions", "manifest_version": 2, - "version": "4.3.14", + "version": "4.3.15", "icons": { "16": "images/16.png",