-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
470 lines (407 loc) · 14.7 KB
/
background.js
File metadata and controls
470 lines (407 loc) · 14.7 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
importScripts("remote_config.js");
const FAVORITES_KEY = "streamguard_favorites_v1";
const USER_DOMAINS_KEY = "streamguard_user_domains_v1";
const popupMonitoring = {
active: false,
tabId: null,
windowId: null,
hostname: "",
startedAt: 0,
events: []
};
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.get([FAVORITES_KEY, USER_DOMAINS_KEY], (result) => {
const updates = {};
if (!Array.isArray(result[FAVORITES_KEY])) updates[FAVORITES_KEY] = [];
if (!Array.isArray(result[USER_DOMAINS_KEY])) updates[USER_DOMAINS_KEY] = [];
if (Object.keys(updates).length) chrome.storage.sync.set(updates);
});
});
async function startMonitoringForTab(tabId, hostname, reason = "Developer overlay opened") {
if (!tabId) {
return { ok: false, message: "No active tab to monitor." };
}
popupMonitoring.active = true;
popupMonitoring.tabId = Number(tabId);
popupMonitoring.windowId = null;
popupMonitoring.hostname = String(hostname || "");
popupMonitoring.startedAt = Date.now();
popupMonitoring.events = [];
try {
await chrome.tabs.sendMessage(tabId, {
type: "streamguard:setLiveMonitoring",
enabled: true,
startedAt: popupMonitoring.startedAt
});
} catch (e) {
}
addMonitoringEvent({
kind: "session",
title: reason,
detail: hostname || "Active tab",
tabId
});
return { ok: true, monitoring: getMonitoringState() };
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
(async () => {
try {
if (msg.type === "streamguard:getPopupData") {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const tabUrl = tab?.url || "";
const hostname = tabUrl ? new URL(tabUrl).hostname : "";
const remoteRows = await streamguardFetchRemote(false);
const syncData = await chrome.storage.sync.get([FAVORITES_KEY, USER_DOMAINS_KEY]);
const favorites = syncData[FAVORITES_KEY] || [];
const userDomains = syncData[USER_DOMAINS_KEY] || [];
const combinedRows = streamguardBuildCombinedRows(remoteRows, userDomains);
const groups = streamguardGroupRows(combinedRows).map((g) => ({
...g,
slug: streamguardSlugFromGroup(g.groupName),
moduleInfo: streamguardGetModuleInfo(g.groupName),
isFavorite: favorites.includes(g.groupName)
}));
const current = streamguardFindByDomain(combinedRows, hostname);
sendResponse({
ok: true,
apiUrl: STREAMGUARD_API_URL,
githubUrl: STREAMGUARD_GITHUB_URL,
currentTab: { url: tabUrl, hostname, id: tab?.id || null },
currentMatch: current,
groups,
favorites,
userDomains,
monitoring: getMonitoringState()
});
return;
}
if (msg.type === "streamguard:startPopupMonitoring") {
const tabId = Number(msg.tabId || 0);
const hostname = String(msg.hostname || "");
const res = await startMonitoringForTab(tabId, hostname, "Developer overlay monitoring started");
sendResponse(res);
return;
}
if (msg.type === "streamguard:stopPopupMonitoring") {
await stopPopupMonitoring();
sendResponse({ ok: true });
return;
}
if (msg.type === "streamguard:devOverlayState") {
const tabId = sender?.tab?.id ? Number(sender.tab.id) : 0;
const hostname = String(msg.hostname || sender?.tab?.url || "");
if (msg.open) {
const res = await startMonitoringForTab(tabId, hostname, "Developer overlay monitoring started");
sendResponse(res);
return;
}
if (popupMonitoring.active && popupMonitoring.tabId === tabId) {
await stopPopupMonitoring();
}
sendResponse({ ok: true, monitoring: getMonitoringState() });
return;
}
if (msg.type === "streamguard:monitorEvent") {
if (popupMonitoring.active && sender?.tab?.id === popupMonitoring.tabId) {
addMonitoringEvent({
...msg.event,
tabId: sender.tab.id
});
}
sendResponse({ ok: true });
return;
}
if (msg.type === "streamguard:toggleFavorite") {
const groupName = String(msg.groupName || "");
const data = await chrome.storage.sync.get([FAVORITES_KEY]);
let favorites = data[FAVORITES_KEY] || [];
if (favorites.includes(groupName)) {
favorites = favorites.filter((x) => x !== groupName);
} else {
favorites.push(groupName);
}
await chrome.storage.sync.set({ [FAVORITES_KEY]: favorites });
sendResponse({ ok: true, favorites });
return;
}
if (msg.type === "streamguard:addLocalDomain") {
const domain = streamguardNormalizeDomain(msg.domain || "");
const group_name = String(msg.group_name || "").trim() || streamguardGuessGroupNameFromDomain(domain);
const payload = {
group_name,
domain,
status: String(msg.status || "suggested").trim().toLowerCase(),
submitted_at: new Date().toISOString().slice(0, 10),
submitted_note: String(msg.submitted_note || "").trim(),
review_note: "",
rejection_reason: "",
favorite_score: 0,
delivery_status: "local_only",
submitted_remote_at: ""
};
if (!domain) {
sendResponse({ ok: false, message: "A domain is required." });
return;
}
const remoteRows = await streamguardFetchRemote(false);
const data = await chrome.storage.sync.get([USER_DOMAINS_KEY]);
const rows = data[USER_DOMAINS_KEY] || [];
const combinedRows = streamguardBuildCombinedRows(remoteRows, rows);
const duplicate = streamguardFindByDomain(combinedRows, domain);
if (duplicate) {
const duplicateSource = duplicate.source === "local" ? "local suggestions" : "remote database";
sendResponse({
ok: false,
message: `Domain already exists in ${duplicateSource}.`,
duplicate
});
return;
}
const submitResult = await submitSuggestionToRemote(payload);
if (!submitResult.ok) {
sendResponse({
ok: false,
message: submitResult.message || "The suggestion could not be sent to the remote database.",
remoteError: true,
detail: submitResult.detail || ""
});
return;
}
payload.delivery_status = submitResult.delivery_status || "submitted";
payload.submitted_remote_at = new Date().toISOString().slice(0, 10);
rows.push(payload);
await chrome.storage.sync.set({ [USER_DOMAINS_KEY]: rows });
await streamguardClearRemoteCache();
const freshRows = await streamguardFetchRemote(true);
sendResponse({
ok: true,
rows,
added: { ...payload, source: "local" },
submitResult,
remoteCount: Array.isArray(freshRows) ? freshRows.length : 0
});
return;
}
if (msg.type === "streamguard:removeLocalDomain") {
const domain = streamguardNormalizeDomain(msg.domain || "");
const data = await chrome.storage.sync.get([USER_DOMAINS_KEY]);
const rows = (data[USER_DOMAINS_KEY] || []).filter(
(r) => streamguardNormalizeDomain(r.domain) !== domain
);
await chrome.storage.sync.set({ [USER_DOMAINS_KEY]: rows });
sendResponse({ ok: true, rows });
return;
}
if (msg.type === "streamguard:injectModulesForCurrentTab") {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab?.id || !tab?.url) {
sendResponse({ ok: false, message: "No active tab." });
return;
}
const remoteRows = await streamguardFetchRemote(false);
const syncData = await chrome.storage.sync.get([USER_DOMAINS_KEY]);
const combinedRows = streamguardBuildCombinedRows(remoteRows, syncData[USER_DOMAINS_KEY] || []);
const match = streamguardFindByDomain(combinedRows, new URL(tab.url).hostname);
if (!match) {
sendResponse({ ok: false, message: "Current domain not found in remote or local database." });
return;
}
if (String(match.source || "remote").toLowerCase() !== "remote" || String(match.status || "").toLowerCase() !== "supported") {
sendResponse({
ok: false,
message: "Only remote supported sites can run modules. Local suggestions are stored for developer analysis first.",
match
});
return;
}
const slug = streamguardSlugFromGroup(match.group_name);
const moduleInfo = streamguardGetModuleInfo(slug);
if (!moduleInfo.available) {
sendResponse({
ok: false,
message: "This site is marked as supported in the remote database, but your current StreamGuard version does not include its module yet. Update from GitHub first.",
updateRequired: true,
match,
slug
});
return;
}
const filesToTry = [
`modules/${slug}/adguard.js`,
`modules/${slug}/fillbrowserscreen.js`
];
const results = [];
for (const file of filesToTry) {
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: [file]
});
results.push({ file, ok: true });
} catch (e) {
results.push({ file, ok: false, error: String(e) });
}
}
sendResponse({ ok: true, slug, match, results });
return;
}
if (msg.type === "streamguard:refreshRemoteCache") {
const rows = await streamguardFetchRemote(true);
sendResponse({ ok: true, count: Array.isArray(rows) ? rows.length : 0 });
return;
}
sendResponse({ ok: false, message: "Unknown message type." });
} catch (e) {
sendResponse({ ok: false, message: String(e) });
}
})();
return true;
});
chrome.tabs.onCreated.addListener((tab) => {
if (!popupMonitoring.active) return;
if (!popupMonitoring.tabId) return;
if (tab.openerTabId !== popupMonitoring.tabId) return;
addMonitoringEvent({
kind: "popup_tab",
title: "New popup/tab opened",
detail: tab.pendingUrl || tab.url || "(pending URL)",
tabId: tab.id || null
});
});
chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
if (popupMonitoring.active && (tabId === popupMonitoring.tabId || tab.openerTabId === popupMonitoring.tabId)) {
if (info.url) {
addMonitoringEvent({
kind: tabId === popupMonitoring.tabId ? "navigation" : "popup_navigation",
title: tabId === popupMonitoring.tabId ? "Monitored tab navigated" : "Popup/tab navigated",
detail: info.url,
tabId
});
}
if (info.status === "complete" && tab.url && tabId === popupMonitoring.tabId) {
try {
await chrome.tabs.sendMessage(tabId, {
type: "streamguard:setLiveMonitoring",
enabled: true,
startedAt: popupMonitoring.startedAt
});
} catch (e) {
}
}
}
if (info.status !== "complete" || !tab.url) return;
try {
const remoteRows = await streamguardFetchRemote(false);
const syncData = await chrome.storage.sync.get([USER_DOMAINS_KEY]);
const combinedRows = streamguardBuildCombinedRows(remoteRows, syncData[USER_DOMAINS_KEY] || []);
const match = streamguardFindByDomain(combinedRows, new URL(tab.url).hostname);
if (!match) return;
if (String(match.status || "").toLowerCase() !== "supported") return;
const slug = streamguardSlugFromGroup(match.group_name);
const moduleInfo = streamguardGetModuleInfo(slug);
if (!moduleInfo.available) return;
for (const file of [`modules/${slug}/adguard.js`, `modules/${slug}/fillbrowserscreen.js`]) {
try {
await chrome.scripting.executeScript({
target: { tabId },
files: [file]
});
} catch (e) {
}
}
} catch (e) {
}
});
chrome.tabs.onRemoved.addListener(async (tabId) => {
if (popupMonitoring.active && popupMonitoring.tabId === tabId) {
await stopPopupMonitoring();
}
});
async function submitSuggestionToRemote(payload) {
const body = {
group_name: String(payload.group_name || "").trim(),
domain: streamguardNormalizeDomain(payload.domain || ""),
submitted_note: String(payload.submitted_note || "").trim()
};
try {
const res = await fetch(STREAMGUARD_API_URL, {
method: "POST",
cache: "no-store",
headers: {
"Content-Type": "text/plain;charset=utf-8"
},
body: JSON.stringify(body),
redirect: "follow"
});
const rawText = await res.text();
let json = {};
try {
json = rawText ? JSON.parse(rawText) : {};
} catch (e) {
json = { ok: false, error: `Invalid JSON response: ${rawText.slice(0, 180)}` };
}
if (!res.ok) {
return {
ok: false,
message: json.error || `Remote submit failed with status ${res.status}.`,
detail: rawText.slice(0, 300)
};
}
if (json && json.ok === false) {
return {
ok: false,
message: json.error || json.message || "Remote submit was rejected.",
detail: rawText.slice(0, 300)
};
}
return {
ok: true,
message: json.message || "Suggestion submitted to remote database.",
delivery_status: json.delivery_status || "submitted",
remote: json
};
} catch (error) {
return {
ok: false,
message: "Could not reach the Apps Script web app.",
detail: String(error)
};
}
}
function addMonitoringEvent(event) {
popupMonitoring.events.unshift({
ts: Date.now(),
kind: event.kind || "event",
title: event.title || "Monitoring event",
detail: event.detail || "",
tabId: event.tabId || null
});
popupMonitoring.events = popupMonitoring.events.slice(0, 25);
}
function getMonitoringState() {
return {
active: popupMonitoring.active,
tabId: popupMonitoring.tabId,
hostname: popupMonitoring.hostname,
startedAt: popupMonitoring.startedAt,
events: popupMonitoring.events.slice(0, 25)
};
}
async function stopPopupMonitoring() {
const oldTabId = popupMonitoring.tabId;
popupMonitoring.active = false;
popupMonitoring.tabId = null;
popupMonitoring.windowId = null;
popupMonitoring.hostname = "";
popupMonitoring.startedAt = 0;
popupMonitoring.events = [];
if (oldTabId) {
try {
await chrome.tabs.sendMessage(oldTabId, {
type: "streamguard:setLiveMonitoring",
enabled: false
});
} catch (e) {
}
}
}