libredirect/src/assets/javascripts/helpers/search.js

325 lines
10 KiB
JavaScript
Raw Normal View History

2022-02-12 20:28:36 +01:00
window.browser = window.browser || window.chrome;
import commonHelper from './common.js'
const targets = [
2022-03-01 16:50:40 +01:00
/^https?:\/\/(www\.|maps\.|search\.|)google\.com(\...|)(\/search\?..*|\/$)/,
/^https?:\/\/libredirect\.onion/
2022-03-01 16:50:40 +01:00
// /^https?:\/\/yandex\.com(\...|)(\/search\/..*|\/$)/,
];
let redirects = {
2022-02-06 03:58:42 +01:00
"searx": {
2022-02-20 20:47:13 +01:00
"normal": [],
"tor": [],
"i2p": []
2022-02-01 19:37:43 +01:00
},
2022-02-06 03:58:42 +01:00
"whoogle": {
2022-02-20 20:47:13 +01:00
"normal": [],
2022-02-20 17:00:12 +01:00
"tor": []
2022-02-04 16:48:24 +01:00
},
2022-02-01 19:37:43 +01:00
};
const getRedirects = () => redirects;
2022-02-16 16:54:13 +01:00
const getCustomRedirects = () => {
return {
"searx": {
2022-02-20 20:47:13 +01:00
"normal": [...searxNormalRedirectsChecks, ...searxNormalCustomRedirects],
"tor": [...searxTorRedirectsChecks, ...searxTorCustomRedirects],
},
"whoogle": {
2022-02-20 20:47:13 +01:00
"normal": [...whoogleNormalRedirectsChecks, ...whoogleNormalCustomRedirects],
"normal": [...whoogleTorRedirectsChecks, ...whoogleTorCustomRedirects]
}
};
};
function setSearxRedirects(val) {
redirects.searx = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searchRedirects: redirects })
console.log("searxRedirects:", val)
2022-02-20 20:47:13 +01:00
for (const item of searxNormalRedirectsChecks) if (!redirects.searx.normal.includes(item)) {
var index = searxNormalRedirectsChecks.indexOf(item);
if (index !== -1) searxNormalRedirectsChecks.splice(index, 1);
}
2022-02-20 20:47:13 +01:00
setSearxNormalRedirectsChecks(searxNormalRedirectsChecks);
for (const item of searxTorRedirectsChecks) if (!redirects.searx.tor.includes(item)) {
var index = searxTorRedirectsChecks.indexOf(item);
if (index !== -1) searxTorRedirectsChecks.splice(index, 1);
}
setSearxTorRedirectsChecks(searxTorRedirectsChecks);
}
2022-02-16 04:41:24 +01:00
function setWhoogleRedirects(val) {
redirects.whoogle = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searchRedirects: redirects })
2022-02-16 04:41:24 +01:00
console.log("whoogleRedirects:", val)
2022-02-20 20:47:13 +01:00
for (const item of whoogleNormalRedirectsChecks) if (!redirects.whoogle.normal.includes(item)) {
var index = whoogleNormalRedirectsChecks.indexOf(item);
if (index !== -1) whoogleNormalRedirectsChecks.splice(index, 1);
}
setWhoogleNormalRedirectsChecks(whoogleNormalRedirectsChecks);
for (const item of whoogleTorRedirectsChecks) if (!redirects.whoogle.tor.includes(item)) {
var index = whoogleTorRedirectsChecks.indexOf(item);
if (index !== -1) whoogleTorRedirectsChecks.splice(index, 1);
2022-02-16 16:54:13 +01:00
}
2022-02-20 20:47:13 +01:00
setWhoogleTorRedirectsChecks(whoogleTorRedirectsChecks);
2022-02-16 04:41:24 +01:00
}
2022-02-20 20:47:13 +01:00
let whoogleNormalRedirectsChecks;
const getWhoogleNormalRedirectsChecks = () => whoogleNormalRedirectsChecks;
function setWhoogleNormalRedirectsChecks(val) {
whoogleNormalRedirectsChecks = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ whoogleNormalRedirectsChecks })
2022-02-20 20:47:13 +01:00
console.log("whoogleNormalRedirectsChecks: ", val)
}
2022-02-20 20:47:13 +01:00
let whoogleTorRedirectsChecks;
const getWhoogleTorRedirectsChecks = () => whoogleTorRedirectsChecks;
function setWhoogleTorRedirectsChecks(val) {
whoogleTorRedirectsChecks = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ whoogleTorRedirectsChecks })
2022-02-20 20:47:13 +01:00
console.log("whoogleTorRedirectsChecks: ", val)
}
2022-02-20 20:47:13 +01:00
let whoogleNormalCustomRedirects = [];
const getWhoogleNormalCustomRedirects = () => whoogleNormalCustomRedirects;
function setWhoogleNormalCustomRedirects(val) {
whoogleNormalCustomRedirects = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ whoogleNormalCustomRedirects })
2022-02-20 20:47:13 +01:00
console.log("whoogleNormalCustomRedirects: ", val)
}
2022-02-20 20:47:13 +01:00
let whoogleTorCustomRedirects = [];
const getWhoogleTorCustomRedirects = () => whoogleTorCustomRedirects;
function setWhoogleTorCustomRedirects(val) {
whoogleTorCustomRedirects = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ whoogleTorCustomRedirects })
2022-02-20 20:47:13 +01:00
console.log("whoogleTorCustomRedirects: ", val)
}
let searxNormalRedirectsChecks;
const getSearxNormalRedirectsChecks = () => searxNormalRedirectsChecks;
function setSearxNormalRedirectsChecks(val) {
searxNormalRedirectsChecks = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searxNormalRedirectsChecks })
2022-02-20 20:47:13 +01:00
console.log("searxNormalRedirectsChecks: ", val)
}
let searxTorRedirectsChecks;
const getSearxTorRedirectsChecks = () => searxTorRedirectsChecks;
function setSearxTorRedirectsChecks(val) {
searxTorRedirectsChecks = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searxTorRedirectsChecks })
2022-02-20 20:47:13 +01:00
console.log("searxTorRedirectsChecks: ", val)
}
let searxNormalCustomRedirects = [];
const getSearxNormalCustomRedirects = () => searxNormalCustomRedirects;
function setSearxNormalCustomRedirects(val) {
searxNormalCustomRedirects = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searxNormalCustomRedirects })
2022-02-20 20:47:13 +01:00
console.log("searxNormalCustomRedirects: ", val)
}
let searxTorCustomRedirects = [];
const getSearxTorCustomRedirects = () => searxTorCustomRedirects;
function setSearxTorCustomRedirects(val) {
searxTorCustomRedirects = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searxTorCustomRedirects })
2022-02-20 20:47:13 +01:00
console.log("searxTorCustomRedirects: ", val)
}
2022-02-16 04:41:24 +01:00
let disable;
const getDisable = () => disable;
function setDisable(val) {
disable = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ disableSearch: disable })
2022-02-16 04:41:24 +01:00
console.log("disableSearch: ", disable)
2022-02-01 19:37:43 +01:00
}
2022-02-16 04:41:24 +01:00
let frontend;
const getFrontend = () => frontend;
function setFrontend(val) {
frontend = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searchFrontend: frontend })
2022-02-16 04:41:24 +01:00
console.log("searchFrontend: ", frontend)
2022-02-01 19:37:43 +01:00
};
2022-02-20 20:47:13 +01:00
let protocol;
2022-02-23 05:19:56 +01:00
const getProtocol = () => protocol;
2022-02-20 20:47:13 +01:00
function setProtocol(val) {
protocol = val;
2022-02-21 19:38:52 +01:00
browser.storage.local.set({ searchProtocol: val })
2022-02-20 20:47:13 +01:00
console.log("searchProtocol: ", val)
}
2022-02-21 21:44:42 +01:00
function isSearch(url) {
2022-02-16 04:41:24 +01:00
if (disable) return false;
return targets.some((rx) => rx.test(url.href));
}
2022-02-01 19:37:43 +01:00
2022-02-16 04:41:24 +01:00
function redirect(url) {
console.log(url.href);
let randomInstance;
2022-02-01 19:37:43 +01:00
let path;
2022-02-16 04:41:24 +01:00
if (frontend == 'searx') {
2022-02-20 20:47:13 +01:00
let instancesList;
if (protocol == 'normal') instancesList = [...searxNormalRedirectsChecks, ...searxNormalCustomRedirects];
else if (protocol == 'tor') instancesList = [...searxTorRedirectsChecks, ...searxTorCustomRedirects];
if (instancesList.length === 0) return null;
randomInstance = commonHelper.getRandomInstance(instancesList)
2022-02-16 04:41:24 +01:00
path = "/";
2022-02-01 19:37:43 +01:00
}
2022-02-16 04:41:24 +01:00
if (frontend == 'whoogle') {
2022-02-20 20:47:13 +01:00
let instancesList
if (protocol == 'normal') instancesList = [...whoogleNormalRedirectsChecks, ...whoogleNormalCustomRedirects];
if (protocol == 'tor') instancesList = [...whoogleTorRedirectsChecks, ...whoogleTorCustomRedirects];
if (instancesList.length === 0) return null;
randomInstance = commonHelper.getRandomInstance(instancesList)
2022-02-16 04:41:24 +01:00
path = "/search";
2022-02-01 19:37:43 +01:00
}
let searchQuery = "";
2022-02-09 22:04:00 +01:00
url.search.slice(1).split("&").forEach((input) => {
2022-02-01 19:37:43 +01:00
if (input.startsWith("q=")) searchQuery = input;
});
2022-02-16 04:41:24 +01:00
return `${randomInstance}${path}?${searchQuery}`;
2022-02-01 19:37:43 +01:00
}
function changeInstance(url) {
let protocolHost = `${url.protocol}//${url.host}`;
let searchList = [
...redirects.searx.normal,
...redirects.searx.tor,
...searxNormalCustomRedirects,
...searxTorCustomRedirects,
...redirects.whoogle.normal,
...redirects.whoogle.tor,
...whoogleNormalCustomRedirects,
...whoogleTorCustomRedirects,
]
if (!searchList.includes(protocolHost)) return null;
let instancesList;
if (frontend == 'searx') {
if (protocol == 'normal') instancesList = [...searxNormalRedirectsChecks, ...searxNormalCustomRedirects];
else if (protocol == 'tor') instancesList = [...searxTorRedirectsChecks, ...searxTorCustomRedirects];
}
else if (frontend == 'whoogle') {
if (protocol == 'normal') instancesList = [...whoogleNormalRedirectsChecks, ...whoogleNormalCustomRedirects];
else if (protocol == 'tor') instancesList = [...whoogleTorRedirectsChecks, ...whoogleTorCustomRedirects];
}
console.log("instancesList", instancesList);
let index = instancesList.indexOf(protocolHost);
if (index > -1) instancesList.splice(index, 1);
if (instancesList.length === 0) return null;
let randomInstance = commonHelper.getRandomInstance(instancesList);
2022-03-03 17:38:37 +01:00
return `${randomInstance}${url.pathname}${url.search}`;
}
async function init() {
2022-02-12 20:28:36 +01:00
return new Promise((resolve) => {
2022-02-20 20:47:13 +01:00
fetch('/instances/data.json').then(response => response.text()).then(data => {
let dataJson = JSON.parse(data);
2022-02-21 19:38:52 +01:00
browser.storage.local.get(
2022-02-20 20:47:13 +01:00
[
"disableSearch",
"searchFrontend",
"searchRedirects",
"whoogleNormalRedirectsChecks",
"whoogleNormalCustomRedirects",
2022-02-20 20:47:13 +01:00
"whoogleTorRedirectsChecks",
"whoogleTorCustomRedirects",
"searxNormalRedirectsChecks",
"searxNormalCustomRedirects",
2022-02-20 20:47:13 +01:00
"searxTorRedirectsChecks",
"searxTorCustomRedirects",
"searchProtocol",
],
(result) => {
disable = result.disableSearch ?? false;
protocol = result.searchProtocol ?? 'normal';
frontend = result.searchFrontend ?? 'searx';
redirects.searx = dataJson.searx;
redirects.whoogle = dataJson.whoogle;
if (result.searchRedirects) redirects = result.searchRedirects;
whoogleNormalRedirectsChecks = result.whoogleNormalRedirectsChecks ?? [...redirects.whoogle.normal];
whoogleNormalCustomRedirects = result.whoogleNormalCustomRedirects ?? [];
whoogleTorRedirectsChecks = result.whoogleTorRedirectsChecks ?? [...redirects.whoogle.tor];
whoogleTorCustomRedirects = result.whoogleTorCustomRedirects ?? [];
searxNormalRedirectsChecks = result.searxNormalRedirectsChecks ?? [...redirects.searx.normal];
searxNormalCustomRedirects = result.searxNormalCustomRedirects ?? [];
searxTorRedirectsChecks = result.searxTorRedirectsChecks ?? [...redirects.searx.tor];
searxTorCustomRedirects = result.searxTorCustomRedirects ?? [];
resolve();
}
);
});
2022-02-12 20:28:36 +01:00
});
}
export default {
isSearch,
2022-02-16 04:41:24 +01:00
getDisable,
setDisable,
getRedirects,
getCustomRedirects,
setSearxRedirects,
setWhoogleRedirects,
2022-02-16 04:41:24 +01:00
getFrontend,
setFrontend,
2022-02-20 20:47:13 +01:00
getWhoogleNormalRedirectsChecks,
setWhoogleNormalRedirectsChecks,
getWhoogleNormalCustomRedirects,
setWhoogleNormalCustomRedirects,
getWhoogleTorRedirectsChecks,
setWhoogleTorRedirectsChecks,
getWhoogleTorCustomRedirects,
setWhoogleTorCustomRedirects,
2022-02-20 20:47:13 +01:00
getSearxNormalRedirectsChecks,
setSearxNormalRedirectsChecks,
getSearxNormalCustomRedirects,
setSearxNormalCustomRedirects,
2022-02-20 20:47:13 +01:00
getSearxTorRedirectsChecks,
setSearxTorRedirectsChecks,
getSearxTorCustomRedirects,
setSearxTorCustomRedirects,
2022-02-23 05:19:56 +01:00
getProtocol,
2022-02-20 20:47:13 +01:00
setProtocol,
2022-02-01 19:37:43 +01:00
redirect,
init,
changeInstance,
};