-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
74 lines (66 loc) · 2.02 KB
/
background.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var swapList = [];
var KEY = "DOMAINSWAPKEY"
browser.webRequest.onBeforeRequest.addListener(swapDomain, {urls: ["<all_urls>"]}, ["blocking"]);
getSwapList();
function swapDomain(info) {
let lnkUrl = new webkitURL(info.url);
let item = swapIt(lnkUrl.host);
//console.log("swapDomain:swapIt(" + lnkUrl.host + ") = " + item);
if (item != null) {
browser.tabs.get(info.tabId, function (tab) {
if (info.type == "main_frame" & tab != undefined) {
const sourceDest = item.split(":");
browser.tabs.update(info.tabId, {url: info.url.replace(sourceDest[0], sourceDest[1])});
//console.log(sourceDest[0] +":" + sourceDest[1]);
return {cancel: true};
}
});
}
}
function swapIt(url) {
for (let i = 0; i < swapList.length; i++) {
const sourceDest = swapList[i].split(":");
if (url.includes(sourceDest[0])) {
return swapList[i];
}
}
return null;
}
function addSwap(item) {
swapList.push(item.toLowerCase());
swapList.sort();
let toSave = {};
toSave[KEY] = JSON.stringify(swapList);
browser.storage.local.set(toSave, function () {
console.log("Added")
});
}
function del(item, callback) {
swapList.splice(swapList.indexOf(item.toLowerCase()), 1);
swapList.sort();
let toSave = {};
toSave[KEY] = JSON.stringify(swapList);
browser.storage.local.set(toSave, callback);
}
function cleanlist() {
var tempList = []
for (var i = 0; i < swapList.length; i++) {
if (swapList[i].length > 2) {
tempList.push(swapList[i])
}
}
swapList = tempList;
}
function getSwapList() {
browser.storage.local.get(KEY,
function (item) {
if (Object.keys(item).length === 0) { //not found
console.log("Domain Swap List not found");
swapList = [];
} else {
swapList = JSON.parse(item[KEY]);
cleanlist();
}
}
);
}