-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
84 lines (68 loc) · 2.18 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
const tabs = chrome.tabs;
const runtime = chrome.runtime;
const chromeLocalStorage = chrome.storage.local;
console.log("TwitchPointsAutoCollector ==> Background script running!");
runtime.onInstalled.addListener(() => {
console.log("TwitchPointsAutoCollector ==> Extension installed!");
});
runtime.onUpdateAvailable.addListener(() => {
console.log("TwitchPointsAutoCollector ==> Extension updated!");
runtime.reload();
});
runtime.onMessage.addListener((message, sender, sendResponse) => {
const { channel, avatarStreamer, nameStreamer, url } = message;
if (channel === "pointsClaimed") {
const { tab } = sender;
chromeLocalStorage.get("twitchPointsAutoCollectorStats").then((storage) => {
const { twitchPointsAutoCollectorStats } = storage;
const pointsClaimed = `${
twitchPointsAutoCollectorStats &&
nameStreamer in twitchPointsAutoCollectorStats
? parseInt(twitchPointsAutoCollectorStats[nameStreamer].points) + 1
: 1
}`;
const newTwitchPointsAutoCollectorStats = {
twitchPointsAutoCollectorStats: {
...twitchPointsAutoCollectorStats,
[nameStreamer]: {
points: pointsClaimed,
avatarStreamer,
nameStreamer,
url,
},
},
};
chromeLocalStorage.set(newTwitchPointsAutoCollectorStats).then(() => {
setBadge(tab.id, pointsClaimed);
});
});
}
});
tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === "complete" && tab.url) {
tabs.sendMessage(tabId, {
channel: "urlChanged",
url: tab.url,
});
chromeLocalStorage.get("twitchPointsAutoCollectorStats").then((storage) => {
const { twitchPointsAutoCollectorStats } = storage;
let text = null;
if (twitchPointsAutoCollectorStats)
text =
Object.values(twitchPointsAutoCollectorStats).find(
(el) => el.url === tab.url
)?.points || null;
setBadge(tab.id, text);
});
}
});
function setBadge(tabId, text) {
chrome.action.setBadgeText({
tabId,
text,
});
chrome.action.setBadgeBackgroundColor({
color: "#a970ff",
tabId,
});
}