+ 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(
{
@ -11,33 +13,50 @@ browser.storage.local.get(
deactivateBlocker();
}
registerCS(storage.instance);
registerCS(storage.instance);
});
// ContentScript für aktuelle Instanz registrieren
async function registerCS(url) {
return await browser.contentScripts.register({
js: [{file: "content.js"}],
matches: ["*://" + url + "/*"]
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
browser.storage.onChanged.addListener((changes) => {
if ("instance" in changes) {
registerCS(changes.instance.newValue);
}
if ("enabledBlock" in changes) {
enabledBlock = changes.enabledBlock.newValue;
}
if ("instance" in changes) {
registerCS(changes.instance.newValue);
}
if ("enabledBlock" in changes) {
enabledBlock = changes.enabledBlock.newValue;
}
});
// Wenn Icon geklickt wird Blocker de-/aktivieren
@ -52,51 +71,52 @@ 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) => {
let activated;
let instance;
if (!tabWhitelist.includes(details.tabId)) {
await browser.storage.local.get({
activated: true,
instance: "yewtu.be",
}).then((item) => {
activated = item.activated;
instance = item.instance;
});
let activated;
let instance;
if (activated) {
const youtubeRegex = /youtube.com(\/?.*)/;
const youtubeShortRegex = /youtu.be\/.+/;
await browser.storage.local.get({
activated: true,
instance: "yewtu.be",
}).then((item) => {
activated = item.activated;
instance = item.instance;
});
if (details.url.startsWith("https://img.youtube.com/vi")) {
console.log("JOOOOO");
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] };
if (activated) {
const youtubeRegex = /youtube.com(\/?.*)/;
const youtubeShortRegex = /youtu.be\/.+/;
} else if (youtubeShortRegex.test(details.url)) {
const youtubeShortCaptureRegex = /youtu.be\/(.+)/;
return {redirectUrl: 'https://' + instance + '/watch?v=' + youtubeShortCaptureRegex.exec(details.url)[1]};
if (details.url.startsWith("https://img.youtube.com/vi")) {
console.log("JOOOOO");
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/")) {
return { redirectUrl: "https://" + instance + "/embed/" + details.url.split("/embed/")[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/*']
},
@ -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://" + 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);
}
}
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,
"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"
"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",
"<all_urls>",
"storage",
"tabs"
"storage",
"tabs"
],
"browser_specific_settings": {
"gecko": {
"id": "InviTube@Indogermane",
"strict_min_version": "79.0"
}
}
}
}