+ 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
browser.storage.local.get(
{
@ -18,16 +20,33 @@ browser.storage.local.get(
// ContentScript für aktuelle Instanz registrieren
async function registerCS(url) {
return await browser.contentScripts.register({
js: [{file: "content.js"}],
js: [{ file: "content.js" }],
matches: ["*://" + url + "/*"]
});
}
// pageAction anzeigen
browser.runtime.onMessage.addListener((data, sender) => {
if (data.type == "showPageAction") {
browser.pageAction.show(sender.tab.id);
if (data.type == "showYoutubePageAction") {
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
@ -52,18 +71,19 @@ browser.browserAction.onClicked.addListener(() => {
});
function activateBlocker() {
browser.browserAction.setIcon({path:"icons/invidious.png"});
browser.storage.local.set({activated: true});
browser.browserAction.setIcon({ path: "icons/invidious.png" });
browser.storage.local.set({ activated: true });
}
function deactivateBlocker() {
browser.browserAction.setIcon({path: "icons/youtube.png"});
browser.storage.local.set({activated: false});
browser.browserAction.setIcon({ path: "icons/youtube.png" });
browser.storage.local.set({ activated: false });
}
// Redirect
browser.webRequest.onBeforeRequest.addListener(async (details) => {
if (!tabWhitelist.includes(details.tabId)) {
let activated;
let instance;
@ -89,14 +109,14 @@ browser.webRequest.onBeforeRequest.addListener(async (details) => {
} else if (youtubeShortRegex.test(details.url)) {
const youtubeShortCaptureRegex = /youtu.be\/(.+)/;
return {redirectUrl: 'https://' + instance + '/watch?v=' + youtubeShortCaptureRegex.exec(details.url)[1]};
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/*']
},
@ -104,18 +124,27 @@ browser.webRequest.onBeforeRequest.addListener(async (details) => {
['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);
}
// URL Icon Click --> Open on YouTube/Invidious
browser.pageAction.onClicked.addListener((tab) => {
browser.storage.local.get({ activated: true, instance: "yewtu.be" }).then(async (storage) => {
if (tab.url.startsWith("https://www.youtube.com")) {
browser.tabs.create({
url: "https://www.youtube.com/" + details.url.split("/")[3]
url: "https://" + storage.instance + "/" + tab.url.split("/")[3]
});
} 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);
}
}
});
});
// 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,30 +2,37 @@
"manifest_version": 2,
"name": "InviTube",
"author": "Indogermane",
"version": "1.0",
"version": "1.1",
"description": "Redirect YouTube to Invidious.",
"background": {
"scripts": ["background.js"]
"scripts": [
"background.js"
]
},
"options_ui": {
"page": "options/options.html"
},
"icons": {
"800": "icons/Icon.png"
},
"browser_action": {
"browser_style": true,
"default_icon": "icons/invidious.png"
},
"page_action": {
"default_icon": "icons/youtube.png",
"default_title": "Open on YouTube"
},
"content_scripts": [
{
"matches": [
"https://www.youtube.com/*"
],
"js": [
"content.js"
]
}
],
"permissions": [
"webRequest",
"webRequestBlocking",
@ -33,7 +40,6 @@
"storage",
"tabs"
],
"browser_specific_settings": {
"gecko": {
"id": "InviTube@Indogermane",