2023-06-17 11:47:15 +00:00
|
|
|
// Einstellungen ziehen, ContentScript registrieren
|
|
|
|
browser.storage.local.get(
|
|
|
|
{
|
|
|
|
activated: true,
|
2023-06-17 12:12:32 +00:00
|
|
|
instance: "yewtu.be",
|
2023-06-17 11:47:15 +00:00
|
|
|
}
|
|
|
|
).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;
|
|
|
|
|
|
|
|
await browser.storage.local.get({
|
|
|
|
activated: true,
|
2023-06-17 12:12:32 +00:00
|
|
|
instance: "yewtu.be",
|
2023-06-17 11:47:15 +00:00
|
|
|
}).then((item) => {
|
|
|
|
activated = item.activated;
|
|
|
|
instance = item.instance;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (activated) {
|
|
|
|
const youtubeRegex = /youtube.com(\/?.*)/;
|
|
|
|
const youtubeShortRegex = /youtu.be\/.+/;
|
|
|
|
|
2023-06-17 12:12:32 +00:00
|
|
|
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" }
|
2023-06-17 11:47:15 +00:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|