-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkify.js
48 lines (40 loc) · 1.5 KB
/
linkify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let local_storage_cache = getLocalStorageData();
async function getLocalStorageData() {
await browser.storage.sync.get(null)
.then(urlObjs => {
local_storage_cache = urlObjs;
});
}
function redirectGoLink(details) {
const golink = details.url.replace(/(.*)\:\/\/go\//, "");
if (!local_storage_cache[golink]) {
return;
}
return {redirectUrl: local_storage_cache[golink].url}
}
/**
* Extract the go link from the search link and redirect to the user's stored website if exists.
*/
function redirectSearchLink(details) {
let golink = details.url.replace(/(.*)\:\/\/(.*)=go%2F/, "")
// Global flag g is included to replace all "%2F" with "/"
golink = golink.replace(/%2F/g, "/");
// Remove trailing query string content of the search link
// Yahoo: &fr=opensearch, Ecosia: &addon=opensearch
golink = golink.replace(/\&fr=opensearch|\&addon=opensearch/, "")
if (!local_storage_cache[golink]) {
return;
}
return {redirectUrl: local_storage_cache[golink].url}
}
browser.storage.onChanged.addListener(() => getLocalStorageData());
browser.webRequest.onBeforeRequest.addListener(
redirectGoLink,
{ "urls": ["*://go/*"] },
["blocking"],
);
browser.webRequest.onBeforeRequest.addListener(
redirectSearchLink,
{ "urls": ["*://*.google.com/*=go%2F*", "*://*.yahoo.com/*=go%2F*", "*://*.bing.com/*=go%2F*", "*://*.duckduckgo.com/*=go%2F*", "*://*.ecosia.org/*=go%2F*", "*://*.baidu.com/*=go%2F*"] },
["blocking"],
);