-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackground.js
More file actions
101 lines (83 loc) · 2.92 KB
/
background.js
File metadata and controls
101 lines (83 loc) · 2.92 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
const defaultSettings = {
blurLevel: 0,
contrastLevel: 0,
brightnessLevel: 0,
ghostingLevel: 0,
snowLevel: 0,
cloudyLevel: 0,
flutterLevel: 0,
colorDeficiencyTypeIndex: 0,
colorDeficiencyMatrixValues: null,
blockType: 'noBlock',
blockStrength: 40,
applyCursorEffects: false
};
async function updateActiveTab() {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!activeTab) return;
// Get settings from storage
const result = await chrome.storage.local.get(`tab_${activeTab.id}`);
const settings = result[`tab_${activeTab.id}`] || defaultSettings;
chrome.tabs.sendMessage(activeTab.id, {
type: 'refresh',
settings: settings
})
}
async function updateSettings(settings) {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!activeTab) return;
// Get current settings from storage
const result = await chrome.storage.local.get(`tab_${activeTab.id}`);
const currentSettings = result[`tab_${activeTab.id}`] || {};
// Save to storage
await chrome.storage.local.set({[`tab_${activeTab.id}`]: {...currentSettings, ...settings}});
await updateActiveTab();
}
// Listen for messages
// (2025-refactor) Must use chrome.runtime (not browser.runtime) to avoid undefined error
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// (2025-refactor) browser refresh can reset settings
if (request.type === 'browserRefresh' && sender.tab) {
chrome.storage.local.remove(`tab_${sender.tab.id}`);
}
if (request.type === 'applyCustomCursor') {
(async () => {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!activeTab) return;
const result = await chrome.storage.local.get(`tab_${activeTab.id}`);
const currentSettings = result[`tab_${activeTab.id}`] || {};
await chrome.storage.local.set({[`tab_${activeTab.id}`]: {
...currentSettings,
applyCursorEffects: request.settings.applyCursorEffects
}});
await updateActiveTab();
sendResponse({ success: true });
})();
return true;
}
if (request.type === 'getSettings') {
(async () => {
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (activeTab) {
const result = await chrome.storage.local.get(`tab_${activeTab.id}`);
const settings = result[`tab_${activeTab.id}`] || defaultSettings;
sendResponse(settings);
} else {
sendResponse(defaultSettings);
}
})();
return true;
}
if (request.type === 'updateSettings') {
(async () => {
await updateSettings(request.settings);
sendResponse({ success: true });
})();
return true;
}
return false;
});
// remove settings when the tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
chrome.storage.local.remove(`tab_${tabId}`);
});