diff --git a/background.js b/background.js new file mode 100644 index 0000000..fb5b71b --- /dev/null +++ b/background.js @@ -0,0 +1,128 @@ +let ausnahme = false; +let w2gRegex = /w2g\.tv/; + + +// Einstellungen ziehen, ContentScript registrieren +browser.storage.local.get( + { + activated: true, + instance: "redirect.invidio.us", + } +).then((storage) => { + if (storage.activated) { + activateBlocker(); + } else { + deactivateBlocker(); + } + + registerCS(storage.instance); +}); + + +// ContentScript für aktuelle Instanz registrieren +async function registerCS(url) { + return await browser.contentScripts.register({ + js: [{file: "content.js"}], + matches: ["*://" + url + "/*"] + }); +} + +// pageAction anzeigen +browser.runtime.onMessage.addListener((data, sender) => { + if (data.type == "showPageAction") { + browser.pageAction.show(sender.tab.id); + } +}) + +// Storage onChange listener +browser.storage.onChanged.addListener((changes) => { + if ("instance" in changes) { + registerCS(changes.instance.newValue); + } + if ("enabledBlock" in changes) { + enabledBlock = changes.enabledBlock.newValue; + } +}); + +// Wenn Icon geklickt wird Blocker de-/aktivieren +browser.browserAction.onClicked.addListener(() => { + browser.storage.local.get("activated").then((item) => { + if (item.activated) { + deactivateBlocker(); + } else { + activateBlocker(); + } + }); +}); + +function activateBlocker() { + browser.browserAction.setIcon({path:"icons/piped.png"}); + browser.storage.local.set({activated: true}); +} + +function deactivateBlocker() { + browser.browserAction.setIcon({path: "icons/youtube.png"}); + browser.storage.local.set({activated: false}); +} + + +// Redirect +browser.webRequest.onBeforeRequest.addListener(async (details) => { + + let activated; + let instance; + let proxy; + let imageRedirect; + + await browser.storage.local.get({ + activated: true, + instance: "redirect.invidio.us", + imageRedirect: true + }).then((item) => { + activated = item.activated; + instance = item.instance; + imageRedirect = item.imageRedirect; + }); + + if (activated) { + const youtubeRegex = /youtube.com(\/?.*)/; + const youtubeShortRegex = /youtu.be\/.+/; + + if (imageRedirect && details.url.startsWith("https://img.youtube.com/vi")) { + return { redirectUrl: 'https://' + proxy + '/vi/' + details.url.split("/vi/")[1].split("/")[0] + "/hqdefault.jpg?host=i.ytimg.com" } + } + else if (youtubeRegex.test(details.url)) { + return { redirectUrl: 'https://' + instance + youtubeRegex.exec(details.url)[1] }; + + } else if (youtubeShortRegex.test(details.url)) { + const youtubeShortCaptureRegex = /youtu.be\/(.+)/; + return {redirectUrl: 'https://' + instance + '/watch?v=' + youtubeShortCaptureRegex.exec(details.url)[1]}; + + } else if (details.url.startsWith("https://www.youtube-nocookie.com/embed/")) { + return { redirectUrl: "https://" + instance + "/embed/" + details.url.split("/embed/")[1] }; + } + } + }, + + { + urls: ['*://*.youtube.com/*', '*://*.youtu.be/*', '*://*.youtube-nocookie.com/*'] + }, + + ['blocking'] +); + +// URL Icon Click --> Open on YouTube +browser.pageAction.onClicked.addListener((details) => { + browser.storage.local.get("activated").then((item) => { + if (item.activated) { + deactivateBlocker(); + setTimeout(() => { + activateBlocker(); + }, 10000); + } + + browser.tabs.create({ + url: "https://www.youtube.com/" + details.url.split("/")[3] + }); + }); +}); diff --git a/content.js b/content.js new file mode 100644 index 0000000..68b3629 --- /dev/null +++ b/content.js @@ -0,0 +1 @@ +browser.runtime.sendMessage({ type: "showPageAction" }); \ No newline at end of file diff --git a/icons/Icon.png b/icons/Icon.png new file mode 100644 index 0000000..0c11619 Binary files /dev/null and b/icons/Icon.png differ diff --git a/icons/piped.png b/icons/piped.png new file mode 100644 index 0000000..6fa0969 Binary files /dev/null and b/icons/piped.png differ diff --git a/icons/youtube.png b/icons/youtube.png new file mode 100644 index 0000000..d4e74d8 Binary files /dev/null and b/icons/youtube.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..2e58677 --- /dev/null +++ b/manifest.json @@ -0,0 +1,43 @@ +{ + "manifest_version": 2, + "name": "InviTube", + "author": "Indogermane", + "version": "1.0", + "description": "Redirect YouTube to Invidious.", + "background": { + "scripts": ["background.js"] + }, + + "options_ui": { + "page": "options/options.html" + }, + + "icons": { + "800": "icons/Icon.png" + }, + + "browser_action": { + "browser_style": true, + "default_icon": "icons/piped.png" + }, + + "page_action": { + "default_icon": "icons/youtube.png", + "default_title": "Open on YouTube" + }, + + "permissions": [ + "webRequest", + "webRequestBlocking", + "", + "storage", + "tabs" + ], + + "browser_specific_settings": { + "gecko": { + "id": "InviTube@Indogermane", + "strict_min_version": "79.0" + } + } +} diff --git a/options/options.html b/options/options.html new file mode 100644 index 0000000..ab80030 --- /dev/null +++ b/options/options.html @@ -0,0 +1,101 @@ + + + + + + + + +
+

+ Instance URL: + +
+ Redirect images + +
+ +

+
+ + + diff --git a/options/options.js b/options/options.js new file mode 100644 index 0000000..a5f0c94 --- /dev/null +++ b/options/options.js @@ -0,0 +1,27 @@ +// Save options +document.querySelector("form").addEventListener("submit", (e) => { + e.preventDefault(); + let instance = document.querySelector("#instance").value.trim(); + instance = instance.replace(/^https?:?\/?\/?/, '').replace('\/$'); + proxy = proxy.replace(/^https?:?\/?\/?/, '').replace('\/$'); + if (!instance || !proxy) { + return alert("Please fill out the fields!"); + } + + browser.storage.local.set({ + instance: instance, + imageRedirect: document.querySelector("#imageRedirect").checked + }).then(() => alert("Saved!")); +}); + + +// Restore options +document.addEventListener("DOMContentLoaded", () => { + browser.storage.local.get({ + imageRedirect: true, + instance: "redirect.invidio.us", + }).then((item) => { + document.querySelector("#instance").value = item.instance; + document.querySelector("#imageRedirect").checked = item.imageRedirect; + }); +}); \ No newline at end of file