Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
64 lines (54 loc) · 2.1 KB
/
Copy pathcontent.js
File metadata and controls
64 lines (54 loc) · 2.1 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
// Content script to manage settings
// Runs in ISOLATED world, communicates with injected.js in MAIN world
// Check if we're in app/standalone mode (PWA or installed app)
function isAppMode() {
return window.matchMedia('(display-mode: standalone)').matches ||
window.matchMedia('(display-mode: minimal-ui)').matches ||
window.navigator.standalone === true;
}
// Check if the extension should be active on this site
function checkAndUpdate() {
chrome.storage.sync.get(['enabledSites', 'blockAllSites'], (result) => {
const enabledSites = result.enabledSites || [];
const blockAllSites = result.blockAllSites || false;
const currentHostname = window.location.hostname;
const inAppMode = isAppMode();
let isEnabled = false;
if (blockAllSites) {
isEnabled = true;
} else {
// Check if current site matches any pattern
isEnabled = enabledSites.some(site => {
// Handle both old string format and new object format
const pattern = typeof site === 'string' ? site : site.pattern;
const appModeOnly = typeof site === 'object' ? site.appModeOnly : false;
// Convert wildcard pattern to regex
const regexPattern = pattern
.replace(/\./g, '\\.')
.replace(/\*/g, '.*');
const regex = new RegExp('^' + regexPattern + '$');
const matches = regex.test(currentHostname);
// Only enable if pattern matches AND (not appModeOnly OR we're in app mode)
return matches && (!appModeOnly || inAppMode);
});
}
// Send the enabled state to the injected script
window.postMessage({
type: 'CTRL_W_BLOCKER_UPDATE',
isEnabled: isEnabled
}, '*');
});
}
// Listen for messages from the injected script
window.addEventListener('message', (event) => {
if (event.source !== window) return;
if (event.data.type === 'CTRL_W_BLOCKER_READY') {
checkAndUpdate();
}
});
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && (changes.enabledSites || changes.blockAllSites)) {
checkAndUpdate();
}
});