-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontent-script.js
39 lines (34 loc) · 1.26 KB
/
content-script.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
"use strict";
const nitterDefault = "https://nitter.net";
let nitterDisabled;
let instance;
window.browser = window.browser || window.chrome;
function redirectTwitter(url) {
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.storage.sync.get(["nitterDisabled", "instance"], (result) => {
nitterDisabled = result.nitterDisabled;
instance = result.instance || nitterDefault;
navigator.serviceWorker.getRegistrations().then((registrations) => {
for (let registration of registrations) {
if (registration.scope === "https://twitter.com/") {
registration.unregister();
console.log("Unregistered Twitter SW", registration);
}
}
});
const url = new URL(window.location);
if (!nitterDisabled && url.host !== instance) {
const redirect = redirectTwitter(url);
console.info("Redirecting", `"${url.href}"`, "=>", `"${redirect}"`);
window.location = redirect;
}
});