-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
2268 lines (2020 loc) · 78.4 KB
/
Copy pathbackground.js
File metadata and controls
2268 lines (2020 loc) · 78.4 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BlockNSFW background service worker */
const browserAPI = typeof browser !== 'undefined' ? browser : chrome;
// Shared browser-safe hostname normalization helpers (RFC 3492 punycode
// decoder, IDN-aware variant helper). See shared/hostname.js.
try {
if (typeof self !== 'undefined' && typeof self.importScripts === 'function') {
self.importScripts('shared/hostname.js');
self.importScripts('shared/host-keywords.js');
self.importScripts('shared/version-compare.js');
self.importScripts('shared/validate-domain.js');
}
} catch (_) {
// shared/hostname.js or shared/host-keywords.js could not be loaded
// (e.g. test environment). The helpers are optional; ASCII-only checks
// still work via ADULT_HOST_KEYWORDS.
}
// --- AI Image Blocker: TF.js + NSFW.js-compatible runtime in the SW ----------
// The service worker owns the model so host-page CSP never applies to the
// classifier runtime.
let _aiModel = null;
let _aiModelPromise = null;
let _aiModelFailed = false;
let _aiRuntimeLoaded = false;
let _aiRuntimeLoadError = '';
let _aiModelLastError = '';
let _aiModelLastFailureAt = 0;
const AI_MODEL_RETRY_COOLDOWN_MS = 5000;
const AI_MODEL_INPUT_SIZE = 224;
function getAiModelErrorMessage(error) {
return String(error && error.message || error || 'unknown error');
}
function getAiModelRetryAfterMs(now = Date.now()) {
if (!_aiModelFailed || !_aiModelLastFailureAt) return 0;
const remaining = AI_MODEL_RETRY_COOLDOWN_MS - (now - _aiModelLastFailureAt);
return remaining > 0 ? remaining : 0;
}
function syncAiRuntimeStateFromGlobals() {
if (typeof self === 'undefined') return;
if (typeof self.tf !== 'undefined' && typeof self.nsfwjs !== 'undefined') {
_aiRuntimeLoaded = true;
_aiRuntimeLoadError = '';
}
}
function preloadAiRuntime() {
if (typeof self === 'undefined' || typeof self.importScripts !== 'function') {
return;
}
try {
self.importScripts('vendor/tfjs/tf.es2017.js', 'vendor/nsfwjs/nsfwjs.runtime.js');
syncAiRuntimeStateFromGlobals();
if (!_aiRuntimeLoaded) {
if (typeof self.tf === 'undefined') {
_aiRuntimeLoadError = 'failed to preload AI runtime: tfjs not available after preload';
} else if (typeof self.nsfwjs === 'undefined') {
_aiRuntimeLoadError = 'failed to preload AI runtime: nsfwjs not available after preload';
}
}
} catch (err) {
_aiRuntimeLoaded = false;
_aiRuntimeLoadError = `failed to import AI runtime: ${getAiModelErrorMessage(err)}`;
}
}
preloadAiRuntime();
async function ensureAiRuntimeLoaded() {
syncAiRuntimeStateFromGlobals();
if (_aiRuntimeLoaded) return;
if (_aiRuntimeLoadError) {
throw new Error(_aiRuntimeLoadError);
}
throw new Error('AI runtime was not preloaded');
}
async function loadAiModel(options = {}) {
const forceRetry = options && options.forceRetry === true;
if (_aiModel) return _aiModel;
if (_aiModelPromise) return _aiModelPromise;
const retryAfterMs = getAiModelRetryAfterMs();
if (_aiModelFailed && !forceRetry && retryAfterMs > 0) {
const suffix = _aiModelLastError ? `: ${_aiModelLastError}` : '';
throw new Error(`AI model cooling down after failure${suffix}`);
}
_aiModelPromise = (async () => {
try {
await ensureAiRuntimeLoaded();
const tfLike = self.tf || null;
if (tfLike && typeof tfLike.setBackend === 'function') {
let backendSet = false;
for (const backend of ['webgl', 'cpu']) {
try {
await tfLike.setBackend(backend);
backendSet = true;
break;
} catch (_) {}
}
if (!backendSet && typeof tfLike.ready === 'function') {
try { await tfLike.ready(); } catch (_) {}
}
}
if (tfLike && typeof tfLike.ready === 'function') {
try { await tfLike.ready(); } catch (_) {}
}
// Default MobileNetV2 is a layers model; nsfwjs.runtime.js (the CSP-safe
// build used here) only supports layers models, so load without options.
_aiModel = await self.nsfwjs.load(browserAPI.runtime.getURL('nsfwjs/'));
_aiModelFailed = false;
_aiModelLastError = '';
_aiModelLastFailureAt = 0;
console.log('[BlockNSFW] AI Image Blocker model loaded.');
return _aiModel;
} catch (err) {
_aiModel = null;
_aiModelFailed = true;
_aiModelLastError = getAiModelErrorMessage(err);
_aiModelLastFailureAt = Date.now();
console.warn('[BlockNSFW] Failed to load NSFW model in SW:',
_aiModelLastError);
throw new Error(_aiModelLastError);
} finally {
_aiModelPromise = null;
}
})();
return _aiModelPromise;
}
async function classifyImageBytes(blobOrArrayBuffer) {
const model = await loadAiModel();
let bitmap;
const bitmapOptions = {
resizeWidth: AI_MODEL_INPUT_SIZE,
resizeHeight: AI_MODEL_INPUT_SIZE,
resizeQuality: 'high'
};
if (blobOrArrayBuffer instanceof ArrayBuffer ||
ArrayBuffer.isView(blobOrArrayBuffer)) {
const blob = new Blob([blobOrArrayBuffer]);
try {
bitmap = await createImageBitmap(blob, bitmapOptions);
} catch (_) {
bitmap = await createImageBitmap(blob);
}
} else {
try {
bitmap = await createImageBitmap(blobOrArrayBuffer, bitmapOptions);
} catch (_) {
bitmap = await createImageBitmap(blobOrArrayBuffer);
}
}
const predictions = await model.classify(bitmap);
try { bitmap.close(); } catch (_) {}
const scores = {};
for (const p of predictions) scores[p.className] = p.probability;
return scores;
}
// ============================================
// OFFSCREEN DOCUMENT (fast path: WebGL on GPU)
// ============================================
// The MV3 service worker has no WebGL and is killed on idle, so running TF.js
// there means CPU inference + repeated model reloads (slow). Chrome 109+ offers
// an offscreen document: a persistent DOM context WITH a WebGL context. We run
// the model there and relay classify/ping requests to it. When chrome.offscreen
// is unavailable (older Chrome, Firefox) we fall back to the in-SW path below.
const OFFSCREEN_URL = 'offscreen.html';
let _offscreenCreating = null;
function offscreenAvailable() {
return typeof chrome !== 'undefined' && !!(chrome.offscreen && chrome.offscreen.createDocument);
}
async function hasOffscreenDocument() {
try {
if (chrome.runtime && typeof chrome.runtime.getContexts === 'function') {
const ctxs = await chrome.runtime.getContexts({ contextTypes: ['OFFSCREEN_DOCUMENT'] });
return Array.isArray(ctxs) && ctxs.length > 0;
}
} catch (_) {}
return false;
}
async function ensureOffscreenDocument() {
if (!offscreenAvailable()) return false;
if (await hasOffscreenDocument()) return true;
if (_offscreenCreating) { try { await _offscreenCreating; } catch (_) {} return true; }
_offscreenCreating = chrome.offscreen.createDocument({
url: OFFSCREEN_URL,
reasons: ['BLOBS'],
justification: 'Run the on-device NSFW image classifier (TensorFlow.js + WebGL) off the main thread for speed.'
});
try {
await _offscreenCreating;
} catch (err) {
// A concurrent create (race) is fine — the document now exists. Re-throw
// anything else.
if (!String(err && err.message || err).toLowerCase().includes('single offscreen')) {
_offscreenCreating = null;
throw err;
}
}
_offscreenCreating = null;
return true;
}
function sendToOffscreen(payload, timeoutMs) {
return new Promise((resolve, reject) => {
let settled = false;
const timer = setTimeout(() => {
if (!settled) { settled = true; reject(new Error('offscreen timeout')); }
}, timeoutMs || 20000);
browserAPI.runtime.sendMessage({ target: 'offscreen-ai', ...payload }, (res) => {
if (settled) return;
settled = true;
clearTimeout(timer);
const lastErr = browserAPI.runtime.lastError;
if (lastErr) return reject(new Error(lastErr.message || String(lastErr)));
resolve(res);
});
});
}
// Storage keys
const SETTINGS_KEY = 'pblocker_settings';
const BLOCKED_STATS_KEY = 'pblocker_stats';
const DAILY_STATS_KEY = 'pblocker_daily_stats';
const WHITELIST_KEY = 'pblocker_whitelist';
const AUDIT_BLOCKED_KEY = 'pblocker_audit_blocked';
const AUDIT_DISABLED_KEY = 'pblocker_audit_disabled';
const AUDIT_MAX_ENTRIES = 1000; // Maximum entries per audit log type
const AUDIT_RETENTION_DAYS = 30;
const STREAK_START_KEY = 'pblocker_streak_start';
const LONGEST_STREAK_KEY = 'pblocker_longest_streak';
const TOP_DOMAINS_KEY = 'pblocker_top_domains';
const DAILY_HISTORY_KEY = 'pblocker_daily_history';
// Default settings
const DEFAULT_SETTINGS = {
enabled: true,
useSmartBlocking: true,
customPatterns: [], // user patterns, wildcard supported e.g. *.example.com, example.com/path
trustedImageDomains: [], // domains where images should never be blocked
debugMode: false,
dnsFilterEnabled: false,
safeSearchEnabled: true,
facebookReelsEnabled: false,
instagramReelsEnabled: false,
aiImageBlocker: false, // Beta — opt-in (off on fresh install)
aiImageScanAllSites: true, // when AI image blocker is on, scan 1st-party too
aiTextBlocker: false, // Beta — opt-in (off on fresh install)
aiTextStrictness: 'balanced',
};
// Default trusted domains for images (gaming, social media, e-commerce platforms)
const DEFAULT_TRUSTED_IMAGE_DOMAINS = [
'steampowered.com',
'steamstatic.com',
'steamcommunity.com',
'store.steampowered.com',
'cdn.akamai.steamstatic.com',
'steamcdn-a.akamaihd.net',
'epicgames.com',
'unrealengine.com',
'gog.com',
'origin.com',
'battle.net',
'blizzard.com',
'ubisoft.com',
'ea.com',
'nintendo.com',
'playstation.com',
'xbox.com',
'microsoft.com',
'amazon.com',
'ebay.com',
'walmart.com',
'target.com',
'bestbuy.com',
'newegg.com',
'youtube.com',
'youtu.be',
'twitch.tv',
'discord.com',
'reddit.com',
'imgur.com',
'github.com',
'stackoverflow.com',
'wikipedia.org',
'wikimedia.org'
];
const DEFAULT_STATS = {
blockedCount: 0,
websiteBlockedCount: 0,
imageBlockedCount: 0,
aiImageBlockedCount: 0,
searchResultBlockedCount: 0,
lastBlocked: null,
lastWebsiteBlocked: null,
};
let defaultBlocklist = [];
let compiledPatterns = [];
let defaultBlocklistSet = new Set();
// Resolves once the blocklist + compiled patterns are loaded. shouldBlock()
// awaits this so the very first request after a cold service-worker wake-up
// cannot leak through while defaultBlocklistSet is still empty.
let resolveReady;
let initReady = new Promise((resolve) => { resolveReady = resolve; });
let isReady = false;
function markReady() {
if (!isReady) {
isReady = true;
resolveReady();
}
}
// Optimized trie-based domain matching with pre-compilation for maximum performance
class OptimizedDomainTrie {
constructor() {
this.root = Object.create(null); // Faster than Map for character keys
this.precompiled = null; // Pre-compiled lookup structure
this.precompiledVersion = 0;
this._domainCount = 0; // O(1) size tracker for hot-path gating
this.stats = {
searches: 0,
hits: 0,
precompiledSearches: 0,
precompiledHits: 0
};
}
// Insert a domain in reverse order for efficient matching
insert(domain) {
const reversed = domain.split('.').reverse().join('.');
let node = this.root;
for (const char of reversed) {
if (!node[char]) {
node[char] = Object.create(null);
}
node = node[char];
}
if (!node['*']) {
this._domainCount++;
}
node['*'] = true; // Mark as blocked domain
// Invalidate pre-compiled cache
this.precompiled = null;
this.precompiledVersion++;
}
// Batch insert multiple domains for better performance
batchInsert(domains) {
if (!Array.isArray(domains)) return;
for (const domain of domains) {
if (typeof domain === 'string' && domain.includes('.')) {
this.insert(domain);
}
}
}
// Check if domain or any parent domain is blocked
search(domain) {
this.stats.searches++;
// Try pre-compiled lookup first for maximum speed
if (this.precompiled) {
this.stats.precompiledSearches++;
const result = this.precompiled[domain];
if (result !== undefined) {
this.stats.precompiledHits++;
return result;
}
}
// Fallback to standard trie search
const reversed = domain.split('.').reverse().join('.');
let node = this.root;
for (const char of reversed) {
if (!node[char]) {
this.stats.hits++;
return false;
}
node = node[char];
// Check if current level is blocked
if (node['*']) {
this.stats.hits++;
return true;
}
}
this.stats.hits++;
return false;
}
// Pre-compile the trie for instant lookups
precompile() {
const lookup = Object.create(null);
const compileNode = (node, currentPath = '') => {
if (node['*']) {
// Add the reversed path (which is the actual domain) to lookup
const domain = currentPath.split('').reverse().join('');
lookup[domain] = true;
}
for (const char in node) {
if (char !== '*') {
compileNode(node[char], currentPath + char);
}
}
};
compileNode(this.root);
this.precompiled = lookup;
return this.precompiled;
}
// Auto-precompile when trie reaches certain size
autoPrecompile(minSize = 1000) {
if (!this.precompiled && this.estimateSize() >= minSize) {
return this.precompile();
}
return this.precompiled;
}
// Estimate memory usage of the trie
estimateSize() {
let count = 0;
const countNodes = (node) => {
for (const key in node) {
count++;
if (typeof node[key] === 'object' && node[key] !== null) {
countNodes(node[key]);
}
}
};
countNodes(this.root);
return count;
}
// O(1) size accessor used by the hot path. Without this getter, callers
// that probed `domainTrie.size` always saw `undefined` and silently bypassed
// the trie lookup branch entirely.
get size() {
return this._domainCount;
}
// Clear the trie
clear() {
this.root = Object.create(null);
this.precompiled = null;
this.precompiledVersion++;
this._domainCount = 0;
this.stats = {
searches: 0,
hits: 0,
precompiledSearches: 0,
precompiledHits: 0
};
}
// Get performance statistics
getStats() {
return {
...this.stats,
size: this.estimateSize(),
precompiled: !!this.precompiled,
precompiledVersion: this.precompiledVersion
};
}
}
// Initialize trie with blocklist
let domainTrie = new OptimizedDomainTrie();
// Multi-tenant CDN parent domains that must not be parent-domain blocked.
const SHARED_CDN_PARENT_DOMAINS = new Set([
'b-cdn.net', 'cloudfront.net', 'akamaized.net', 'akamaihd.net',
'azureedge.net', 'azurefd.net', 'cloudflare.net', 'fastly.net',
'fastlylb.net', 'cdn77.org', 'kxcdn.com', 'stackpathdns.com',
'edgecastcdn.net', 'imgix.net', 'scene7.com', 'amazonaws.com',
'digitaloceanspaces.com', 'r2.dev', 'netlify.app', 'vercel.app',
'pages.dev', 'herokuapp.com', 'github.io', 'imagedelivery.net',
'twimg.com', 'fbcdn.net', 'cdninstagram.com', 'gstatic.com',
'googleapis.com', 'ggpht.com',
]);
function isSharedCDNParent(domain) {
return SHARED_CDN_PARENT_DOMAINS.has(domain);
}
function filterSharedCDNParents(hosts) {
return hosts.filter(h => !isSharedCDNParent(h));
}
// Performance optimization: Pattern and URL caching
let patternCache = new Map(); // Cache for compiled regex patterns
let urlCheckCache = new Map(); // Cache for URL blocking decisions
let keywordCheckCache = new Map(); // Cache for hostname keyword checks
let preCompiledDomainPatterns = new Map(); // Pre-compiled domain patterns for instant matching
const MAX_CACHE_SIZE = 1000; // Limit cache size to prevent memory bloat
let cacheVersion = 0; // Version to invalidate caches when patterns change
// Remote blocklist configuration
// Hosted in the maintainer's codepurse/BlockNSFW repository under MIT
// (data/LICENSE). Provenance: see data/SOURCE_NOTES.txt.
const REMOTE_BLOCKLIST_URL = 'https://raw.githubusercontent.com/codepurse/BlockNSFW/refs/heads/main/data/HOSTS.txt';
const BLOCKLIST_CACHE_META_KEY = 'pblocker_blocklist_meta_v2';
const BLOCKLIST_CACHE_CHUNK_PREFIX = 'pblocker_blocklist_chunk_v2_';
const BLOCKLIST_CACHE_CHUNK_SIZE = 5000;
const BLOCKLIST_CACHE_TTL = 1000 * 60 * 60 * 12; // 12 hours
let blocklistMeta = null;
let remoteBlocklistPromise = null;
// Remote global whitelist (false-positive overrides managed via GitHub)
// Same maintainer-owned repository as the blocklist above.
const REMOTE_WHITELIST_URL = 'https://raw.githubusercontent.com/codepurse/BlockNSFW/refs/heads/main/data/WHITELIST.txt';
const REMOTE_WHITELIST_CACHE_KEY = 'pblocker_remote_whitelist_v1';
const REMOTE_WHITELIST_META_KEY = 'pblocker_remote_whitelist_meta_v1';
let remoteWhitelistSet = new Set();
let remoteWhitelistMeta = null;
let remoteWhitelistPromise = null;
// Update checker. A small version.json lives next to HOSTS.txt/WHITELIST.txt in
// the maintainer's repo; we fetch it, compare `latest` against the installed
// manifest version, and stash the verdict in storage so the popup/options page
// can show an "update available" banner. Self-hosted (not the store APIs) so a
// single code path works identically on Chrome and Firefox. Bump version.json
// on each published release.
const REMOTE_VERSION_URL = 'https://raw.githubusercontent.com/codepurse/BlockNSFW/refs/heads/main/data/version.json';
const UPDATE_INFO_KEY = 'pblocker_update_info';
const DEFAULT_UPDATE_URL = 'https://github.com/codepurse/BlockNSFW/releases';
const UPDATE_CHECK_TTL = 1000 * 60 * 60 * 12; // 12 hours
let updateCheckPromise = null;
// Prefer the store link for the running browser, falling back to a generic URL.
// Uses detectBrowserKey() (UA-based) rather than `typeof browser`, which misfires
// on Chrome — see the note there.
function pickUpdateUrl(data) {
if (!data || typeof data !== 'object') return DEFAULT_UPDATE_URL;
const isFirefox = detectBrowserKey() === 'firefox';
if (isFirefox && typeof data.firefoxUrl === 'string' && data.firefoxUrl) return data.firefoxUrl;
if (!isFirefox && typeof data.chromeUrl === 'string' && data.chromeUrl) return data.chromeUrl;
if (typeof data.url === 'string' && data.url) return data.url;
return DEFAULT_UPDATE_URL;
}
// Fetch version.json (TTL-guarded, like the remote blocklist/whitelist) and
// write { current, latest, updateAvailable, notes, url, checkedAt } to storage.
// Returns the info object, or null on failure (callers fail silently — a failed
// check must never block or surface an error to the user).
async function checkForUpdate(options = {}) {
const { forceRefresh = false } = options;
if (updateCheckPromise) return updateCheckPromise;
updateCheckPromise = (async () => {
try {
const { [UPDATE_INFO_KEY]: cached } = await browserAPI.storage.local.get(UPDATE_INFO_KEY);
const isFresh = cached && cached.checkedAt &&
(Date.now() - cached.checkedAt) < UPDATE_CHECK_TTL;
if (isFresh && !forceRefresh) return cached;
const current = browserAPI.runtime.getManifest().version;
const response = await fetch(REMOTE_VERSION_URL, { cache: 'no-store' });
if (!response.ok) throw new Error(`version check failed (${response.status})`);
const data = await response.json();
const latest = (data && typeof data.latest === 'string') ? data.latest.trim() : '';
const updateAvailable = !!(latest &&
typeof VersionCompare !== 'undefined' &&
VersionCompare.isOutdated(current, latest));
const info = {
current,
latest: latest || current,
updateAvailable,
notes: (data && typeof data.notes === 'string') ? data.notes : '',
url: pickUpdateUrl(data),
checkedAt: Date.now()
};
await browserAPI.storage.local.set({ [UPDATE_INFO_KEY]: info });
if (updateAvailable) {
console.log(`BlockNSFW: update available ${current} -> ${latest}`);
}
return info;
} catch (error) {
console.warn('BlockNSFW: update check failed', error);
return null;
} finally {
updateCheckPromise = null;
}
})();
return updateCheckPromise;
}
// Announcement banner. A small announcement.json lives next to version.json in
// the maintainer's repo; we fetch it (TTL-guarded, like the update check) and
// stash the normalized payload so the options page can render a dismissible info
// banner. This lets the maintainer broadcast a message (notice, event, tip) by
// editing a single file in the repo — no store update required. Self-hosted so
// the same code path works on Chrome and Firefox.
const REMOTE_ANNOUNCEMENT_URL = 'https://raw.githubusercontent.com/codepurse/BlockNSFW/refs/heads/main/data/announcement.json';
const ANNOUNCEMENT_INFO_KEY = 'pblocker_announcement_info';
const ANNOUNCEMENT_CHECK_TTL = 1000 * 60 * 60 * 6; // 6 hours
let announcementCheckPromise = null;
// Bucket the running browser from the user agent. We do NOT use `typeof browser`
// as a Firefox signal: recent Chrome/Chromium also expose a `browser` global, so
// that test misfires and hands Chrome the Firefox override. The UA is reliable
// in the MV3 service worker (WorkerNavigator exposes userAgent). Order matters —
// Edge's UA also contains "Chrome/", so it must be checked first.
function detectBrowserKey() {
const ua = (typeof navigator !== 'undefined' && navigator.userAgent) || '';
if (/\bEdg(?:e|A|iOS)?\//.test(ua)) return 'edge';
if (/\bFirefox\//.test(ua)) return 'firefox';
if (/\bChrom(?:e|ium)\//.test(ua)) return 'chrome';
// UA unavailable/unrecognized: fall back to the API-shim signal, treating a
// `browser` global without Chrome's `chrome.runtime` as Firefox.
if (typeof browser !== 'undefined' && !(typeof chrome !== 'undefined' && chrome.runtime)) return 'firefox';
return 'chrome';
}
// Author-facing key aliases per bucket, matched case-insensitively so the
// `browsers` map can say "chrome"/"chromium"/"Chromium" (etc.) interchangeably.
const BROWSER_KEY_ALIASES = {
firefox: ['firefox'],
chrome: ['chrome', 'chromium'],
edge: ['edge']
};
// Look up the override object for the running browser in a `browsers` map,
// tolerant of key casing and the chrome/chromium alias. Returns null when the
// map is absent or has no entry for this browser.
function lookupBrowserOverride(browsers) {
if (!browsers || typeof browsers !== 'object') return null;
const lower = {};
for (const k of Object.keys(browsers)) lower[k.toLowerCase()] = browsers[k];
for (const alias of (BROWSER_KEY_ALIASES[detectBrowserKey()] || [])) {
if (lower[alias] != null) return lower[alias];
}
return null;
}
// Resolve the announcement for the running browser. A per-browser override in
// `browsers.<firefox|chrome|edge>` wins over the shared top-level fields, so
// each browser can have its own title/message/link/linkText/type; any field the
// override omits falls back to the shared value. `link` is special: when a
// `browsers` map is present we use ONLY this browser's link (never the shared
// one) so we can't route a user to another browser's store — a missing/blank
// link yields '' and the banner hides the button. Without a `browsers` map the
// shared `link` is treated as a generic link (plain single-link announcements
// keep working).
function resolveAnnouncement(data) {
const shared = (data && typeof data === 'object') ? data : {};
const browsers = (shared.browsers && typeof shared.browsers === 'object') ? shared.browsers : null;
const ovRaw = browsers ? lookupBrowserOverride(browsers) : null;
const ov = (ovRaw && typeof ovRaw === 'object') ? ovRaw : {};
const str = (v) => (typeof v === 'string' ? v.trim() : '');
const merged = (field) => str(ov[field]) || str(shared[field]);
return {
title: merged('title'),
message: merged('message'),
linkText: merged('linkText'),
type: str(ov.type) || str(shared.type),
link: browsers ? str(ov.link) : str(shared.link)
};
}
// Fetch announcement.json (TTL-guarded) and write a sanitized payload to storage.
// Returns the info object, or null on failure (callers fail silently — a broken
// announcement fetch must never surface an error to the user).
async function fetchAnnouncement(options = {}) {
const { forceRefresh = false } = options;
if (announcementCheckPromise) return announcementCheckPromise;
announcementCheckPromise = (async () => {
try {
const { [ANNOUNCEMENT_INFO_KEY]: cached } = await browserAPI.storage.local.get(ANNOUNCEMENT_INFO_KEY);
const isFresh = cached && cached.checkedAt &&
(Date.now() - cached.checkedAt) < ANNOUNCEMENT_CHECK_TTL;
if (isFresh && !forceRefresh) return cached;
const response = await fetch(REMOTE_ANNOUNCEMENT_URL, { cache: 'no-store' });
if (!response.ok) throw new Error(`announcement fetch failed (${response.status})`);
const data = await response.json();
const str = (v) => (typeof v === 'string' ? v.trim() : '');
// Collapse the per-browser payload down to the fields for THIS browser.
const resolved = resolveAnnouncement(data);
// Only http(s) links pass through — never javascript:/data: schemes. The
// options page renders title/message as text (never innerHTML), but we
// sanitize the link here so a bad value can't produce a dangerous href.
const safeLink = /^https?:\/\//i.test(resolved.link) ? resolved.link : '';
const rawType = resolved.type.toLowerCase();
const type = (rawType === 'warning' || rawType === 'success') ? rawType : 'info';
const info = {
id: str(data && data.id),
enabled: !!(data && data.enabled),
type,
title: resolved.title,
message: resolved.message,
link: safeLink,
linkText: resolved.linkText,
checkedAt: Date.now()
};
await browserAPI.storage.local.set({ [ANNOUNCEMENT_INFO_KEY]: info });
return info;
} catch (error) {
console.warn('BlockNSFW: announcement fetch failed', error);
return null;
} finally {
announcementCheckPromise = null;
}
})();
return announcementCheckPromise;
}
// Cache management functions
function clearAllCaches() {
patternCache.clear();
urlCheckCache.clear();
keywordCheckCache.clear();
cacheVersion++;
}
function limitCacheSize(cache, maxSize) {
if (cache.size > maxSize) {
const keysToDelete = Array.from(cache.keys()).slice(0, cache.size - maxSize);
keysToDelete.forEach(key => cache.delete(key));
}
}
function addToCache(cache, key, value) {
cache.set(key, value);
limitCacheSize(cache, MAX_CACHE_SIZE);
}
function normalizeDomainForCache(domain) {
// Remove www. prefix and normalize for consistent caching
return (domain || '').trim().toLowerCase().replace(/^www\./, '');
}
function isLikelyDomain(candidate) {
if (!candidate) return false;
if (candidate.length > 253) return false;
// Accept ASCII labels, including ACE-encoded punycode labels that begin with
// "xn--". Each label must be 1-63 chars, alphanumeric or hyphen, may not
// start or end with a hyphen. The TLD may also be a punycode TLD ("xn--...").
const label = '(?!-)(?:xn--[a-z0-9-]{2,61}|[a-z0-9-]{1,63})(?<!-)';
const domainPattern = new RegExp(`^(?:${label}\\.)+${label}$`, 'i');
return domainPattern.test(candidate);
}
function parseHostsFile(text) {
const domains = new Set();
if (typeof text !== 'string' || text.length === 0) {
return domains;
}
const ipPattern = /^(?:\d{1,3}\.){3}\d{1,3}$/;
const lines = text.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
const rawLine = lines[i];
if (!rawLine) continue;
const line = rawLine.trim();
if (!line || line.startsWith('#')) continue;
const parts = line.split(/\s+/);
for (let j = 0; j < parts.length; j++) {
const part = parts[j];
if (!part || part.startsWith('#')) break;
if (ipPattern.test(part) || part === '::1') continue;
const normalized = normalizeDomainForCache(part);
if (isLikelyDomain(normalized)) {
domains.add(normalized);
}
}
}
return domains;
}
function chunkArray(items, chunkSize) {
const chunks = [];
if (!Array.isArray(items) || chunkSize <= 0) return chunks;
for (let i = 0; i < items.length; i += chunkSize) {
chunks.push(items.slice(i, i + chunkSize));
}
return chunks;
}
async function loadBlocklistMeta() {
try {
const { [BLOCKLIST_CACHE_META_KEY]: meta } = await browserAPI.storage.local.get(BLOCKLIST_CACHE_META_KEY);
blocklistMeta = meta || null;
return blocklistMeta;
} catch (error) {
console.warn('BlockNSFW: failed to load blocklist metadata', error);
blocklistMeta = null;
return null;
}
}
async function removeStaleBlocklistChunks(keepCount) {
if (!blocklistMeta || typeof blocklistMeta.chunkCount !== 'number') return;
if (blocklistMeta.chunkCount <= keepCount) return;
const keysToRemove = [];
for (let index = keepCount; index < blocklistMeta.chunkCount; index++) {
keysToRemove.push(`${BLOCKLIST_CACHE_CHUNK_PREFIX}${index}`);
}
if (keysToRemove.length > 0) {
await browserAPI.storage.local.remove(keysToRemove);
}
}
async function storeBlocklistInCache(domains) {
if (!Array.isArray(domains) || domains.length === 0) return null;
const previousMeta = blocklistMeta || (await loadBlocklistMeta());
const uniqueDomains = Array.from(new Set(domains.map(normalizeDomainForCache))).filter(isLikelyDomain);
uniqueDomains.sort();
const chunks = chunkArray(uniqueDomains, BLOCKLIST_CACHE_CHUNK_SIZE);
const dataToStore = {};
chunks.forEach((chunk, index) => {
dataToStore[`${BLOCKLIST_CACHE_CHUNK_PREFIX}${index}`] = chunk;
});
const meta = {
updatedAt: Date.now(),
chunkCount: chunks.length,
version: (previousMeta?.version || 0) + 1,
source: 'remote',
domainCount: uniqueDomains.length
};
dataToStore[BLOCKLIST_CACHE_META_KEY] = meta;
await browserAPI.storage.local.set(dataToStore);
await removeStaleBlocklistChunks(chunks.length);
blocklistMeta = meta;
return meta;
}
async function loadBlocklistFromCache() {
const meta = blocklistMeta || (await loadBlocklistMeta());
if (!meta || !meta.chunkCount) {
return [];
}
const chunkKeys = Array.from({ length: meta.chunkCount }, (_, index) => `${BLOCKLIST_CACHE_CHUNK_PREFIX}${index}`);
const storedChunks = await browserAPI.storage.local.get(chunkKeys);
const domains = [];
for (let index = 0; index < chunkKeys.length; index++) {
const key = chunkKeys[index];
const chunk = storedChunks[key];
if (Array.isArray(chunk)) {
for (let j = 0; j < chunk.length; j++) {
const normalized = normalizeDomainForCache(chunk[j]);
if (isLikelyDomain(normalized)) {
domains.push(normalized);
}
}
}
}
return domains;
}
async function fetchRemoteBlocklist() {
const response = await fetch(REMOTE_BLOCKLIST_URL, { cache: 'no-store' });
if (!response.ok) {
throw new Error(`Failed to download remote blocklist (${response.status})`);
}
const text = await response.text();
const domainSet = parseHostsFile(text);
return Array.from(domainSet);
}
async function ensureRemoteBlocklistUpToDate(options = {}) {
const { forceRefresh = false } = options;
if (remoteBlocklistPromise) {
return remoteBlocklistPromise;
}
remoteBlocklistPromise = (async () => {
try {
const meta = blocklistMeta || (await loadBlocklistMeta());
const isStale = !meta || !meta.updatedAt || (Date.now() - meta.updatedAt) > BLOCKLIST_CACHE_TTL || forceRefresh;
if (!isStale && Array.isArray(defaultBlocklist) && defaultBlocklist.length > 0) {
return { meta, domains: defaultBlocklist };
}
const remoteDomains = await fetchRemoteBlocklist();
if (remoteDomains.length > 0) {
const newMeta = await storeBlocklistInCache(remoteDomains);
const refreshedDomains = await loadBlocklistFromCache();
if (Array.isArray(refreshedDomains) && refreshedDomains.length > 0) {
defaultBlocklist = refreshedDomains;
} else {
defaultBlocklist = remoteDomains.map(normalizeDomainForCache).filter(isLikelyDomain);
}
defaultBlocklistSet = new Set(defaultBlocklist);
await rebuildCompiledPatterns();
return { meta: newMeta, domains: defaultBlocklist };
}
return { meta, domains: defaultBlocklist };
} catch (error) {
console.warn('BlockNSFW: remote blocklist update failed', error);
return { meta: blocklistMeta, domains: defaultBlocklist };
} finally {
remoteBlocklistPromise = null;
}
})();
return remoteBlocklistPromise;
}
// --- Remote Global Whitelist ---
function parseWhitelistFile(text) {
const domains = new Set();
if (typeof text !== 'string' || text.length === 0) return domains;
const lines = text.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line || line.startsWith('#')) continue;
try {
const url = line.includes('://') ? new URL(line) : new URL('https://' + line);
const normalized = normalizeDomainForCache(url.hostname);
if (isLikelyDomain(normalized)) domains.add(normalized);
} catch (_) {
const normalized = normalizeDomainForCache(line);
if (isLikelyDomain(normalized)) domains.add(normalized);
}
}
return domains;
}
async function fetchRemoteWhitelist() {
const response = await fetch(REMOTE_WHITELIST_URL, { cache: 'no-store' });
if (!response.ok) throw new Error(`Failed to download remote whitelist (${response.status})`);
const text = await response.text();
return Array.from(parseWhitelistFile(text));
}
async function loadRemoteWhitelistFromCache() {
try {
const result = await browserAPI.storage.local.get([REMOTE_WHITELIST_CACHE_KEY, REMOTE_WHITELIST_META_KEY]);
const domains = result[REMOTE_WHITELIST_CACHE_KEY];
remoteWhitelistMeta = result[REMOTE_WHITELIST_META_KEY] || null;
if (Array.isArray(domains) && domains.length > 0) {
remoteWhitelistSet = new Set(domains.map(normalizeDomainForCache).filter(isLikelyDomain));
return true;
}
} catch (error) {
console.warn('BlockNSFW: failed to load cached remote whitelist', error);
}
return false;
}
async function storeRemoteWhitelistInCache(domains) {
const uniqueDomains = Array.from(new Set(domains.map(normalizeDomainForCache).filter(isLikelyDomain)));
uniqueDomains.sort();
const meta = {
updatedAt: Date.now(),
version: (remoteWhitelistMeta?.version || 0) + 1,
domainCount: uniqueDomains.length
};
await browserAPI.storage.local.set({
[REMOTE_WHITELIST_CACHE_KEY]: uniqueDomains,
[REMOTE_WHITELIST_META_KEY]: meta
});
remoteWhitelistMeta = meta;
return meta;
}
async function ensureRemoteWhitelistUpToDate(options = {}) {