-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
182 lines (150 loc) · 7.58 KB
/
Copy pathbackground.js
File metadata and controls
182 lines (150 loc) · 7.58 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
// MindTab background service worker
// In Chrome (service worker), importScripts loads the shared constant.
// In Firefox, manifest.json background.scripts lists defaults.js first, so it's already defined.
if (typeof DEFAULTS === 'undefined') importScripts('config/defaults.js');
// Community-maintained filter lists in standard ABP/uBlock cosmetic format.
// uBlock Origin: updated continuously by the community, hosted on GitHub.
// AdGuard: same format, broader coverage of social media annoyances.
const DEFAULT_FILTER_LISTS = [
'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/annoyances-social.txt',
'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/quick-fixes.txt',
'https://raw.githubusercontent.com/AdguardTeam/AdguardFilters/master/AnnoyancesFilter/sections/social-widget.txt'
];
// Domains to extract cosmetic selectors for.
const TARGET_DOMAINS = ['youtube.com', 'instagram.com', 'facebook.com'];
// ─── Filter list parser ───────────────────────────────────────────────────────
// Parses standard ABP/uBlock cosmetic filter lines (domain##selector).
// Skips network rules, procedural filters, and global (no-domain) rules.
function parseFilterList(text) {
const result = {};
for (const d of TARGET_DOMAINS) result[d] = new Set();
for (const raw of text.split('\n')) {
const line = raw.trim();
if (!line || line.startsWith('!') || line.startsWith('[') || line.startsWith('@@')) continue;
const sep = line.indexOf('##');
if (sep === -1) continue;
const domainsPart = line.substring(0, sep);
if (!domainsPart) continue; // global rule - skip to avoid over-blocking
const selector = line.substring(sep + 2);
if (!selector) continue;
// Procedural/extended filters like :has(), :matches-css() can't be used
// as plain querySelectorAll selectors - skip them.
if (selector.includes(':matches') || selector.includes(':upward(') ||
selector.includes(':is(') && selector.includes(':not(') ||
(selector.startsWith(':') && selector.includes('('))) continue;
const lineDomains = domainsPart.split(',').map(d => d.trim().toLowerCase());
for (const target of TARGET_DOMAINS) {
if (lineDomains.some(d => {
if (d.startsWith('~')) return false; // exclusion rule
return d === target || target.endsWith('.' + d);
})) {
result[target].add(selector);
}
}
}
return Object.fromEntries(
Object.entries(result).map(([k, v]) => [k, [...v]])
);
}
// ─── Fetcher ──────────────────────────────────────────────────────────────────
async function updateFilterLists() {
const { mindtab } = await chrome.storage.sync.get('mindtab');
const urls = mindtab?.filterListUrls ?? DEFAULT_FILTER_LISTS;
const merged = {};
for (const d of TARGET_DOMAINS) merged[d] = new Set();
let successCount = 0;
let lastError = null;
for (const url of urls) {
try {
const res = await fetch(url, { cache: 'no-store', signal: AbortSignal.timeout(10000) });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
const parsed = parseFilterList(text);
for (const [domain, selectors] of Object.entries(parsed)) {
selectors.forEach(s => merged[domain].add(s));
}
successCount++;
} catch (e) {
console.warn(`[MindTab] Filter list fetch failed (${url}):`, e.message);
lastError = e.message;
}
}
if (successCount > 0) {
const newTotal = [...Object.values(merged)].reduce((n, s) => n + s.size, 0);
// Integrity check: if selector count drops >30% vs cache, keep cached data.
// Guards against truncated fetches or a compromised filter list source.
const { mindtabExternalFilters: cached } = await chrome.storage.local.get('mindtabExternalFilters');
const cachedTotal = cached
? Object.values(cached).reduce((n, a) => n + a.length, 0)
: 0;
if (cachedTotal > 0 && newTotal < cachedTotal * 0.7) {
console.warn(`[MindTab] Selector count dropped unexpectedly (${cachedTotal} → ${newTotal}) - keeping cached selectors.`);
await chrome.storage.local.set({
mindtabFiltersStatus: `Warning: selector count dropped unexpectedly (${cachedTotal} → ${newTotal}) - keeping cached selectors`
});
return;
}
const totals = Object.fromEntries(
Object.entries(merged).map(([k, v]) => [k, v.size])
);
await chrome.storage.local.set({
mindtabExternalFilters: Object.fromEntries(
Object.entries(merged).map(([k, v]) => [k, [...v]])
),
mindtabFiltersUpdated: Date.now(),
mindtabFiltersStatus: `Updated - ${newTotal} selectors across ${Object.keys(totals).length} sites`
});
console.log('[MindTab] Filter lists updated:', totals);
} else {
await chrome.storage.local.set({
mindtabFiltersStatus: `All ${urls.length} sources failed. Last error: ${lastError}`
});
console.warn('[MindTab] All filter list sources failed - keeping cached selectors.');
}
}
// ─── Lifecycle ────────────────────────────────────────────────────────────────
chrome.runtime.onInstalled.addListener(async () => {
const { mindtab } = await chrome.storage.sync.get('mindtab');
if (!mindtab) {
await chrome.storage.sync.set({ mindtab: DEFAULTS });
}
// Fetch immediately on install, then set up daily alarm.
await updateFilterLists();
chrome.alarms.create('mindtab-filter-update', { periodInMinutes: 1440 });
});
// Wrap in an async IIFE so Chrome's service worker lifetime tracking sees
// the pending promise and keeps the worker alive until the fetch completes.
chrome.alarms.onAlarm.addListener(alarm => {
if (alarm.name === 'mindtab-filter-update') (async () => { await updateFilterLists(); })();
});
// ─── Badge counter ────────────────────────────────────────────────────────────
const tabCounts = {};
function updateBadge(tabId) {
const n = tabCounts[tabId] || 0;
const text = n === 0 ? '' : n > 999 ? '999+' : String(n);
chrome.action.setBadgeText({ text, tabId });
chrome.action.setBadgeBackgroundColor({ color: '#4A90E2', tabId });
}
chrome.tabs.onUpdated.addListener((tabId, info) => {
if (info.status === 'loading') {
tabCounts[tabId] = 0;
chrome.action.setBadgeText({ text: '', tabId });
}
});
chrome.tabs.onRemoved.addListener(tabId => { delete tabCounts[tabId]; });
// ─── Messages ─────────────────────────────────────────────────────────────────
// Allow the popup to trigger a manual update and read status.
chrome.runtime.onMessage.addListener((msg, sender, reply) => {
if (msg.type === 'UPDATE_FILTERS') {
updateFilterLists().then(() => reply({ ok: true })).catch(e => reply({ ok: false, error: e.message }));
return true;
}
if (msg.type === 'GET_FILTER_STATUS') {
chrome.storage.local.get(['mindtabFiltersUpdated', 'mindtabFiltersStatus']).then(reply);
return true;
}
if (msg.type === 'BADGE_COUNT' && sender.tab?.id) {
tabCounts[sender.tab.id] = (tabCounts[sender.tab.id] || 0) + msg.delta;
updateBadge(sender.tab.id);
}
});