This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.js
79 lines (71 loc) · 2.6 KB
/
addon.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
/* NightServ - Released under the GPL-3.0 license | Copyright 2020 - 2022 Adrian Bit - abit.dev */
chrome.runtime.onInstalled.addListener((reason) => {
console.log("THANK YOU FOR CHOOSING NIGHTSERV MADE BY abit systems ^^");
chrome.storage.local.get(["nightServ_enabled"], (res) => {
if (isEmpty(res)) chrome.storage.local.set({ nightServ_enabled: true });
});
console.time("Refresh Data");
reloadThemesAndOptions().then((res) => {console.timeEnd("Refresh Data")
console.log(res);
});
});
async function reloadThemesAndOptions() {
return new Promise((resolve, reject) => {
chrome.storage.local.get("active", (res) => {
fetch("https://content.nightserv.cc/nightserv/2/themes.json")
.then((data) => data.json())
.then(json => {
chrome.storage.local.set({ "json": json });
if(json[res.active] == null) res.active = "dark";
fetch("https://content.nightserv.cc/nightserv/2/" + json[res.active].category + "/template.css")
.then(res => res.text())
.then((template) => {
fetch("https://content.nightserv.cc/nightserv/2/" + json[res.active].category + "/" + res.active + "/theme.css")
.then(res => res.text())
.then((data) => { chrome.storage.local.set({ "theme": template + data }); });
});
});
});
});
}
chrome.storage.local.set({ "iserv.de": false });
//TODO: Change listener to inject right layout and theme
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (!tab.url.startsWith("http")) return;
let url = extractDomain(tab.url);
if (changeInfo.status != "loading") return;
console.time("inject");
chrome.storage.local.get(
[url, "nightServ_enabled"],
(r) => {
if (r[url] == null) return;
if (r[url] && r["nightServ_enabled"]) {
chrome.storage.local.get(["theme"], (data) => {
let style = {
target: { "tabId": tabId },
css: data.theme
}
console.log(data);
chrome.scripting.insertCSS(style);
console.timeEnd("inject");
})
}
}
);
});
function extractDomain(url) {
return url.replace(
/^(?:https?:\/\/)?(?:[^\/]+\.)?([^.\/]+\.[^.\/]+).*$/,
"$1"
);
}
function isEmpty(r) {
for (var i in r) if (r.hasOwnProperty(i)) return !1;
return JSON.stringify(r) === JSON.stringify({});
}
function readFile(file, callback) {
let url = chrome.runtime.getURL(file);
fetch(url)
.then((response) => response.json())
.then(data => callback(data));
}