From d52f37667c22d47dae5f7682a728dc0b916b6e6b Mon Sep 17 00:00:00 2001 From: aminecmi Date: Fri, 15 Jul 2022 15:46:52 +0200 Subject: [PATCH] Initial version. --- .gitignore | 2 ++ background.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ icons/48.png | Bin 0 -> 3876 bytes manifest.json | 35 ++++++++++++++++++++++++++++++++++ options.html | 20 ++++++++++++++++++++ options.js | 23 +++++++++++++++++++++++ to-invidious.js | 11 +++++++++++ 7 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 background.js create mode 100644 icons/48.png create mode 100644 manifest.json create mode 100644 options.html create mode 100644 options.js create mode 100644 to-invidious.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eed2341 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +web-ext-artifacts/ +.web-extension-id diff --git a/background.js b/background.js new file mode 100644 index 0000000..d5bc742 --- /dev/null +++ b/background.js @@ -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') + }) +}); \ No newline at end of file diff --git a/icons/48.png b/icons/48.png new file mode 100644 index 0000000000000000000000000000000000000000..0548069dfab33c25ba40849e0b05c084588d5b7c GIT binary patch literal 3876 zcmY*b2{@E(_kYHiGWJ52WE=ZBBwN-&mO@Q-*<#F$Wrmrt6=t%$B%&sy)I=gncw=O# zWZx=FiV(7Ar?QmBf4p!1zVAEFb)NIP&;2{++~>Kk>s-&(v$hso>>}&{0B~7Znjx7M zzOQTtm~SVRokXT#MVr`|0KngA9J}5S=8_w0YI-)n)WQ@Wh$oy6_Vz(ppl~Px)(2^U z005oTR0nsAr~|*@-P%ee>9=tM1qC;`87FR41tR<6hvyln%AiqYYG9Q~dgs!odsx}y zQ@icVbdT=L z=B&odjS*MIIMQpB-7?2ySd>M|RA$~yR<_T!wCIr5`g60StlGr|uM1lb;>X!He6_mXm=w5|f9U>41~a zeV(=U|(ztOto}BiHp$Jbt1e)otFXRdUA`_U=X8Pja>S zr+}7`IJ>k;cr^bwxMnj;k&yX0k(cH$JxWR{SH)Jg^6sH(vCwrG;9mbcfxy0Q*y7`G z`MKyksi;`H@0C*FKsK6pJT4-%QNOEg#cY?Z?wP|L8iUN?oaGS%CMFeCDlToo#42pC zib>t6P8hBBjil|G*&}`|(JLxwtlnU+?jyfs6Xlg_QyYgI3k_SGitcA2etD`q<8PBO z_{$@QlhgqZ-TnR}%;{b>+vH7MK@O1=88t`xI--N@M3>9w^{gh5(6gFU4jQ2<>vLUkX>XRlRN(ile_(XVrjfdVTS=c zv-f9oEYA1A5e0j&wcg^URg44dIcH2HrX;i%>K6${56X z=tbk4uGOD%q}6{Qej!PtMf_Dr9m50n2t9jEo^yj0o^#r+oQ#7pVDjRK9byHlHJT*3L z`1vi7Mxcd^Q##xyR0*C0&k%7U&BA{ma#b^6rTpRF?6cUcbE7nb>yV1Ryl;X*Q*)E= zX}fuWzv7|ZYL~*aOZZX+-RlpuSKxh1qDv`)MZ_gsY2}B{M)s#N56DFoOsyUV+v29; zVOB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Po$9yV!b36z5lmo MWo~O$hVY90KWUW9IsgCw literal 0 HcmV?d00001 diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..12ae35a --- /dev/null +++ b/manifest.json @@ -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": [""], + "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" + } + } + +} + diff --git a/options.html b/options.html new file mode 100644 index 0000000..fa58e01 --- /dev/null +++ b/options.html @@ -0,0 +1,20 @@ + + + + + + + + + +
+ + +
+ + + + + + + diff --git a/options.js b/options.js new file mode 100644 index 0000000..c46dfdb --- /dev/null +++ b/options.js @@ -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); diff --git a/to-invidious.js b/to-invidious.js new file mode 100644 index 0000000..a6f01ce --- /dev/null +++ b/to-invidious.js @@ -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')}); +}); \ No newline at end of file