mirror of
https://codeberg.org/Nero/InviTube.git
synced 2024-07-07 00:03:26 +00:00
129 lines
3.2 KiB
JavaScript
129 lines
3.2 KiB
JavaScript
|
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]
|
||
|
});
|
||
|
});
|
||
|
});
|