-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackground.js
98 lines (85 loc) · 2.69 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* global chrome */
const selectedMirrorKey = 'selectedMirror';
const onOffKey = 'active';
// Stay active as a default
let active = true;
let selectedMirror;
function WikiMirror(name, mirrorType) {
this.name = name;
this.mirrorType = mirrorType;
}
// eslint-disable-next-line func-names
WikiMirror.prototype.getModifiedUrl = function (actualUrlParts, domainIndex, stubParts) {
let stub;
switch (this.mirrorType) {
case 0:
stubParts.splice(domainIndex, 2, this.name);
stub = stubParts.join('.');
actualUrlParts.splice(2, 1, stub);
return actualUrlParts.join('/');
// eslint-disable-next-line no-case-declarations
default:
if (stubParts[0] === 'www') {
stubParts.splice(domainIndex, 2, this.name);
stub = stubParts.join('.');
actualUrlParts.splice(2, 1, stub);
return actualUrlParts.join('/');
}
let newUrl = `https://www.${this.name}/${stubParts[0]}/`;
const lastPart = actualUrlParts[actualUrlParts.length - 1];
if (lastPart !== '') {
newUrl += `${lastPart}`;
}
return newUrl;
}
};
const allMirrors = [
new WikiMirror('0wikipedia.org', 0),
new WikiMirror('wikizero.com', 1),
new WikiMirror('wikiwand.com', 1),
new WikiMirror('wikipedi0.org', 0),
];
function setSelectedMirror(selectedMirrorName) {
[selectedMirror] = allMirrors.filter(mirror => mirror.name === selectedMirrorName);
}
function getAllMirrorsNames(){
return allMirrors.map((mirror) => mirror.name)
}
chrome.storage.local.get([selectedMirrorKey, onOffKey], (data) => {
let selectedMirrorName = data[selectedMirrorKey];
active = data[onOffKey]
if (!selectedMirrorName) {
selectedMirrorName = allMirrors[0].name;
const mirrorData = {};
mirrorData[selectedMirrorKey] = selectedMirrorName;
mirrorData[onOffKey] = true
active = true
chrome.storage.local.set(mirrorData);
}
setSelectedMirror(selectedMirrorName);
});
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.data == "refresh") {
chrome.storage.local.get([onOffKey], (data) => {
active = data[onOffKey]
})
}
});
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
if (!active)
return {}
const actualUrl = details.url;
const actualUrlParts = actualUrl.split('/');
const stub = actualUrlParts[2];
const stubParts = stub.split('.');
const domainIndex = stubParts.length - 2;
if (stubParts[domainIndex] === 'wikipedia') {
const newUrl = selectedMirror.getModifiedUrl(actualUrlParts, domainIndex, stubParts);
return { redirectUrl: newUrl };
}
return { redirectUrl: actualUrl };
},
{ urls: ['*://*.wikipedia.org/*'] },
['blocking'],
);