Copy files from YouPiped

This commit is contained in:
Nero 2023-06-17 13:47:15 +02:00
parent d75f05bb10
commit 312a0664f1
8 changed files with 300 additions and 0 deletions

128
background.js Normal file
View file

@ -0,0 +1,128 @@
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]
});
});
});

1
content.js Normal file
View file

@ -0,0 +1 @@
browser.runtime.sendMessage({ type: "showPageAction" });

BIN
icons/Icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

BIN
icons/piped.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

BIN
icons/youtube.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

43
manifest.json Normal file
View file

@ -0,0 +1,43 @@
{
"manifest_version": 2,
"name": "InviTube",
"author": "Indogermane",
"version": "1.0",
"description": "Redirect YouTube to Invidious.",
"background": {
"scripts": ["background.js"]
},
"options_ui": {
"page": "options/options.html"
},
"icons": {
"800": "icons/Icon.png"
},
"browser_action": {
"browser_style": true,
"default_icon": "icons/piped.png"
},
"page_action": {
"default_icon": "icons/youtube.png",
"default_title": "Open on YouTube"
},
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>",
"storage",
"tabs"
],
"browser_specific_settings": {
"gecko": {
"id": "InviTube@Indogermane",
"strict_min_version": "79.0"
}
}
}

101
options/options.html Normal file
View file

@ -0,0 +1,101 @@
<!DOCTYPE html>
<html>
<head>
<style>
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 23px;
margin: 1.5em 1em;
}
.slider {
position: absolute;
border-radius: 34px;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
border-radius: 34px;
content: "";
height: 17px;
width: 17px;
left: 4px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #009EE0;
}
input:focus + .slider {
box-shadow: 0 0 1px #009EE0;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(16px);
}
p {
font-size: 1.4em;
color: white;
text-align: center;
}
.textfield {
text-align: center;
background-color: #131313;
color: white;
font-size: 1em;
border: none;
border-bottom: 1px solid #ccc;
outline: none;
margin: 1.5em 1em;
}
#saveBtn {
margin: 1.5em 0;
border-radius: 0;
font-size: 1em;
padding: .5em 1em;
border: none;
background-color: #131313;
color: white;
}
</style>
<meta charset="utf-8">
</head>
<body bgcolor="202023">
<form>
<p>
Instance URL:
<input id="instance" class="textfield" placeholder="piped.kavin.rocks"/>
<br>
<label class="switch">
<input type="checkbox" id="imageRedirect">
<span class="slider" />
</label> Redirect images
<span title="Might cause not every image to be loaded">&#x2753;</span>
<br>
<button id="saveBtn" type="submit">Save</button>
</p>
</form>
<script src="options.js"></script>
</body>
</html>

27
options/options.js Normal file
View file

@ -0,0 +1,27 @@
// Save options
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
let instance = document.querySelector("#instance").value.trim();
instance = instance.replace(/^https?:?\/?\/?/, '').replace('\/$');
proxy = proxy.replace(/^https?:?\/?\/?/, '').replace('\/$');
if (!instance || !proxy) {
return alert("Please fill out the fields!");
}
browser.storage.local.set({
instance: instance,
imageRedirect: document.querySelector("#imageRedirect").checked
}).then(() => alert("Saved!"));
});
// Restore options
document.addEventListener("DOMContentLoaded", () => {
browser.storage.local.get({
imageRedirect: true,
instance: "redirect.invidio.us",
}).then((item) => {
document.querySelector("#instance").value = item.instance;
document.querySelector("#imageRedirect").checked = item.imageRedirect;
});
});