+ Tab whitelist

This commit is contained in:
Nero 2023-09-03 17:33:31 +02:00
parent d9c9be52d0
commit d2723183a4
3 changed files with 105 additions and 66 deletions

View file

@ -1,3 +1,5 @@
let tabWhitelist = [];
// Einstellungen ziehen, ContentScript registrieren // Einstellungen ziehen, ContentScript registrieren
browser.storage.local.get( browser.storage.local.get(
{ {
@ -11,33 +13,50 @@ browser.storage.local.get(
deactivateBlocker(); deactivateBlocker();
} }
registerCS(storage.instance); registerCS(storage.instance);
}); });
// ContentScript für aktuelle Instanz registrieren // ContentScript für aktuelle Instanz registrieren
async function registerCS(url) { async function registerCS(url) {
return await browser.contentScripts.register({ return await browser.contentScripts.register({
js: [{file: "content.js"}], js: [{ file: "content.js" }],
matches: ["*://" + url + "/*"] matches: ["*://" + url + "/*"]
}); });
} }
// pageAction anzeigen // pageAction anzeigen
browser.runtime.onMessage.addListener((data, sender) => { browser.runtime.onMessage.addListener((data, sender) => {
if (data.type == "showPageAction") { if (data.type == "showYoutubePageAction") {
browser.pageAction.show(sender.tab.id); browser.pageAction.setIcon({
tabId: sender.tab.id,
path: "icons/youtube.png"
});
browser.pageAction.setTitle({
tabId: sender.tab.id,
title: "Open on YouTube"
});
} else if (data.type == "showInvidiousPageAction") {
browser.pageAction.setIcon({
tabId: sender.tab.id,
path: "icons/invidious.png"
});
browser.pageAction.setTitle({
tabId: sender.tab.id,
title: "Open on Invidious"
});
} }
browser.pageAction.show(sender.tab.id);
}) })
// Storage onChange listener // Storage onChange listener
browser.storage.onChanged.addListener((changes) => { browser.storage.onChanged.addListener((changes) => {
if ("instance" in changes) { if ("instance" in changes) {
registerCS(changes.instance.newValue); registerCS(changes.instance.newValue);
} }
if ("enabledBlock" in changes) { if ("enabledBlock" in changes) {
enabledBlock = changes.enabledBlock.newValue; enabledBlock = changes.enabledBlock.newValue;
} }
}); });
// Wenn Icon geklickt wird Blocker de-/aktivieren // Wenn Icon geklickt wird Blocker de-/aktivieren
@ -52,51 +71,52 @@ browser.browserAction.onClicked.addListener(() => {
}); });
function activateBlocker() { function activateBlocker() {
browser.browserAction.setIcon({path:"icons/invidious.png"}); browser.browserAction.setIcon({ path: "icons/invidious.png" });
browser.storage.local.set({activated: true}); browser.storage.local.set({ activated: true });
} }
function deactivateBlocker() { function deactivateBlocker() {
browser.browserAction.setIcon({path: "icons/youtube.png"}); browser.browserAction.setIcon({ path: "icons/youtube.png" });
browser.storage.local.set({activated: false}); browser.storage.local.set({ activated: false });
} }
// Redirect // Redirect
browser.webRequest.onBeforeRequest.addListener(async (details) => { browser.webRequest.onBeforeRequest.addListener(async (details) => {
if (!tabWhitelist.includes(details.tabId)) {
let activated;
let instance;
await browser.storage.local.get({ let activated;
activated: true, let instance;
instance: "yewtu.be",
}).then((item) => {
activated = item.activated;
instance = item.instance;
});
if (activated) { await browser.storage.local.get({
const youtubeRegex = /youtube.com(\/?.*)/; activated: true,
const youtubeShortRegex = /youtu.be\/.+/; instance: "yewtu.be",
}).then((item) => {
activated = item.activated;
instance = item.instance;
});
if (details.url.startsWith("https://img.youtube.com/vi")) { if (activated) {
console.log("JOOOOO"); const youtubeRegex = /youtube.com(\/?.*)/;
return { redirectUrl: 'https://' + instance + '/vi/' + details.url.split("/vi/")[1].split("/")[0] + "/mqdefault.jpg" } const youtubeShortRegex = /youtu.be\/.+/;
}
else if (youtubeRegex.test(details.url)) {
return { redirectUrl: 'https://' + instance + youtubeRegex.exec(details.url)[1] };
} else if (youtubeShortRegex.test(details.url)) { if (details.url.startsWith("https://img.youtube.com/vi")) {
const youtubeShortCaptureRegex = /youtu.be\/(.+)/; console.log("JOOOOO");
return {redirectUrl: 'https://' + instance + '/watch?v=' + youtubeShortCaptureRegex.exec(details.url)[1]}; return { redirectUrl: 'https://' + instance + '/vi/' + details.url.split("/vi/")[1].split("/")[0] + "/mqdefault.jpg" }
}
else if (youtubeRegex.test(details.url)) {
return { redirectUrl: 'https://' + instance + youtubeRegex.exec(details.url)[1] };
} else if (details.url.startsWith("https://www.youtube-nocookie.com/embed/")) { } else if (youtubeShortRegex.test(details.url)) {
return { redirectUrl: "https://" + instance + "/embed/" + details.url.split("/embed/")[1] }; 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/*'] urls: ['*://*.youtube.com/*', '*://*.youtu.be/*', '*://*.youtube-nocookie.com/*']
}, },
@ -104,18 +124,27 @@ browser.webRequest.onBeforeRequest.addListener(async (details) => {
['blocking'] ['blocking']
); );
// URL Icon Click --> Open on YouTube // URL Icon Click --> Open on YouTube/Invidious
browser.pageAction.onClicked.addListener((details) => { browser.pageAction.onClicked.addListener((tab) => {
browser.storage.local.get("activated").then((item) => { browser.storage.local.get({ activated: true, instance: "yewtu.be" }).then(async (storage) => {
if (item.activated) { if (tab.url.startsWith("https://www.youtube.com")) {
deactivateBlocker(); browser.tabs.create({
setTimeout(() => { url: "https://" + storage.instance + "/" + tab.url.split("/")[3]
activateBlocker(); });
}, 10000); } else {
let newTab = await browser.tabs.create({
url: "https://www.youtube.com/" + tab.url.split("/")[3]
});
if (!tabWhitelist.includes(newTab.id)) {
tabWhitelist.push(newTab.id);
}
} }
browser.tabs.create({
url: "https://www.youtube.com/" + details.url.split("/")[3]
});
}); });
}); });
// Close tab -> Remove from whitelist
browser.tabs.onRemoved.addListener((tabId) => {
if (tabWhitelist.includes(tabId)) {
tabWhitelist.splice(tabWhitelist.indexOf(tabId), 1);
}
});

View file

@ -1 +1,5 @@
browser.runtime.sendMessage({ type: "showPageAction" }); if (location.href.startsWith("https://www.youtube.com")) {
browser.runtime.sendMessage({ type: "showInvidiousPageAction" });
} else {
browser.runtime.sendMessage({ type: "showYoutubePageAction" });
}

View file

@ -2,42 +2,48 @@
"manifest_version": 2, "manifest_version": 2,
"name": "InviTube", "name": "InviTube",
"author": "Indogermane", "author": "Indogermane",
"version": "1.0", "version": "1.1",
"description": "Redirect YouTube to Invidious.", "description": "Redirect YouTube to Invidious.",
"background": { "background": {
"scripts": ["background.js"] "scripts": [
"background.js"
]
}, },
"options_ui": { "options_ui": {
"page": "options/options.html" "page": "options/options.html"
}, },
"icons": { "icons": {
"800": "icons/Icon.png" "800": "icons/Icon.png"
}, },
"browser_action": { "browser_action": {
"browser_style": true, "browser_style": true,
"default_icon": "icons/invidious.png" "default_icon": "icons/invidious.png"
}, },
"page_action": { "page_action": {
"default_icon": "icons/youtube.png", "default_icon": "icons/youtube.png",
"default_title": "Open on YouTube" "default_title": "Open on YouTube"
}, },
"content_scripts": [
{
"matches": [
"https://www.youtube.com/*"
],
"js": [
"content.js"
]
}
],
"permissions": [ "permissions": [
"webRequest", "webRequest",
"webRequestBlocking", "webRequestBlocking",
"<all_urls>", "<all_urls>",
"storage", "storage",
"tabs" "tabs"
], ],
"browser_specific_settings": { "browser_specific_settings": {
"gecko": { "gecko": {
"id": "InviTube@Indogermane", "id": "InviTube@Indogermane",
"strict_min_version": "79.0" "strict_min_version": "79.0"
} }
} }
} }