-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
228 lines (198 loc) · 10.4 KB
/
popup.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
// Copyright (C) 2022 s4my <[email protected]>
// See end of file for extended copyright information.
(function () {
"use strict";
const settingsBtn = document.getElementById("settings");
const updateBtn = document.getElementById("update-btn");
const title = document.getElementById("title_txt");
const nostreamDiv = document.getElementById("nostream");
const streamsDiv = document.getElementById("streams");
function numberWithCommas(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function updateBadge(counter) {
chrome.browserAction.setBadgeBackgroundColor({color: "#6a75f2"});
chrome.browserAction.setBadgeText({"text": counter});
}
function updateUI() {
settingsBtn.textContent = chrome.i18n.getMessage("settings_btn");
updateBtn.title = chrome.i18n.getMessage("update_btn");
title.textContent = chrome.i18n.getMessage("title");
chrome.storage.local.get(["liveChannels", "loggedin"], (storage) => {
if (!storage.loggedin) {
updateBadge("");
updateBtn.style.display = "none";
nostreamDiv.childNodes[0].textContent = chrome.i18n.getMessage("nostream_loggedout_01");
nostreamDiv.childNodes[3].textContent = chrome.i18n.getMessage("nostream_loggedout_02");
nostreamDiv.childNodes[4].textContent = chrome.i18n.getMessage("nostream_loggedout_03");
nostreamDiv.childNodes[5].textContent = chrome.i18n.getMessage("nostream_loggedout_04");
streamsDiv.innerHTML = "";
return;
} else {
updateBtn.style.display = "block";
if (!storage.liveChannels || storage.liveChannels.length === 0) {
updateBadge("0");
nostreamDiv.textContent = chrome.i18n.getMessage("nostream_loggedin");
nostreamDiv.style.display = "block";
streamsDiv.innerHTML = "";
return;
} else nostreamDiv.style.display = "none";
}
streamsDiv.innerHTML = "";
updateBadge(storage.liveChannels.length.toString());
for (const channel of storage.liveChannels) {
const streamDiv = document.getElementById("stream-template").content
.cloneNode(true).firstElementChild;
streamDiv.setAttribute("title", channel.title);
const logoDiv = streamDiv.querySelector(".logo");
logoDiv.firstElementChild.setAttribute("src", channel.logo.replace("300x300", "70x70"));
const streamerDiv = streamDiv.querySelector(".streamer");
streamerDiv.textContent = channel.name;
const categoryDiv = streamDiv.querySelector(".category");
categoryDiv.textContent = channel.category;
const viewersDiv = streamDiv.querySelector(".viewers");
viewersDiv.children[1].textContent = numberWithCommas(channel.viewers);
streamsDiv.append(streamDiv);
}
});
}
// update the UI every time the popup is opened
updateUI();
chrome.storage.local.get(["settings"], (storage) => {
// by default the theme is set to Auto before the settings are even set.
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.body.classList.add("dark-theme");
} else {
document.body.classList.remove("dark-theme");
}
if (storage.settings !== undefined) {
if (storage.settings["theme"] === 1/*Light*/) {
document.body.classList.remove("dark-theme");
} else if (storage.settings["theme"] === 2/*Dark*/) {
document.body.classList.add("dark-theme");
}
}
});
// update the UI if the message "updateUI" is received from background.js
chrome.runtime.onMessage.addListener((request) => {
if (request.message === "updateUI") updateUI();
});
// update the UI according to the extension's `status` when fetching an update in the background.
chrome.storage.onChanged.addListener((storage) => {
if (storage.status) {
if (storage.status.newValue === "updating") {
nostreamDiv.textContent = chrome.i18n.getMessage("nostream_updating");
updateBtn.style.backgroundImage = "url('/icons/loading.gif')";
updateBtn.style.cursor = "unset";
updateBtn.style.pointerEvents = "none";
} else {
if (storage.loggedin) {
nostreamDiv.textContent = chrome.i18n.getMessage("nostream_loggedin");
} else {
updateBadge("");
updateBtn.style.display = "none";
nostreamDiv.innerHTML = `
This extension requires your public Twitch account information.<br/><br/>
You need to <a href="#" id="login">Log In</a> to give it authorization
to get the list of channels you follow.`;
nostreamDiv.childNodes[0].textContent = chrome.i18n.getMessage("nostream_loggedout_01");
nostreamDiv.childNodes[3].textContent = chrome.i18n.getMessage("nostream_loggedout_02");
nostreamDiv.childNodes[4].textContent = chrome.i18n.getMessage("nostream_loggedout_03");
nostreamDiv.childNodes[5].textContent = chrome.i18n.getMessage("nostream_loggedout_04");
}
updateBtn.style.backgroundImage = "";
updateBtn.style.cursor = "pointer";
updateBtn.style.pointerEvents = "";
}
}
});
// auto hide the scrollbar
let hideScrollbarStyle = document.createElement("style");
hideScrollbarStyle.id = "remove-scrollbar";
if (navigator.userAgent.indexOf("Chrome") > -1) {
hideScrollbarStyle.textContent = "#streams::-webkit-scrollbar{display:none !important}";
} else if (navigator.userAgent.indexOf("Firefox") > -1) {
hideScrollbarStyle.textContent = "#streams{scrollbar-width: none !important}";
}
document.documentElement.appendChild(hideScrollbarStyle);
document.documentElement.addEventListener("mouseover", () => {
// show the scrollbar
if (document.documentElement.contains(hideScrollbarStyle)) {
document.documentElement.removeChild(hideScrollbarStyle);
}
});
document.documentElement.addEventListener("mouseout", () => {
// hide the scrollbar
document.documentElement.appendChild(hideScrollbarStyle);
});
if (navigator.userAgent.indexOf("Win") > -1) {
document.getElementById("streams").style.scrollbarWidth = "thin";
}
window.addEventListener("click", (event) => {
if (event.target.id === "update-btn") {
chrome.storage.local.get(["status", "loggedin"], (storage) => {
if (storage.loggedin !== undefined && storage.loggedin) {
if(!storage.status || storage.status === "done") {
// tell background.js to fetch an update.
chrome.runtime.sendMessage({"message": "update"});
}
}
});
} else if (event.target.id === "settings" || event.target.id === "login") {
if (chrome.runtime.openOptionsPage) chrome.runtime.openOptionsPage();
else window.open(chrome.runtime.getURL("options.html"));
}
if (event.target.matches(".stream, .streamer, .logo, .logo>img, .viewers, .viewers>span, .category")) {
const name = event.target.closest(".stream").getElementsByClassName("streamer")[0]
.textContent.toLowerCase();
chrome.storage.local.get(["settings"], (storage) => {
if (storage.settings !== undefined) {
if (!storage.settings["popup"]) {
chrome.tabs.create({url: "https://www.twitch.tv/" + encodeURIComponent(name)});
} else {
if (navigator.userAgent.indexOf("Chrome") > -1) {
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 if (navigator.userAgent.indexOf("Firefox") > -1) {
chrome.windows.create({
url: "https://player.twitch.tv/?channel=" + encodeURIComponent(name) +
"&enableExtensions=true&muted=false&parent=twitch.tv&player=popout&volume=1",
focused: true,
state: "maximized",
type: "popup"
});
}
}
window.close();
}
});
}
});
}());
// 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/>.