diff options
Updated youtube-yewtube-video-redirector to load observer on ytd-app appearane, improved documentation and made invidious instance configurable
2 files changed, 52 insertions, 29 deletions
| -rw-r--r-- | youtube-invidious-video-redirector.js | 52 | ||||
| -rw-r--r-- | youtube-yewtube-smart-redirector.js | 29 |
diff --git a/youtube-invidious-video-redirector.js b/youtube-invidious-video-redirector.js new file mode 100644 index 0000000..03bf689 --- /dev/null +++ b/youtube-invidious-video-redirector.js @@ -0,0 +1,52 @@ +// ==UserScript== +// @name YouTube to Invidious video redirector +// @description When opening YouTube videos, redirect to an Invidious instance, but when going back from instance, don't redirect again +// +// @version 1.1 +// @license MIT +// @author Syndamia +// +// @source https://gitlab.com/Syndamia/userscripts/-/blob/main/youtube-invidious-video-redirector.js +// @supportURL https://gitlab.com/Syndamia/userscripts/-/issues +// @homepage https://gitlab.com/Syndamia/userscripts/ +// @icon https://upload.wikimedia.org/wikipedia/commons/a/a0/Invidious-logo.svg +// +// @namespace Syndamia +// @match *://www.youtube.com/* +// @run-at document-start +// ==/UserScript== + +const invidiousInstance = 'yewtu.be'; + + + +const ytdObserver = new MutationObserver(function(mutations) { + // Detect when ytd-app element is present + if (document.getElementsByTagName('ytd-app').length > 0) { + // Attach the redirection function to it + const observer = new MutationObserver(redirect); + observer.observe(document.getElementsByTagName('ytd-app')[0], { childList: true, attributes: true, subtree: true, }); + ytdObserver.disconnect(); + } +}); + +ytdObserver.observe(document, {attributes: false, childList: true, characterData: false, subtree:true}); + +// Opening YouTube links in YouTube doesn't reload the page, but we know that if the page changes, ytd-app (element that contains the UI) will also change +// so, when ytd-app changes, we check if the link has changed, and redirect +function redirect() { + const latest = localStorage.getItem('LATEST'), loc = window.location.search.substr(3); + + if (loc !== latest) { + localStorage.removeItem(latest); + localStorage.removeItem('LATEST'); + } + + if (window.location.pathname === '/' || localStorage.getItem(loc) === 'true') { + return; + } + + localStorage.setItem(loc, true); + localStorage.setItem('LATEST', loc); + window.location.host = invidiousInstance; +} diff --git a/youtube-yewtube-smart-redirector.js b/youtube-yewtube-smart-redirector.js deleted file mode 100644 index 7a40562..0000000 --- a/youtube-yewtube-smart-redirector.js +++ /dev/null @@ -1,29 +0,0 @@ -// ==UserScript== -// @name YouTube to YewTube smart redirector -// @description When opening YouTube videos, redirect to an Invidious instance, but when going back from instance, don't redirect again -// @version 1.0 -// @namespace Syndamia -// @author Kamen Mladenov -// @match *://www.youtube.com/* -// @run-at document-end -// ==/UserScript== - -function redirect() { - const latest = localStorage.getItem('LATEST'), loc = window.location.search.substr(3); - - if (latest && loc !== latest) { - localStorage.removeItem(latest); - localStorage.removeItem('LATEST'); - } - - if (window.location.pathname === '/' || localStorage.getItem(loc) === 'true') { - return; - } - - localStorage.setItem(loc, true); - localStorage.setItem('LATEST', loc); - window.location.host = "yewtu.be"; -} - -const observer = new MutationObserver(redirect); -observer.observe(document.getElementsByTagName('ytd-app')[0], { childList: true, attributes: true, subtree: true, }); |
