Initial version.

This commit is contained in:
aminecmi 2022-07-15 15:46:52 +02:00
parent fa4926b79f
commit d52f37667c
7 changed files with 140 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
web-ext-artifacts/
.web-extension-id

49
background.js Normal file
View File

@ -0,0 +1,49 @@
// Storage init
browser.runtime.onInstalled.addListener(function() {
browser.storage.sync.set({
url: "https://invidious.snopyta.org"
});
});
// Url format
function getVideoToken(url) {
// https://regexr.com/531i0
var rx = /(?:\/|%3D|v=|vi=)([0-9A-z-_]{11})(?:[%#?&]|$)/g;
var arr = rx.exec(url);
return arr[1];
}
function cleanToken(token) {
let t = token;
// https://regexr.com/531i0
t = t.replace(/^(vi=|v=)/,"");
t = t.replace(/^(\/)/,"");
t = t.replace(/^(%3D)/,"");
t = t.replace(/(&|%)$/,"");
return t;
}
// Links click event handling
browser.runtime.onMessage.addListener(function(message) {
let url = message.youtubeUrl;
const name = message.targetName;
browser.storage.sync.get("url").then(function(item) {
const subst = item.url + `/watch?v=`;
let token = getVideoToken(url)
const cleanedToken = cleanToken(token)
url = url.replace(/.*\/watch\?v=/gm, subst);
if (name != "_self") {
browser.tabs.create(
{active: true, url: url}
);
} else {
browser.tabs.update(
{url: url, loadReplace: true}
)
}
console.log('Youtube to invidious is redirecting you to invidious')
})
});

BIN
icons/48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

35
manifest.json Normal file
View File

@ -0,0 +1,35 @@
{
"manifest_version": 2,
"name": "Youtube to invidious",
"version": "0.1",
"description": "Change every youtube link to an invidious one",
"icons": {
"48": "icons/48.png"
},
"web_accessible_resources": ["icons/48.png"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["to-invidious.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"options_ui": {
"page": "options.html"
},
"permissions": ["storage", "tabs"],
"browser_specific_settings": {
"gecko": {
"id": "1f63659fb336c2593b0f48375566cf66c5d8e12d@amine-louveau.fr"
}
}
}

20
options.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form>
<label>Invidious instance url<input type="text" id="url" ></label>
<button type="submit">Save</button>
</form>
<script src="options.js"></script>
</body>
</html>

23
options.js Normal file
View File

@ -0,0 +1,23 @@
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
url: document.querySelector("#url").value
});
}
function restoreOptions() {
function setCurrentChoice(result) {
document.querySelector("#url").value = result.url;
}
function onError(error) {
console.log(`Error: ${error}`);
}
let getting = browser.storage.sync.get("url");
getting.then(setCurrentChoice, onError);
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);

11
to-invidious.js Normal file
View File

@ -0,0 +1,11 @@
window.addEventListener("click", (e) => {
var target = e.target;
while ((target.tagName != "A" || !target.href) && target.parentNode) {
target = target.parentNode;
}
if (target.tagName != "A" || target.href.indexOf('youtube') == -1)
return;
e.preventDefault();
browser.runtime.sendMessage({"youtubeUrl": target.href, "targetName": (target.attributes.target != null ? target.attributes.target.nodeValue : '_self')});
});