Merge pull request #3 from ManeraKai/change-instance-btn

Change instance btn
This commit is contained in:
alefvanoon 2021-12-04 20:37:50 +01:00 committed by GitHub
commit 5fa9ff1de3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 31 deletions

View File

@ -14,7 +14,25 @@
"48": "assets/images/icon48.png", "48": "assets/images/icon48.png",
"128": "assets/images/icon128.png" "128": "assets/images/icon128.png"
}, },
"permissions": ["storage", "webRequest", "webRequestBlocking", "<all_urls>"], "permissions": [
"tabs",
"storage",
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"page_action": {
"show_matches": [
"<all_urls>"
],
"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": { "browser_action": {
"default_title": "Privacy Redirect", "default_title": "Privacy Redirect",
"default_popup": "pages/popup/popup.html", "default_popup": "pages/popup/popup.html",
@ -34,7 +52,9 @@
"*://pbs.twimg.com/*", "*://pbs.twimg.com/*",
"*://video.twimg.com/*" "*://video.twimg.com/*"
], ],
"js": ["assets/javascripts/remove-twitter-sw.js"], "js": [
"assets/javascripts/remove-twitter-sw.js"
],
"run_at": "document_start" "run_at": "document_start"
}, },
{ {
@ -63,7 +83,9 @@
"*://c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid.onion/*", "*://c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid.onion/*",
"*://w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd.onion/*" "*://w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd.onion/*"
], ],
"js": ["assets/javascripts/persist-invidious-prefs.js"], "js": [
"assets/javascripts/persist-invidious-prefs.js"
],
"run_at": "document_start" "run_at": "document_start"
} }
], ],
@ -71,7 +93,9 @@
"page": "pages/options/options.html", "page": "pages/options/options.html",
"open_in_tab": false "open_in_tab": false
}, },
"web_accessible_resources": ["assets/javascripts/helpers/*"], "web_accessible_resources": [
"assets/javascripts/helpers/*"
],
"browser_specific_settings": { "browser_specific_settings": {
"gecko": { "gecko": {
"id": "{b7f9d2cd-d772-4302-8c3f-eb941af36f76}", "id": "{b7f9d2cd-d772-4302-8c3f-eb941af36f76}",

View File

@ -323,8 +323,7 @@ function redirectYouTube(url, initiator, type) {
url.searchParams.append("autoplay", 1); url.searchParams.append("autoplay", 1);
} }
return `${ return `${invidiousInstance || commonHelper.getRandomInstance(invidiousRandomPool)
invidiousInstance || commonHelper.getRandomInstance(invidiousRandomPool)
}${url.pathname.replace("/shorts", "")}${url.search}`; }${url.pathname.replace("/shorts", "")}${url.search}`;
} }
@ -348,16 +347,13 @@ function redirectTwitter(url, initiator) {
return null; return null;
} }
if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") { if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") {
return `${ return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
}/pic/${encodeURIComponent(url.href)}`; }/pic/${encodeURIComponent(url.href)}`;
} else if (url.pathname.split("/").includes("tweets")) { } else if (url.pathname.split("/").includes("tweets")) {
return `${ return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
}${url.pathname.replace("/tweets", "")}${url.search}`; }${url.pathname.replace("/tweets", "")}${url.search}`;
} else { } else {
return `${ return `${nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
nitterInstance || commonHelper.getRandomInstance(nitterRandomPool)
}${url.pathname}${url.search}`; }${url.pathname}${url.search}`;
} }
} }
@ -383,13 +379,11 @@ function redirectInstagram(url, initiator, type) {
url.pathname === "/" || url.pathname === "/" ||
instagramReservedPaths.includes(url.pathname.split("/")[1]) instagramReservedPaths.includes(url.pathname.split("/")[1])
) { ) {
return `${ return `${bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool)
bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool)
}${url.pathname}${url.search}`; }${url.pathname}${url.search}`;
} else { } else {
// Likely a user profile, redirect to '/u/...' // Likely a user profile, redirect to '/u/...'
return `${ return `${bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool)
bibliogramInstance || commonHelper.getRandomInstance(bibliogramRandomPool)
}/u${url.pathname}${url.search}`; }/u${url.pathname}${url.search}`;
} }
} }
@ -416,8 +410,7 @@ function redirectGoogleMaps(url, initiator) {
params = "&zoom=17"; params = "&zoom=17";
} }
// Set map layer // Set map layer
params = `${params}&layers=${ params = `${params}&layers=${layers[url.searchParams.get("layer")] || layers["none"]
layers[url.searchParams.get("layer")] || layers["none"]
}`; }`;
// Handle Google Maps Embed API // Handle Google Maps Embed API
if (url.pathname.split("/").includes("embed")) { if (url.pathname.split("/").includes("embed")) {
@ -484,8 +477,7 @@ function redirectGoogleMaps(url, initiator) {
} else if (url.pathname.match(placeRegex)) { } else if (url.pathname.match(placeRegex)) {
query = url.pathname.match(placeRegex)[1]; query = url.pathname.match(placeRegex)[1];
} }
redirect = `${osmInstance}/${query ? "search?query=" + query : ""}${ redirect = `${osmInstance}/${query ? "search?query=" + query : ""}${mapCentre || "#"
mapCentre || "#"
}${params}`; }${params}`;
} }
@ -586,6 +578,8 @@ function redirectGoogleTranslate(url, initiator) {
return `${simplyTranslateInstance}/${url.search}`; return `${simplyTranslateInstance}/${url.search}`;
} }
var oldDomain = '';
function redirectWikipedia(url, initiator) { function redirectWikipedia(url, initiator) {
if (disableWikipedia || isException(url, initiator)) { if (disableWikipedia || isException(url, initiator)) {
return null; return null;
@ -622,6 +616,8 @@ function redirectWikipedia(url, initiator) {
else return null; else return null;
} }
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
(details) => { (details) => {
const url = new URL(details.url); const url = new URL(details.url);
@ -633,22 +629,27 @@ browser.webRequest.onBeforeRequest.addListener(
} }
let redirect; let redirect;
if (youtubeDomains.includes(url.host)) { if (youtubeDomains.includes(url.host)) {
oldDomain = 'https://youtube.com/';
redirect = { redirect = {
redirectUrl: redirectYouTube(url, initiator, details.type), redirectUrl: redirectYouTube(url, initiator, details.type),
}; };
} else if (twitterDomains.includes(url.host)) { } else if (twitterDomains.includes(url.host)) {
oldDomain = 'https://twitter.com/';
redirect = { redirect = {
redirectUrl: redirectTwitter(url, initiator), redirectUrl: redirectTwitter(url, initiator),
}; };
} else if (instagramDomains.includes(url.host)) { } else if (instagramDomains.includes(url.host)) {
oldDomain = 'https://instagram.com/';
redirect = { redirect = {
redirectUrl: redirectInstagram(url, initiator, details.type), redirectUrl: redirectInstagram(url, initiator, details.type),
}; };
} else if (url.href.match(googleMapsRegex)) { } else if (url.href.match(googleMapsRegex)) {
oldDomain = 'https://maps.google.com/';
redirect = { redirect = {
redirectUrl: redirectGoogleMaps(url, initiator), redirectUrl: redirectGoogleMaps(url, initiator),
}; };
} else if (redditDomains.includes(url.host)) { } else if (redditDomains.includes(url.host)) {
oldDomain = 'https://reddit.com/';
redirect = { redirect = {
redirectUrl: redirectReddit(url, initiator, details.type), redirectUrl: redirectReddit(url, initiator, details.type),
}; };
@ -657,14 +658,17 @@ browser.webRequest.onBeforeRequest.addListener(
redirectUrl: redirectMedium(url, initiator), redirectUrl: redirectMedium(url, initiator),
}; };
} else if (url.href.match(googleSearchRegex)) { } else if (url.href.match(googleSearchRegex)) {
oldDomain = 'https://google.com/';
redirect = { redirect = {
redirectUrl: redirectSearchEngine(url, initiator), redirectUrl: redirectSearchEngine(url, initiator),
}; };
} else if (googleTranslateDomains.includes(url.host)) { } else if (googleTranslateDomains.includes(url.host)) {
oldDomain = 'https://translate.google.com/';
redirect = { redirect = {
redirectUrl: redirectGoogleTranslate(url, initiator), redirectUrl: redirectGoogleTranslate(url, initiator),
}; };
} else if (url.host.match(wikipediaRegex)) { } else if (url.host.match(wikipediaRegex)) {
oldDomain = 'https://wikipedia.com/';
redirect = { redirect = {
redirectUrl: redirectWikipedia(url, initiator), redirectUrl: redirectWikipedia(url, initiator),
}; };
@ -686,6 +690,8 @@ browser.webRequest.onBeforeRequest.addListener(
["blocking"] ["blocking"]
); );
browser.runtime.onInstalled.addListener((details) => { browser.runtime.onInstalled.addListener((details) => {
browser.storage.sync.get( browser.storage.sync.get(
["disableSearchEngine", "disableSimplyTranslate", "disableWikipedia"], ["disableSearchEngine", "disableSimplyTranslate", "disableWikipedia"],
@ -729,3 +735,21 @@ browser.runtime.onInstalled.addListener((details) => {
); );
} }
}); });
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(changeInstance);