This commit is contained in:
Noel Aeby 2024-05-20 14:41:14 +02:00 committed by GitHub
commit 859813ca04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 146 additions and 18 deletions

View file

@ -2,27 +2,63 @@ window.browser = window.browser || window.chrome;
const INVIDIOUS_INSTANCES = [];
// Those are the default settings and also required as reading from local storage before deciding on whether to reject the request appears to be too slow
var activeInstance = "piped.video";
var redirectDisabled = false;
// Reading settings from local storage on startup
browser.storage.local.get().then(localStorage => {
if(localStorage.disabled) redirectDisabled = true;
if(typeof localStorage.instance != "undefined") activeInstance = localStorage.instance;
});
// Instance decoding from api docs and put in local storage for ui to retrieve when loading
fetch("https://raw.githubusercontent.com/wiki/TeamPiped/Piped-Frontend/Instances.md")
.then(resp => resp.text())
.then(body => {
var instances = [];
let lines = body.split("\n");
lines.map(line => {
let split = line.split("|");
if(split.length == 5 && split[0].indexOf(" libre") == -1) {
instances.push(split[0]);
}
});
browser.storage.local.set({instances: instances.slice(2)});
});
// Fetching invidious instances and redirecting
fetch("https://api.invidious.io/instances.json")
.then((resp) => resp.json())
.then((array) => array.forEach((json) => INVIDIOUS_INSTANCES.push(json[0])));
browser.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
if (url.hostname.endsWith("youtu.be") && url.pathname.length > 1) {
return { redirectUrl: "https://piped.kavin.rocks/watch?v=" + url.pathname.substr(1) };
}
if (
url.hostname.endsWith("youtube.com") ||
url.hostname.endsWith("youtube-nocookie.com") ||
INVIDIOUS_INSTANCES.includes(url.hostname)
) {
url.hostname = "piped.kavin.rocks";
return { redirectUrl: url.href };
}
browser.webRequest.onBeforeRequest.addListener(
(details) => {
if(!redirectDisabled) {
const url = new URL(details.url);
if (url.hostname.endsWith("youtu.be") && url.pathname.length > 1) {
return { redirectUrl: "https://" + activeInstance + "/watch?v=" + url.pathname.substr(1) };
}
if (
url.hostname.endsWith("youtube.com") ||
url.hostname.endsWith("youtube-nocookie.com") ||
INVIDIOUS_INSTANCES.includes(url.hostname)
) {
url.hostname = activeInstance;
return { redirectUrl: url.href };
}
}
},
{
urls: ["<all_urls>"],
},
["blocking"],
);
);
// Realtime communication needed to stay in sync with user preferences
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.action == "instanceChanged") {
activeInstance = request.value;
} else if(request.action == "statusChanged") {
redirectDisabled = request.value;
}
});

View file

@ -1,6 +1,6 @@
{
"name": "Piped Redirects",
"version": "1.2",
"version": "2.0",
"description": "Redirects YouTube links to Piped",
"permissions": [
"webRequest",
@ -8,7 +8,8 @@
"*://youtu.be/*",
"*://*.youtube.com/*",
"*://*.youtube-nocookie.com/*",
"<all_urls>"
"<all_urls>",
"storage"
],
"icons": {
"48": "img/logo-48.png",
@ -20,5 +21,10 @@
"js/background.js"
]
},
"browser_action": {
"default_icon": "img/logo.svg",
"default_title": "Piped Redirects",
"default_popup": "popup/ui.html"
},
"manifest_version": 2
}

12
popup/ui.css Normal file
View file

@ -0,0 +1,12 @@
body {
background-color: rgba(0, 0, 0, 0.8);
text-align: center;
color: white;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji, ui-sans-serif, system-ui;
}
a {
color: white;
}
* {
margin: 5px;
}

21
popup/ui.html Normal file
View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="ui.css"/>
</head>
<body>
<img src="../img/logo-128.png" id="STATUS_BTN">
<div>
Enable/Disable piped redirects
</div>
<hr>
<div>Select instance</div>
<select id="PIPED_INSTANCES">
<option value="piped.video">Piped Official</option>
</select>
<div>
<a href="https://github.com/TeamPiped/Piped/wiki/Instances">About Instances</a>
</div>
<script src="ui.js"></script>
</body>
</html>

53
popup/ui.js Normal file
View file

@ -0,0 +1,53 @@
window.browser = window.browser || window.chrome;
const instanceSelector = document.getElementById("PIPED_INSTANCES");
const statusBtn = document.getElementById("STATUS_BTN");
var isDisabled = false;
function updateStatus(disabled) {
if(!disabled) {
statusBtn.style.opacity = "1";
} else {
statusBtn.style.opacity = "0.4";
}
}
// Read settings from local storage
browser.storage.local.get().then(localStorage => {
// Set instances
localStorage.instances.forEach(instance => {
let instanceOption = document.createElement("option");
instanceOption.innerText = instance.trim();
instanceOption.value = "piped." + instance.replace("(Official)", "").trim();
instanceSelector.appendChild(instanceOption);
});
// check if redirections are disabled
if(localStorage.disabled) {
updateStatus(true);
isDisabled = true;
}
// select correct instance according to settings
if(typeof localStorage.instance != "undefined") {
instanceSelector.value = localStorage.instance;
} else instanceSelector.value = "piped.video";
});
statusBtn.addEventListener("click", () => {
isDisabled = !isDisabled;
updateStatus(isDisabled);
browser.runtime.sendMessage({
action: "statusChanged",
value: isDisabled
});
browser.storage.local.set({disabled: isDisabled});
});
instanceSelector.addEventListener("change", () => {
browser.runtime.sendMessage({
action: "instanceChanged",
value: instanceSelector.value
});
browser.storage.local.set({instance: instanceSelector.value});
});