-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
356 lines (308 loc) · 13.1 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (C) 2022 s4my <[email protected]>
// See end of file for extended copyright information.
const CLIENT_ID = "yhzcodpomkejkstupuqajj9leqg630";
let isFirstRun = true;
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL("options.html"));
}
}
});
async function validateToken() {
try {
const accessToken = await getStorageItem("access_token");
if (accessToken) {
fetch ("https://id.twitch.tv/oauth2/validate", {
headers: {"Authorization": `Bearer ${accessToken}`}
}).then(response => {
if (!response.ok) {
throw new Error("failed to verify Access Token validity (${response.status})");
}
return response.json();
}).then(response => {
if (response["expires_in"] === 0 && response["client_id"] !== CLIENT_ID) {
chrome.identity.launchWebAuthFlow({
url: `https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}`+
`&redirect_uri=${chrome.identity.getRedirectURL()}&response_type=token`+
"&scope=user:read:follows&force_verify=true",
interactive: true
}, (redirect_url) => {
if (chrome.runtime.lastError || redirect_url.includes("error")) {
throw new Error("failed to get Access Token: ("+chrome.runtime.lastError.message+")");
} else {
const access_token = redirect_url.split("#").pop().split("&")[0].split("=")[1];
chrome.storage.local.set({"access_token": access_token});
chrome.storage.local.set({"loggedin": true});
}
});
}
}).catch(error => {
console.error(error);
chrome.storage.local.set({"loggedin": false});
updateBadge("");
});
}
} catch (error) {
console.error(error);
}
}
function getStorageItem(item) {
return new Promise((resolve, reject) => {
chrome.storage.local.get([item], (storage) => {
if (storage[item] !== undefined) resolve(storage[item]);
else {
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError);
reject(new Error(`could not find "${item}" in storage.`));
}
});
});
}
async function GETRequest(URL) {
try {
const authToken = await getStorageItem("access_token");
const response = await fetch (
URL,
{
method: "GET",
headers: {
"Client-ID": CLIENT_ID,
"Authorization": `Bearer ${authToken}`,
"Cache-Control": 'no-cache'
}
}
);
if (response.status === 401) {
chrome.storage.local.set({"loggedin": false});
chrome.storage.local.set({"access_token": ""});
updateBadge("");
throw new Error("OAuth token is missing or expired.");
}
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return await response.json();
} catch (error) {
console.error(error);
return null;
}
}
async function updateLiveChannels() {
try {
const loginStatus = await getStorageItem("loggedin");
if (!loginStatus) throw new Error("failed to update, the user is not logged in.");
chrome.storage.local.set({"status": "updating"});
// get the list of all live streams
let liveChannels = [];
const settings = await getStorageItem("settings");
const URL = `https://api.twitch.tv/helix/streams/followed?first=100&user_id=${settings["userID"]}`;
const response = await GETRequest(URL);
if (!response) throw new Error("failed to fetch live channels.");
console.log(response);
if (response.data.length > 0) {
let user_ids = [];
for (const stream of response.data) {
const user_id = stream.user_id;
const category = (stream.game_name === "") ? "UNDEFINED":stream.game_name;
const viewers = stream.viewer_count;
const title = stream.title;
if (!user_id || !category) continue;
// sometimes the `user_name` is empty for fault of the twitch API
// (https://github.com/twitchdev/issues/issues/500) which causes false
// positive notifications.
let user_name = stream.user_name;
if (user_name === "") {
try {
const liveChannels = await getStorageItem("liveChannels");
for (const channel of liveChannels) {
if (channel.id === user_id) {
user_name = channel.name;
break;
}
}
} catch (error) {
continue;
}
}
if (user_name === "") continue;
user_ids.push(stream.user_id);
const data = {
"id": user_id,
"name": user_name,
"category": category,
"viewers": viewers,
"title": title,
// fall back profile picture
"logo": "https://static-cdn.jtvnw.net/user-default-pictures-uv/" +
"cdd517fe-def4-11e9-948e-784f43822e80-profile_image-70x70.png"
};
liveChannels.push(data);
}
// get profile pictures
const getProfilePics = await GETRequest(`https://api.twitch.tv/helix/users?id=${user_ids.join("&id=")}`);
if (!getProfilePics) throw new Error("failed to get profile pictures");
for (const channel of liveChannels) {
for (const user_info of getProfilePics.data) {
if (channel.id === user_info["id"]) {
channel["logo"] = user_info["profile_image_url"];
}
}
}
}
chrome.storage.local.set({"liveChannels": liveChannels});
updateBadge(liveChannels.length.toString());
console.log(liveChannels);
console.log("Last time updated: "+new Date().toUTCString());
// tell popup.js to update the UI
chrome.runtime.sendMessage({"message": "updateUI"});
chrome.storage.local.set({"status": "done"});
} catch (error) {
console.error(error);
chrome.storage.local.set({"status": "done"});
}
}
function updateBadge(liveChannelCounter) {
chrome.browserAction.setBadgeBackgroundColor({color: "#6a75f2"});
chrome.browserAction.setBadgeText({"text": liveChannelCounter});
}
async function showNotification(channel) {
let notificationID = null;
const name = channel.name;
const category = channel.category;
const logo = await fetch(channel.logo.replace("300x300", "70x70"))
.then(response => response.blob())
.then(blob => {
return new Promise((resolve) => {
let canvas = document.createElement("canvas");
canvas.width = 48;
canvas.height = 48;
let ctx = canvas.getContext("2d");
const img = new Image();
img.src = URL.createObjectURL(blob);
img.onload = () => {
ctx.save();
ctx.beginPath();
ctx.arc((canvas.width/2), (canvas.width/2), (canvas.width/2), 0,
Math.PI * 2, false);
ctx.clip();
ctx.drawImage(img, 0, 0, 48, 48);
ctx.restore();
resolve(canvas.toDataURL("image/png"));
};
});
})
.catch(error => {
console.error(error);
// fall back icon
return chrome.runtime.getURL("icons/icon-48.png");
});
let notificationOptions = null;
if (navigator.userAgent.indexOf("Chrome") > -1) {
notificationOptions = {
title: "Twitch Live Channels",
priority: 0,
type: "list",
message: "",
items: [{
title : name,
message : ` is Live streaming ${category}`
}],
iconUrl: logo,
buttons: [{title : "Open"}]
};
} else if (navigator.userAgent.indexOf("Firefox") > -1) {
notificationOptions = {
title: navigator.userAgent.indexOf("Win") > -1 ? `${name} just went LIVE`:"Twitch Live Channels",
priority: 0,
type: "basic",
message: navigator.userAgent.indexOf("Win") > -1 ?
`Streaming ${category}`:`<b>${name}</b> is Live streaming ${category}`,
iconUrl: logo
};
}
chrome.notifications.create("", notificationOptions, (ID) => notificationID = ID);
try {
const settings = await getStorageItem("settings");
if (navigator.userAgent.indexOf("Chrome") > -1) {
chrome.notifications.onButtonClicked.addListener((ID, btnID) => {
if (ID === notificationID) {
if (btnID === 0) {
if (settings["popup"]) {
const popupWidth = 900;
const popupHeight = 650;
chrome.windows.create({
url: "https://player.twitch.tv/?channel=" + encodeURIComponent(name) +
"&enableExtensions=true&muted=false&parent=twitch.tv&player=popout&volume=1",
width: popupWidth,
height: popupHeight,
left: parseInt((screen.width/2) - (popupWidth/2)),
top: parseInt((screen.height/2) - (popupHeight/2)),
focused: true,
type: "popup"
});
} else {
chrome.tabs.create({url: "https://www.twitch.tv/" + encodeURIComponent(name)});
}
chrome.notifications.clear(notificationID);
}
}
});
}
} catch (error) {
console.error(error);
}
}
updateBadge("");
// get list of all live channels every 2 min
updateLiveChannels();
setInterval(updateLiveChannels, 60*1000*2);
chrome.runtime.onMessage.addListener((request) => {
if (request.message === "update") {
// update when the updateBtn is clicked on the popup.html
updateLiveChannels();
} else if (request.message === "validate_token") {
// validate access token every 60 min
setInterval(validateToken, 60*1000*60);
}
});
chrome.storage.onChanged.addListener(async (storage) => {
if(storage.liveChannels !== undefined) {
// check for each channel if it's already in the storage, if not, show notification
// and update the badge.
for (const channelNew of storage.liveChannels.newValue) {
let notificationStatus = true;
if (storage.liveChannels.oldValue?.length > 0) {
for (const channelOld of storage.liveChannels.oldValue) {
if (channelNew.name === channelOld.name) {
notificationStatus = false;
break;
}
}
}
if (isFirstRun) notificationStatus = false;
const settings = await getStorageItem("settings");
if (settings["notifications"] && notificationStatus) {
showNotification(channelNew);
}
}
updateBadge(storage.liveChannels.newValue.length.toString());
isFirstRun = false;
}
});
// Twitch Live Channels helps you keep track of who is live out of the channels you follow
// on Twitch (https://www.twitch.tv/).
//
// Copyright (C) 2022 s4my <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.