-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
111 lines (96 loc) · 3.78 KB
/
background.js
File metadata and controls
111 lines (96 loc) · 3.78 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
// Background script for Productivity Blocker
let timeLimits = {};
let timeSpent = {};
let reminderMessages = {};
let blockedMessage = 'Time limit exceeded. Focus on your goals!';
const defaultSettings = {
sites: {
'facebook.com': { enabled: true, timeLimit: 7, message: 'Stay productive! Avoid distractions.' },
'twitter.com': { enabled: true, timeLimit: 7, message: 'Focus on your goals!' },
'instagram.com': { enabled: true, timeLimit: 7, message: 'Time to be productive!' },
'tiktok.com': { enabled: true, timeLimit: 7, message: 'Distractions blocked. Get back to work!' },
'reddit.com': { enabled: true, timeLimit: 7, message: 'Prioritize productivity!' },
'youtube.com': { enabled: true, timeLimit: 3600, message: 'YouTube time up. Focus on tasks!' }
},
defaultTime: 300,
reminderMessage: 'Stay focused and productive! Avoid distractions.',
blockedMessage: 'Time limit exceeded. Take a break and refocus on your priorities.'
};
chrome.runtime.onInstalled.addListener(() => {
console.log('Productivity Blocker extension installed.');
// Initialize settings if not exist
chrome.storage.sync.get(defaultSettings, (settings) => {
chrome.storage.sync.set(settings);
loadSettings();
});
// Initialize timeSpent from storage
chrome.storage.local.get(['timeSpent'], (result) => {
timeSpent = result.timeSpent || {};
});
// Open options page for new users with a small delay
setTimeout(() => {
chrome.tabs.create({ url: chrome.runtime.getURL('options.html') });
}, 1000);
});
// Initialize on startup as well
chrome.runtime.onStartup.addListener(() => {
loadSettings();
chrome.storage.local.get(['timeSpent'], (result) => {
timeSpent = result.timeSpent || {};
});
});
// Load settings from storage
function loadSettings() {
chrome.storage.sync.get(defaultSettings, (settings) => {
timeLimits = {};
reminderMessages = {};
for (const [domain, config] of Object.entries(settings.sites)) {
if (config.enabled) {
timeLimits[domain] = config.timeLimit * 1000; // convert to ms
reminderMessages[domain] = config.message;
}
}
blockedMessage = settings.blockedMessage;
console.log('Settings loaded:', { timeLimits, reminderMessages });
});
}
// Reload settings when changed
chrome.storage.onChanged.addListener(() => {
loadSettings();
});
// Save timeSpent to storage periodically
setInterval(() => {
chrome.storage.local.set({ timeSpent });
}, 10000); // every 10 seconds
// Check active tab every second
setInterval(async () => {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab && tab.url && tab.url.startsWith('http')) {
const url = new URL(tab.url);
const domain = url.hostname.replace('www.', '');
console.log(`Checking domain: ${domain}, timeLimits:`, timeLimits);
if (timeLimits[domain]) {
timeSpent[domain] = (timeSpent[domain] || 0) + 1000;
console.log(`Time spent on ${domain}: ${timeSpent[domain]}ms, limit: ${timeLimits[domain]}ms`);
if (timeSpent[domain] >= timeLimits[domain]) {
console.log(`Time limit exceeded for ${domain}, redirecting...`);
// Redirect to blocked page
const blockedUrl = chrome.runtime.getURL('blocked.html');
chrome.tabs.update(tab.id, { url: blockedUrl });
// Reset time
timeSpent[domain] = 0;
// Show notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon48.png',
title: 'Time Limit Exceeded',
message: reminderMessages[domain] || 'Focus on your productivity!'
});
}
}
}
} catch (e) {
console.error('Error in time checking:', e);
}
}, 1000);