forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
120 lines (113 loc) · 3.51 KB
/
Copy pathbackground.js
File metadata and controls
120 lines (113 loc) · 3.51 KB
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
'use strict';
import Config from './config.js';
import { us } from './api.js';
const win = self ?? window;
win.US = Config;
let brws = (typeof browser=="undefined"?chrome:browser);
us.check(Config).then((c) => {
let messenger;
function connected(p) {
messenger = p;
messenger.postMessage({cfg: c.cachedLocalStorage});
// eslint-disable-next-line no-unused-vars
messenger.onMessage.addListener((r) => {
us.log("Background Script: received message from content script",r);
if(r.delete) {
c.local.handler.deleteProperty(r.delete);
};
if(r.save) {
us.log("Background Script: saving...",r.save);
if(r.params) {
c.local.handler.set(r.save,r.params);
us.log("Background Script: ",r.save,r.params);
} else {
c.local.handler.set(r.save,c.cachedLocalStorage[r.save]);
us.log("Background Script: ",c.cachedLocalStorage[r.save]);
};
};
});
}
brws.runtime.onConnect.addListener(connected);
/**
* [handleMessage description]
* @param request The message itself. This is a JSON-ifiable object.
* @param sender A runtime.MessageSender object representing the sender of the message.
* @param sendResponse A function to call, at most once, to send a response to the message. The function takes a single argument, which may be any JSON-ifiable object. This argument is passed back to the message sender.
*/
// eslint-disable-next-line no-unused-vars
function handleMessage(request, sender, sendResponse) {
us.log(sender);
if(!sender.url.includes("options.html")) {
return Promise.resolve({
value: c.cachedLocalStorage[request.name]
});
} else {
c.local.handler.set(request.name,request.value);
return Promise.resolve({
name: request.name,
value: request.value
});
}
}
brws.runtime.onMessage.addListener(handleMessage);
});
const countApi = "https://greasyfork.org/scripts/by-site.json",
adultAPI = "https://sleazyfork.org/scripts/by-site.json",
getCurrentTabUrl = async (callback) => {
try {
let queryInfo = {
active: true,
currentWindow: true,
};
brws.tabs.query(queryInfo, (tabs) => {
let tab = tabs[0],
url = tab.url;
console.assert(typeof url == "string", "tab.url should be a string");
callback(url);
});
} catch (e) {
us.err(e);
brws.browserAction.setBadgeText({
text: "err",
});
return callback(url);
}
},
getUrlHost = (url) => {
let a = us.create("a",null,{href: url});
let mainHost = psl.get(a.hostname) || a.hostname.split(".").splice(-2).join(".");
return mainHost;
},
changeBadge = (cData) => {
getCurrentTabUrl((url) => {
let host = getUrlHost(url),
count = cData[host];
count = count > 50 ? 50 : count;
sessionStorage.setItem("host", host);
(count) ? brws.browserAction.setBadgeText({
text: count.toString(),
}) : brws.browserAction.setBadgeText({
text: "",
});
});
},
countBadge = async (api) => {
await new Promise((reject) => {
try {
fetch(api).then((r) => {
r.json().then((data) => {
brws.tabs.onUpdated.addListener(() => {
changeBadge(data);
});
brws.tabs.onActivated.addListener(() => {
changeBadge(data);
});
});
});
} catch (error) {
reject(error);
}
})
};
countBadge(countApi);
countBadge(adultAPI);