-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbackground.js
65 lines (58 loc) · 1.63 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
"use strict";
const nitterDefault = "https://nitter.net";
let instance;
let nitterDisabled;
window.browser = window.browser || window.chrome;
browser.storage.sync.get(["nitterDisabled", "instance"], (result) => {
nitterDisabled = result.nitterDisabled;
instance = result.instance || nitterDefault;
});
browser.storage.onChanged.addListener(function (changes) {
if ("instance" in changes) {
instance = changes.instance.newValue || nitterDefault;
}
if ("nitterDisabled" in changes) {
nitterDisabled = changes.nitterDisabled.newValue;
}
});
function redirectTwitter(url) {
if (nitterDisabled) {
return null;
}
if (url.host.split(".")[0] === "pbs") {
return `${instance}/pic/${encodeURIComponent(url.href)}`;
} else if (url.host.split(".")[0] === "video") {
return `${instance}/gif/${encodeURIComponent(url.href)}`;
} else if (url.pathname.includes("tweets")) {
return `${instance}${url.pathname.replace("/tweets", "")}${url.search}`;
} else {
return `${instance}${url.pathname}${url.search}`;
}
}
browser.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
let redirect;
redirect = { redirectUrl: redirectTwitter(url) };
if (redirect && redirect.redirectUrl) {
console.info(
"Redirecting",
`"${url.href}"`,
"=>",
`"${redirect.redirectUrl}"`
);
console.info("Details", details);
}
return redirect;
},
{
urls: [
"*://twitter.com/*",
"*://www.twitter.com/*",
"*://mobile.twitter.com/*",
"*://pbs.twimg.com/*",
"*://video.twimg.com/*",
],
},
["blocking"]
);