This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
203 lines (182 loc) · 5.37 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const twitterUrls = ["*://twitter.com/*", "*://mobile.twitter.com/*"];
const quoraUrls = ["*://www.quora.com/*", "*://quora.com/*"];
const redditUrls = ["*://reddit.com/*", "*://www.reddit.com/*"];
const youtubeUrls = [
"*://youtube.com/*",
"*://m.youtube.com/*",
"*://www.youtube.com/*",
"*://youtu.be/*",
];
const mediumUrls = ["*://medium.com/*", "*://*.medium.com/*"];
const allInstances = {
nitter: [
"nitter.net",
"nitter.pussthecat.org",
"nitter.kavin.rocks",
"nitter.eu",
],
teddit: ["teddit.net", "teddit.ggc-project.de", "teddit.kavin.rocks"],
invidious: ["invidious.snopyta.org", "yewtu.be"],
scribe: ["scribe.rip", "scribe.nixnet.services", "scribe.citizen4.eu"],
quetre: ["quetre.iket.me", "quetre.odyssey346.dev"],
};
let allInstancesArray = [];
Object.values(allInstances).forEach((instances) => {
instances.forEach((instance) => allInstancesArray.push(`*://${instance}/*`));
});
let currentInstances = {
nitter: "nitter.net",
teddit: "teddit.net",
invidious: "yewtu.be",
scribe: "scribe.rip",
quetre: "quetre.iket.me",
disable: false,
disable_nitter: false,
disable_teddit: false,
disable_invidious: false,
disable_scribe: false,
disable_quetre: false,
};
function replaceUrl(url, regex, newDomain) {
return url.replace(regex, `$1://${newDomain}/$3`);
}
function getDomain(url) {
if (url) {
return url.replace(/https?:\/\/([^\/]*).*/, "$1");
} else {
return null;
}
}
function redirect(requestDetails) {
if (currentInstances.disable) {
return null;
} else {
const originalUrl = requestDetails.url;
const originalDomain = getDomain(originalUrl);
const twitterRegex = /(https?):\/\/(twitter.com|mobile.twitter.com)\/(.*)/;
const redditRegex = /(https?):\/\/(reddit.com|www.reddit.com)\/(.*)/;
const youtubeRegex =
/(https?):\/\/(youtube.com|m.youtube.com|www.youtube.com|youtu.be)\/(.*)/;
const mediumRegex = /https?:\/\/(?:.*\.)*(?<!link\.)medium\.com(\/.*)?$/;
const quoraRegex = /(https?):\/\/(quora.com|www.quora.com)\/(.*)/;
// Twitter -> Nitter
if (!currentInstances.disable_nitter && twitterRegex.test(originalUrl)) {
const newUrl = replaceUrl(
originalUrl,
twitterRegex,
currentInstances.nitter
);
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
// Reddit -> Teddit
if (!currentInstances.disable_teddit && redditRegex.test(originalUrl)) {
const newUrl = replaceUrl(
originalUrl,
redditRegex,
currentInstances.teddit
);
// newUrl = originalUrl.replace(redditRegex, `$1://${currentInstances.teddit}/$3`)
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
// YouTube -> Invidious
if (!currentInstances.disable_invidious && youtubeRegex.test(originalUrl)) {
const newUrl = replaceUrl(
originalUrl,
youtubeRegex,
currentInstances.invidious
);
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
// Medium → Scribe
if (!currentInstances.disable_scribe && mediumRegex.test(originalUrl)) {
const newUrl = originalUrl.replace(
mediumRegex,
`https://${currentInstances.scribe}$1`
);
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
// Quora → Quetre
if (!currentInstances.disable_quetre && quoraRegex.test(originalUrl)) {
const newUrl = replaceUrl(
originalUrl,
quoraRegex,
currentInstances.quetre
);
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
// Other Instance -> Current Instance
if (
allInstancesArray.findIndex((instance) =>
instance.includes(originalDomain)
) > -1 &&
!Object.values(currentInstances).includes(originalDomain)
) {
let instanceKey = "";
for (const [k, v] of Object.entries(allInstances)) {
if (v.includes(originalDomain)) {
instanceKey = k;
}
}
const newUrl = originalUrl.replace(
/(https?:\/\/)([^\/]*)(.*)/,
`$1${currentInstances[instanceKey]}$3`
);
console.log("New URL ", newUrl);
return { redirectUrl: newUrl };
}
}
}
function setToLocalStorage(object) {
browser.storage.local.set(object).then(
() => console.log("Saved to Local Storage"),
(err) => console.log(err)
);
}
function updateCurrentInstances() {
browser.storage.local.get().then(
(res) => {
if (Object.keys(res).length !== 0) {
console.log("Current", res);
currentInstances = res;
} else {
console.log("Local storage is empty", res);
}
},
(err) => console.log(err)
);
}
function handleMessage(message, sender, sendResponse) {
if (message.type === "bg_update_instances") {
setToLocalStorage(message.instancesSelected);
updateCurrentInstances();
sendResponse("Instances Updated");
}
if (message.type === "bg_get_instances") {
sendResponse({
allInstances,
currentInstances,
});
}
}
updateCurrentInstances();
browser.runtime.onMessage.addListener(handleMessage);
browser.webRequest.onBeforeRequest.addListener(
redirect,
{
urls: [
...twitterUrls,
...redditUrls,
...youtubeUrls,
...mediumUrls,
...quoraUrls,
...allInstancesArray,
],
types: ["main_frame"],
},
["blocking"]
);