metiis / invidious-redirect

A (very) small WebExtension to redirect YouTube links to Invidious.
The Unlicense
39 stars 20 forks source link

Consider webRequest.onBeforeRequest instead of window.location #10

Open stoically opened 5 years ago

stoically commented 5 years ago

Currently requests to youtube will still hit their servers and only then the content script is loaded, which in turn redirects to invidious. Consider to instead register an onBeforeRequest listener, which can cancel the request to youtube and redirect to invidious.

Something along the lines of

manifest.json ``` "permissions": { "*://*.youtube.com/*", "*://*.youtube-nocookie.com/*", "*://*.youtu.be/*, "webRequest", "webRequestBlocking" }, "background": { "scripts": ["background.js"] } ```
background.js ```js browser.webRequest.onBeforeRequest.addListener(request => { const url = new URL(request.url); if (url.hostname.endsWith("youtube.com") || url.hostname.endsWith("youtube-nocookie.com")) { const v = url.searchParams.get("v"); if (v) { return {redirectUrl: `https://invidio.us/watch?v=${v}`}; } const q = url.searchParams.get("q"); if (q) { return {redirectUrl: `https://invidio.us/search?q=${q}`}; } const searchQuery = url.searchParams.get("search_query"); if (searchQuery) { return {redirectUrl: `https://invidio.us/search?q=${searchQuery}`}; } if (url.pathname.startsWith("/channel/")) { return {redirectUrl: `https://invidio.us${url.pathname}`}; } } if (url.hostname.endsWith("youtu.be")) { return {redirectUrl: `https://invidio.us/watch?v=${url.pathname.replace("/", "")}`}; } }, { "urls": ["*://*.youtube.com/*", "*://*.youtube-nocookie.com/*", "*://*.youtu.be/*"], "types": ["main_frame"] }, ["blocking"] ); ```
doronbehar commented 5 years ago

If anyone is interested (@stoically , @ghost, @Perflyst), I've incorporated this very useful suggestion along with my configuration (#9) and embed support merged on my fork. I'm sorry but it seems the developer just won't give his attention.

Note I've renamed the id of the extension so I'd be able to install it signed my self.

samiraguiar commented 5 years ago

@doronbehar thanks for the work. Since the developer of this extension seems to be inactive, why don't you publish your version to Mozilla? I thought of creating an issue in your repo but issues are disabled there.

doronbehar commented 5 years ago

It should be available here in a few hours / days - after reviewed by Firefox. Also enabled issues in my fork.