From 398b122ec66a729a19446816a131ecc72fa46905 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Thu, 21 Oct 2021 20:37:28 +0300 Subject: [PATCH 1/2] Added Change Instance btn to address-bar --- src/manifest.json | 33 +++++++++++--- src/pages/background/background.js | 69 +++++++++++++++++++----------- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 9a2031f..25b37e1 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -14,7 +14,24 @@ "48": "assets/images/icon48.png", "128": "assets/images/icon128.png" }, - "permissions": ["storage", "webRequest", "webRequestBlocking", ""], + "permissions": [ + "storage", + "webRequest", + "webRequestBlocking", + "" + ], + "page_action": { + "show_matches": [ + "" + ], + "default_title": "Change Instance", + "default_icon": { + "16": "assets/images/icon16.png", + "32": "assets/images/icon32.png", + "48": "assets/images/icon48.png", + "128": "assets/images/icon128.png" + } + }, "browser_action": { "default_title": "Privacy Redirect", "default_popup": "pages/popup/popup.html", @@ -34,7 +51,9 @@ "*://pbs.twimg.com/*", "*://video.twimg.com/*" ], - "js": ["assets/javascripts/remove-twitter-sw.js"], + "js": [ + "assets/javascripts/remove-twitter-sw.js" + ], "run_at": "document_start" }, { @@ -63,7 +82,9 @@ "*://c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid.onion/*", "*://w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd.onion/*" ], - "js": ["assets/javascripts/persist-invidious-prefs.js"], + "js": [ + "assets/javascripts/persist-invidious-prefs.js" + ], "run_at": "document_start" } ], @@ -71,11 +92,13 @@ "page": "pages/options/options.html", "open_in_tab": false }, - "web_accessible_resources": ["assets/javascripts/helpers/*"], + "web_accessible_resources": [ + "assets/javascripts/helpers/*" + ], "browser_specific_settings": { "gecko": { "id": "{b7f9d2cd-d772-4302-8c3f-eb941af36f76}", "strict_min_version": "67.0" } } -} +} \ No newline at end of file diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 9b8c3a1..ec8ae57 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -126,8 +126,8 @@ browser.storage.sync.get( invidiousDarkMode = result.invidiousDarkMode; exceptions = result.exceptions ? result.exceptions.map((e) => { - return new RegExp(e); - }) + return new RegExp(e); + }) : []; invidiousVolume = result.invidiousVolume; invidiousPlayerStyle = result.invidiousPlayerStyle; @@ -299,9 +299,8 @@ function redirectYouTube(url, initiator, type) { url.searchParams.append("autoplay", 1); } - return `${ - invidiousInstance || commonHelper.getRandomInstance(invidiousRandomPool) - }${url.pathname.replace("/shorts", "")}${url.search}`; + return `${invidiousInstance || commonHelper.getRandomInstance(invidiousRandomPool) + }${url.pathname.replace("/shorts", "")}${url.search}`; } function redirectTwitter(url, initiator) { @@ -324,17 +323,14 @@ function redirectTwitter(url, initiator) { return null; } if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") { - return `${ - nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) - }/pic/${encodeURIComponent(url.href)}`; + return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) + }/pic/${encodeURIComponent(url.href)}`; } else if (url.pathname.split("/").includes("tweets")) { - return `${ - nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) - }${url.pathname.replace("/tweets", "")}${url.search}`; + return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) + }${url.pathname.replace("/tweets", "")}${url.search}`; } else { - return `${ - nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) - }${url.pathname}${url.search}`; + return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool) + }${url.pathname}${url.search}`; } } @@ -359,14 +355,12 @@ function redirectInstagram(url, initiator, type) { url.pathname === "/" || instagramReservedPaths.includes(url.pathname.split("/")[1]) ) { - return `${ - bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool) - }${url.pathname}${url.search}`; + return `${bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool) + }${url.pathname}${url.search}`; } else { // Likely a user profile, redirect to '/u/...' - return `${ - bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool) - }/u${url.pathname}${url.search}`; + return `${bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool) + }/u${url.pathname}${url.search}`; } } @@ -392,9 +386,8 @@ function redirectGoogleMaps(url, initiator) { params = "&zoom=17"; } // Set map layer - params = `${params}&layers=${ - layers[url.searchParams.get("layer")] || layers["none"] - }`; + params = `${params}&layers=${layers[url.searchParams.get("layer")] || layers["none"] + }`; // Handle Google Maps Embed API if (url.pathname.split("/").includes("embed")) { let query = ""; @@ -460,9 +453,8 @@ function redirectGoogleMaps(url, initiator) { } else if (url.pathname.match(placeRegex)) { query = url.pathname.match(placeRegex)[1]; } - redirect = `${osmInstance}/${query ? "search?query=" + query : ""}${ - mapCentre || "#" - }${params}`; + redirect = `${osmInstance}/${query ? "search?query=" + query : ""}${mapCentre || "#" + }${params}`; } return redirect; @@ -541,6 +533,8 @@ function redirectGoogleTranslate(url, initiator) { return `${simplyTranslateInstance}/${url.search}`; } +var oldDomain = ''; + function redirectWikipedia(url, initiator) { if (disableWikipedia || isException(url, initiator)) { return null; @@ -577,6 +571,8 @@ function redirectWikipedia(url, initiator) { else return null; } + + browser.webRequest.onBeforeRequest.addListener( (details) => { const url = new URL(details.url); @@ -588,34 +584,42 @@ browser.webRequest.onBeforeRequest.addListener( } let redirect; if (youtubeDomains.includes(url.host)) { + oldDomain = 'https://youtube.com/'; redirect = { redirectUrl: redirectYouTube(url, initiator, details.type), }; } else if (twitterDomains.includes(url.host)) { + oldDomain = 'https://twitter.com/'; redirect = { redirectUrl: redirectTwitter(url, initiator), }; } else if (instagramDomains.includes(url.host)) { + oldDomain = 'https://instagram.com/'; redirect = { redirectUrl: redirectInstagram(url, initiator, details.type), }; } else if (url.href.match(googleMapsRegex)) { + oldDomain = 'https://maps.google.com/'; redirect = { redirectUrl: redirectGoogleMaps(url, initiator), }; } else if (redditDomains.includes(url.host)) { + oldDomain = 'https://reddit.com/'; redirect = { redirectUrl: redirectReddit(url, initiator, details.type), }; } else if (url.href.match(googleSearchRegex)) { + oldDomain = 'https://google.com/'; redirect = { redirectUrl: redirectSearchEngine(url, initiator), }; } else if (googleTranslateDomains.includes(url.host)) { + oldDomain = 'https://translate.google.com/'; redirect = { redirectUrl: redirectGoogleTranslate(url, initiator), }; } else if (url.host.match(wikipediaRegex)) { + oldDomain = 'https://wikipedia.com/'; redirect = { redirectUrl: redirectWikipedia(url, initiator), }; @@ -637,6 +641,8 @@ browser.webRequest.onBeforeRequest.addListener( ["blocking"] ); + + browser.runtime.onInstalled.addListener((details) => { browser.storage.sync.get( ["disableSearchEngine", "disableSimplyTranslate", "disableWikipedia"], @@ -680,3 +686,14 @@ browser.runtime.onInstalled.addListener((details) => { ); } }); + + +function openPage() { + if (oldDomain != '') { + browser.tabs.update({ + url: oldDomain + }); + } + +} +browser.pageAction.onClicked.addListener(openPage); \ No newline at end of file From 97d3d19b7d65720f6d1677d31fe1ce670ac63580 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Wed, 27 Oct 2021 10:18:05 +0300 Subject: [PATCH 2/2] Preserved url's path. Only the domain will change now. --- src/manifest.json | 1 + src/pages/background/background.js | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 25b37e1..83557cf 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -15,6 +15,7 @@ "128": "assets/images/icon128.png" }, "permissions": [ + "tabs", "storage", "webRequest", "webRequestBlocking", diff --git a/src/pages/background/background.js b/src/pages/background/background.js index ec8ae57..9829feb 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -688,12 +688,19 @@ browser.runtime.onInstalled.addListener((details) => { }); -function openPage() { - if (oldDomain != '') { - browser.tabs.update({ - url: oldDomain - }); - } +function changeInstance() { + browser.tabs.query({ + active: true, + lastFocusedWindow: true + }, function (tabs) { + var tabUrl = new URL(tabs[0].url); + if (oldDomain != '') { + browser.tabs.update({ + url: tabUrl.href.replace(`${tabUrl.protocol}//${tabUrl.host}/`, oldDomain) + }); + } + }); } -browser.pageAction.onClicked.addListener(openPage); \ No newline at end of file + +browser.pageAction.onClicked.addListener(changeInstance); \ No newline at end of file