-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
169 lines (145 loc) · 5.25 KB
/
Copy pathbackground.js
File metadata and controls
169 lines (145 loc) · 5.25 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
const DEFAULTS = {
enabled: true,
automation: "manual",
timeStart: "20:00",
timeEnd: "07:00",
intensity: "medium",
disabledSites: [],
userDarkSites: []
};
const PRELOAD_SCRIPT_ID = "sensodark-preload";
async function syncPreloadRegistration(enabled) {
const registered = await chrome.scripting.getRegisteredContentScripts({
ids: [PRELOAD_SCRIPT_ID]
});
const exists = registered.length > 0;
if (enabled && !exists) {
await chrome.scripting.registerContentScripts([{
id: PRELOAD_SCRIPT_ID,
matches: ["<all_urls>"],
css: ["preload.css"],
runAt: "document_start",
allFrames: true,
persistAcrossSessions: true
}]);
} else if (!enabled && exists) {
await chrome.scripting.unregisterContentScripts({ ids: [PRELOAD_SCRIPT_ID] });
}
}
async function ensureInitialized() {
let { settings } = await chrome.storage.sync.get("settings");
// One-time migration from the pre-1.5 local store to synced storage
if (!settings) {
const local = await chrome.storage.local.get("settings");
if (local.settings) settings = local.settings;
}
const merged = {
...DEFAULTS,
...(settings || {}),
disabledSites: Array.isArray(settings?.disabledSites) ? settings.disabledSites : [],
userDarkSites: Array.isArray(settings?.userDarkSites) ? settings.userDarkSites : []
};
if (merged.automation === "time") {
merged.enabled = isInTimeRange(merged.timeStart, merged.timeEnd);
}
if (!settings || JSON.stringify(settings) !== JSON.stringify(merged)) {
await chrome.storage.sync.set({ settings: merged });
}
await syncPreloadRegistration(merged.enabled);
const alarm = await chrome.alarms.get("sensodark-time-check");
if (!alarm) chrome.alarms.create("sensodark-time-check", { periodInMinutes: 1 });
}
// ── Install / browser startup ──
chrome.runtime.onInstalled.addListener(() => ensureInitialized());
chrome.runtime.onStartup.addListener(() => ensureInitialized());
ensureInitialized().catch(() => {});
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "sync" || !changes.settings?.newValue) return;
syncPreloadRegistration(!!changes.settings.newValue.enabled).catch(() => {});
});
// ── Badge ──
// Tab states live in storage.session so they survive service worker restarts
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.type === "TAB_STATE" && sender.tab && sender.frameId === 0) {
chrome.storage.session.set({ ["tab_" + sender.tab.id]: msg.active });
updateBadge(sender.tab.id, msg.active);
}
if (msg.type === "SYSTEM_DARK_MODE") {
handleSystemDarkMode(msg.isDark);
}
});
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
const key = "tab_" + tabId;
const data = await chrome.storage.session.get(key);
if (data[key] !== undefined) {
updateBadge(tabId, data[key]);
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
chrome.storage.session.remove("tab_" + tabId);
});
function updateBadge(tabId, active) {
chrome.action.setBadgeText({ text: active ? "" : "OFF", tabId });
chrome.action.setBadgeBackgroundColor({ color: "#555", tabId });
}
// ── Feature 5: Keyboard shortcuts ──
chrome.commands.onCommand.addListener(async (command) => {
const { settings } = await chrome.storage.sync.get("settings");
if (!settings) return;
if (command === "toggle-dark") {
settings.enabled = !settings.enabled;
await chrome.storage.sync.set({ settings });
}
if (command === "toggle-site") {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || !tab.url) return;
try {
const hostname = new URL(tab.url).hostname;
const sites = settings.disabledSites || [];
const idx = sites.indexOf(hostname);
if (idx >= 0) {
sites.splice(idx, 1);
} else {
sites.push(hostname);
}
settings.disabledSites = sites;
await chrome.storage.sync.set({ settings });
} catch (_) {}
}
});
async function handleSystemDarkMode(isDark) {
const { settings } = await chrome.storage.sync.get("settings");
if (!settings || settings.automation !== "system") return;
if (settings.enabled !== isDark) {
settings.enabled = isDark;
await chrome.storage.sync.set({ settings });
}
}
// ── Feature 6: Time-based automation ──
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name !== "sensodark-time-check") return;
const { settings } = await chrome.storage.sync.get("settings");
if (!settings || settings.automation !== "time") return;
const shouldBeEnabled = isInTimeRange(settings.timeStart, settings.timeEnd);
if (settings.enabled !== shouldBeEnabled) {
settings.enabled = shouldBeEnabled;
await chrome.storage.sync.set({ settings });
}
});
function isInTimeRange(startStr, endStr) {
const now = new Date();
const h = now.getHours();
const m = now.getMinutes();
const current = h * 60 + m;
const [sh, sm] = startStr.split(":").map(Number);
const [eh, em] = endStr.split(":").map(Number);
const start = sh * 60 + sm;
const end = eh * 60 + em;
// Same start and end means the whole day
if (start === end) return true;
if (start <= end) {
return current >= start && current < end;
}
// Wraps midnight (e.g., 20:00 - 07:00)
return current >= start || current < end;
}