-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
267 lines (225 loc) · 6.82 KB
/
background.js
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
// State management
let state = {
blockedWebsites: [],
screenTime: {},
activeTab: {
id: null,
url: null,
lastUpdate: Date.now(),
},
};
// Helper functions
const getStartOfDay = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
};
const getDomain = (url) => {
try {
if (!url || url.startsWith("chrome-extension://")) return null;
const domain = new URL(url).hostname;
return domain && !domain.includes("chrome-extension") ? domain : null;
} catch (e) {
return null;
}
};
const validateUrl = (url) => {
try {
url = url.replace(/^(https?:\/\/)?(www\.)?/, "").split(/[/?#]/)[0];
if (!url || url.length < 3 || !url.includes(".")) {
throw new Error("Invalid URL format");
}
return url.toLowerCase();
} catch (error) {
throw new Error("Invalid URL format");
}
};
// Website blocking functions
const updateBlockingRules = async () => {
try {
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
const rulesToRemove = existingRules.map((rule) => rule.id);
const rulesToAdd = state.blockedWebsites.flatMap((site, index) => {
const patterns = [`*://${site.url}/*`, `*://www.${site.url}/*`];
return patterns.map((pattern, i) => ({
id: index * 2 + i + 1,
priority: 1,
action: { type: "block" },
condition: {
urlFilter: pattern,
resourceTypes: ["main_frame", "sub_frame"],
},
}));
});
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rulesToRemove,
addRules: rulesToAdd,
});
} catch (error) {
console.error("Error updating blocking rules:", error);
throw error;
}
};
const blockWebsite = async (website, duration) => {
try {
if (!website || !duration) {
throw new Error("Website URL and duration are required");
}
const url = validateUrl(website);
const now = Date.now();
const finishTime = now + duration * 60 * 1000;
// Check if already blocked
if (
state.blockedWebsites.some(
(site) => site.url === url && site.finishTime > now
)
) {
throw new Error("Website is already blocked");
}
// Add to blocked list
state.blockedWebsites.push({
url,
finishTime,
blockedAt: now,
});
// Save state and update rules
await chrome.storage.local.set({ blockedWebsites: state.blockedWebsites });
await updateBlockingRules();
await chrome.alarms.create(url, { when: finishTime });
return true;
} catch (error) {
console.error("Error blocking website:", error);
throw error;
}
};
const unblockWebsite = async (website) => {
try {
const url = validateUrl(website);
state.blockedWebsites = state.blockedWebsites.filter(
(site) => site.url !== url
);
await chrome.storage.local.set({ blockedWebsites: state.blockedWebsites });
await updateBlockingRules();
await chrome.alarms.clear(url);
return true;
} catch (error) {
console.error("Error unblocking website:", error);
throw error;
}
};
// Screen time tracking functions
const updateScreenTime = () => {
if (!state.activeTab.url) return;
const domain = getDomain(state.activeTab.url);
if (!domain) return;
const now = Date.now();
const timeSpent = now - state.activeTab.lastUpdate;
// Only update if time spent is reasonable (less than 1 minute)
if (timeSpent > 0 && timeSpent < 60000) {
if (!state.screenTime[domain]) {
state.screenTime[domain] = { timeSpent: 0, lastUpdated: now };
}
// Reset if it's a new day
if (state.screenTime[domain].lastUpdated < getStartOfDay()) {
state.screenTime[domain].timeSpent = 0;
}
state.screenTime[domain].timeSpent += timeSpent;
state.screenTime[domain].lastUpdated = now;
chrome.storage.local.set({ screenTime: state.screenTime });
}
state.activeTab.lastUpdate = now;
};
// Initialize extension
const initializeExtension = async () => {
try {
// Load saved state
const data = await chrome.storage.local.get([
"blockedWebsites",
"screenTime",
]);
// Restore blocked websites
state.blockedWebsites = (data.blockedWebsites || []).filter(
(site) => site.finishTime > Date.now()
);
// Restore screen time
state.screenTime = data.screenTime || {};
// Clean up expired data
const todayStart = getStartOfDay();
Object.keys(state.screenTime).forEach((domain) => {
if (state.screenTime[domain].lastUpdated < todayStart) {
state.screenTime[domain].timeSpent = 0;
state.screenTime[domain].lastUpdated = Date.now();
}
});
// Save cleaned up state
await chrome.storage.local.set({
blockedWebsites: state.blockedWebsites,
screenTime: state.screenTime,
});
// Update blocking rules
await updateBlockingRules();
} catch (error) {
console.error("Error initializing extension:", error);
}
};
// Event listeners
chrome.runtime.onInstalled.addListener(initializeExtension);
chrome.runtime.onStartup.addListener(initializeExtension);
chrome.alarms.onAlarm.addListener((alarm) => {
unblockWebsite(alarm.name);
});
chrome.tabs.onActivated.addListener(async (activeInfo) => {
updateScreenTime();
state.activeTab.id = activeInfo.tabId;
try {
const tab = await chrome.tabs.get(activeInfo.tabId);
state.activeTab.url = tab.url;
state.activeTab.lastUpdate = Date.now();
} catch (error) {
console.error("Error getting tab:", error);
state.activeTab.url = null;
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tabId === state.activeTab.id && changeInfo.url) {
updateScreenTime();
state.activeTab.url = changeInfo.url;
state.activeTab.lastUpdate = Date.now();
}
});
// Message handling
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Received message:", message);
switch (message.action) {
case "blockWebsite":
blockWebsite(message.website, message.duration)
.then(() => sendResponse({ success: true }))
.catch((error) =>
sendResponse({
success: false,
error: error.message,
})
);
break;
case "unblockWebsite":
unblockWebsite(message.website)
.then(() => sendResponse({ success: true }))
.catch((error) =>
sendResponse({
success: false,
error: error.message,
})
);
break;
case "getScreenTime":
sendResponse({ screenTime: state.screenTime });
break;
case "getBlockedWebsites":
sendResponse({ blockedWebsites: state.blockedWebsites });
break;
default:
sendResponse({ success: false, error: "Unknown action" });
}
return true;
});
// Update screen time periodically
setInterval(updateScreenTime, 1000);