-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbackground.js
More file actions
63 lines (55 loc) · 2.13 KB
/
Copy pathbackground.js
File metadata and controls
63 lines (55 loc) · 2.13 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
/**
* MatuFlow Background Service Worker
* Handles fetching theme from local bridge to avoid CORS/LocalNetwork CORS popups in content scripts.
*/
const BRIDGE_URL = 'http://localhost:3000/api/theme';
let pollInterval = null;
async function fetchAndStoreTheme() {
try {
// Add cache-buster to bypass any middleman caching
const cacheBuster = `?t=${Date.now()}`;
const response = await fetch(BRIDGE_URL + cacheBuster);
const data = await response.json();
if (data.css) {
// Small optimization: Only set if changed
chrome.storage.local.get(['matuflow_updated_at'], (result) => {
if (result.matuflow_updated_at !== data.updatedAt) {
chrome.storage.local.set({
'matuflow_theme': data.css,
'matuflow_updated_at': data.updatedAt
});
}
});
}
} catch (e) {
// Silent fail if bridge is down
}
}
// MV3 Service Workers hibernate. Using multiple triggers to wake it up.
// 1. Periodic poll (for when browser is active)
setInterval(fetchAndStoreTheme, 2000);
// 2. Alarm fallback (MV3 recommended for background tasks)
chrome.alarms.create('sync_theme', { periodInMinutes: 1 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'sync_theme') {
fetchAndStoreTheme();
}
});
// 3. Activity triggers (Wake up on tab changes/navigation)
chrome.tabs.onActivated.addListener(fetchAndStoreTheme);
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (changeInfo.status === 'complete') fetchAndStoreTheme();
});
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get(['matuflow_enabled', 'matuflow_blacklist', 'disable_site_theming'], (result) => {
const defaults = {};
if (result.matuflow_enabled === undefined) defaults.matuflow_enabled = true;
if (result.matuflow_blacklist === undefined) defaults.matuflow_blacklist = [];
if (result.disable_site_theming === undefined) defaults.disable_site_theming = false;
if (Object.keys(defaults).length > 0) {
chrome.storage.local.set(defaults);
}
});
fetchAndStoreTheme();
});
chrome.runtime.onStartup.addListener(fetchAndStoreTheme);