Update background.js

This commit is contained in:
Yumi Izumi 2020-11-08 23:50:55 +09:00 committed by GitHub
parent 4bf5f20ad4
commit a69798f525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 0 deletions

View File

@ -129,12 +129,25 @@ const layers = {
traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C",
};
const notPrivateSearchEngine = [
"google.com",
"google.co.jp",
"www.google.com",
"www.google.co.jp",
];
const privateSearchEngine = [
{ link: "https://duckduckgo.com", q: "/" },
{ link: "https://startpage.com", q: "/search/" },
{ link: "https://www.qwant.com", q: "/" },
{ link: "https://www.mojeek.com", q: "/search" },
];
let disableNitter;
let disableInvidious;
let disableBibliogram;
let disableOsm;
let disableOldReddit;
let disableSearchEngine;
let nitterInstance;
let invidiousInstance;
let bibliogramInstance;
@ -172,6 +185,7 @@ browser.storage.sync.get(
"disableBibliogram",
"disableOsm",
"disableOldReddit",
"disableSearchEngine",
"alwaysProxy",
"onlyEmbeddedVideo",
"videoQuality",
@ -192,6 +206,7 @@ browser.storage.sync.get(
disableBibliogram = result.disableBibliogram;
disableOsm = result.disableOsm;
disableOldReddit = result.disableOldReddit;
disableSearchEngine = result.disableSearchEngine;
nitterInstance = result.nitterInstance;
invidiousInstance = result.invidiousInstance;
bibliogramInstance = result.bibliogramInstance;
@ -254,6 +269,9 @@ browser.storage.onChanged.addListener((changes) => {
if ("disableOldReddit" in changes) {
disableOldReddit = changes.disableOldReddit.newValue;
}
if ("disableSearchEngine" in changes) {
disableSearchEngine = changes.disableSearchEngine.newValue;
}
if ("alwaysProxy" in changes) {
alwaysProxy = changes.alwaysProxy.newValue;
}
@ -569,6 +587,28 @@ function redirectReddit(url, initiator, type) {
return `${oldRedditView}${url.pathname}${url.search}`;
}
function redirectSearchEngine(url, initiator) {
if (disableSearchEngine || isException(url, initiator)) {
return null;
}
if (url.pathname.includes("/home")) {
return null;
}
if (url.pathname.includes("search")) {
searchEngine =
searchEngineInstance || getRandomInstance(privateSearchEngine);
search = "";
url.search
.slice(1)
.split("&")
.forEach(function (input) {
if (input.startsWith("q=")) search = input;
});
console.log("search: ", search);
return `${searchEngine.link}${searchEngine.q}?${search}`;
}
}
browser.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
@ -602,6 +642,10 @@ browser.webRequest.onBeforeRequest.addListener(
redirect = {
redirectUrl: redirectReddit(url, initiator, details.type),
};
} else if (notPrivateSearchEngine.includes(url.host)) {
redirect = {
redirectUrl: redirectSearchEngine(url, initiator),
};
}
if (redirect && redirect.redirectUrl) {
console.info(