Summary Files Commits Branches @commit @diff @stats
path: root / youtube-invidious-video-redirector.js
diff options
context:
space:
mode:
Commit792715a11899ac516423b8efbfff2355f44d1758 (patch)
Author
 Kamen Mladenov <kamen.d.mladenov@protonmail.com>
2022-04-17 06:51:03 +0000
Committer
 Kamen Mladenov <kamen.d.mladenov@protonmail.com>
2022-04-17 06:51:03 +0000
Filese1a17a6c857bab56cc0ac5c7e48f025772efa95a /youtube-invidious-video-redirector.js
userscripts-792715a11899ac516423b8efbfff2355f44d1758.tar
userscripts-792715a11899ac516423b8efbfff2355f44d1758.tar.gz
userscripts-792715a11899ac516423b8efbfff2355f44d1758.zip
Parent8298f2fde490381d75eded3909da148667f9471c (diff)
Updated youtube-yewtube-video-redirector to load observer on ytd-app appearane, improved documentation and made invidious instance configurable
1 files changed, 52 insertions, 0 deletions
-rw-r--r--youtube-invidious-video-redirector.js52
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;
+}