Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
109 lines (97 loc) · 3.18 KB
/
Copy pathbackground.js
File metadata and controls
109 lines (97 loc) · 3.18 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
/**
* STFU Background Script
* Monitors tabs and automatically mutes those matching configured URL prefixes
*/
// Initialize storage with default structure if needed
browser.runtime.onInstalled.addListener(async () => {
const data = await browser.storage.local.get("prefixes");
if (!data.prefixes) {
await browser.storage.local.set({ prefixes: [] });
}
// Check all existing tabs on install
await checkAllTabs();
});
/**
* Retrieves all enabled URL prefixes from storage
* @returns {Promise<string[]>} Array of enabled URL prefixes
*/
async function getEnabledPrefixes() {
const data = await browser.storage.local.get("prefixes");
const prefixes = data.prefixes || [];
return prefixes.filter((p) => p.enabled).map((p) => p.url);
}
/**
* Checks if a URL matches any of the enabled prefixes
* @param {string} url - The URL to check
* @param {string[]} prefixes - Array of URL prefixes
* @returns {boolean} True if URL matches any prefix
*/
function urlMatchesPrefix(url, prefixes) {
if (!url) return false;
return prefixes.some((prefix) => url.startsWith(prefix));
}
/**
* Mutes or unmutes a tab based on whether its URL matches enabled prefixes
* @param {number} tabId - The ID of the tab to process
* @param {string} url - The URL of the tab
*/
async function processTab(tabId, url) {
try {
const enabledPrefixes = await getEnabledPrefixes();
const shouldMute = urlMatchesPrefix(url, enabledPrefixes);
// Get current tab state
const tab = await browser.tabs.get(tabId);
// Only update if the muted state needs to change
if (tab.mutedInfo && tab.mutedInfo.muted !== shouldMute) {
await browser.tabs.update(tabId, { muted: shouldMute });
}
} catch (error) {
// Tab might have been closed, ignore errors
console.debug("Error processing tab:", error);
}
}
/**
* Checks all open tabs and applies muting rules
*/
async function checkAllTabs() {
try {
const tabs = await browser.tabs.query({});
const enabledPrefixes = await getEnabledPrefixes();
for (const tab of tabs) {
if (tab.url) {
const shouldMute = urlMatchesPrefix(tab.url, enabledPrefixes);
if (tab.mutedInfo && tab.mutedInfo.muted !== shouldMute) {
await browser.tabs.update(tab.id, { muted: shouldMute });
}
}
}
} catch (error) {
console.error("Error checking all tabs:", error);
}
}
// Listen for tab updates (URL changes, page loads, etc.)
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// Only process when URL is available
if (changeInfo.url || changeInfo.status === "complete") {
processTab(tabId, tab.url);
}
});
// Listen for new tabs being created
browser.tabs.onCreated.addListener((tab) => {
if (tab.url) {
processTab(tab.id, tab.url);
}
});
// Listen for storage changes to immediately apply new rules
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "local" && changes.prefixes) {
// Prefixes changed, recheck all tabs
checkAllTabs();
}
});
// Handle messages from popup (for immediate refresh if needed)
browser.runtime.onMessage.addListener((message) => {
if (message.action === "refreshTabs") {
return checkAllTabs();
}
});