-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
224 lines (189 loc) · 6.23 KB
/
background.js
File metadata and controls
224 lines (189 loc) · 6.23 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
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
// AI Focus Agent - Background Script
// Monitors browsing activity and detects distractions
// State management
let currentTab = null;
let sessionStartTime = Date.now();
let siteVisits = {};
let currentSiteStartTime = null;
let warningCount = 0;
let isInFocusMode = false;
let consecutiveDistractions = 0;
// Default settings
let settings = {
focusDomains: [],
productiveKeywords: ['github', 'stackoverflow', 'documentation', 'docs', 'learn', 'tutorial', 'course'],
distractingPatterns: [
'youtube.com/watch',
'reddit.com',
'twitter.com',
'facebook.com',
'instagram.com',
'tiktok.com',
'netflix.com',
'twitch.tv',
'news',
'sports',
'gaming'
],
focusSessionMinutes: 25, // Pomodoro-style
distractionThresholdSeconds: 60, // Alert after 60 seconds on distracting site
enableNotifications: true,
strictMode: false // In strict mode, blocks distracting sites
};
// Load settings from storage
browser.storage.local.get('settings').then((result) => {
if (result.settings) {
settings = { ...settings, ...result.settings };
}
});
// Site categorization logic
function categorizeSite(url) {
if (!url) return 'unknown';
const urlLower = url.toLowerCase();
// Check if it's a focus domain
for (const domain of settings.focusDomains) {
if (urlLower.includes(domain.toLowerCase())) {
return 'focus';
}
}
// Check for productive keywords
for (const keyword of settings.productiveKeywords) {
if (urlLower.includes(keyword.toLowerCase())) {
return 'productive';
}
}
// Check for distracting patterns
for (const pattern of settings.distractingPatterns) {
if (urlLower.includes(pattern.toLowerCase())) {
return 'distracting';
}
}
// Default to neutral
return 'neutral';
}
// Track time spent on current site
function trackSiteTime(url) {
if (currentSiteStartTime && currentTab) {
const timeSpent = Date.now() - currentSiteStartTime;
const domain = new URL(currentTab.url).hostname;
if (!siteVisits[domain]) {
siteVisits[domain] = {
time: 0,
visits: 0,
category: categorizeSite(currentTab.url)
};
}
siteVisits[domain].time += timeSpent;
siteVisits[domain].visits += 1;
}
currentSiteStartTime = Date.now();
currentTab = { url };
}
// Check if user is getting distracted
function checkForDistraction(url, timeOnSite) {
const category = categorizeSite(url);
if (category === 'distracting') {
const secondsOnSite = timeOnSite / 1000;
// Alert after threshold
if (secondsOnSite > settings.distractionThresholdSeconds) {
consecutiveDistractions++;
sendDistractionAlert(url, category, consecutiveDistractions);
return true;
}
} else if (category === 'focus' || category === 'productive') {
// Reset consecutive distractions when back on track
consecutiveDistractions = 0;
}
return false;
}
// Send distraction notification
function sendDistractionAlert(url, category, consecutiveCount) {
if (!settings.enableNotifications) return;
warningCount++;
const messages = [
"Hey! Looks like you're getting distracted. Time to refocus! 🎯",
"You've wandered off track. Let's get back to work! 💪",
"Distraction detected! Remember your goals. 🚀",
"Taking a break? Make sure it's intentional! ⏰",
"Focus time! Close this tab and get back on track. 🧘"
];
const escalatedMessages = [
"You've been distracted multiple times. Take a real break or refocus! ⚠️",
"Seriously, this is your " + consecutiveCount + "th distraction. Time to lock in! 🔒",
"Pattern detected: You keep getting pulled away. What's your priority right now? 🤔"
];
let message = messages[warningCount % messages.length];
if (consecutiveCount > 2) {
message = escalatedMessages[(consecutiveCount - 3) % escalatedMessages.length];
}
browser.notifications.create({
type: 'basic',
iconUrl: 'icons/icon-96.png',
title: 'AI Focus Agent Alert',
message: message
});
// Update badge
browser.browserAction.setBadgeText({ text: warningCount.toString() });
browser.browserAction.setBadgeBackgroundColor({ color: '#FF6B6B' });
}
// Monitor tab changes
browser.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await browser.tabs.get(activeInfo.tabId);
if (currentTab && currentTab.url) {
const timeOnSite = Date.now() - currentSiteStartTime;
checkForDistraction(currentTab.url, timeOnSite);
}
trackSiteTime(tab.url);
});
// Monitor URL changes in current tab
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url) {
if (currentTab && currentTab.url) {
const timeOnSite = Date.now() - currentSiteStartTime;
checkForDistraction(currentTab.url, timeOnSite);
}
trackSiteTime(changeInfo.url);
}
});
// Periodic check (every 30 seconds)
browser.alarms.create('checkDistraction', { periodInMinutes: 0.5 });
browser.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'checkDistraction' && currentTab && currentTab.url) {
const timeOnSite = Date.now() - currentSiteStartTime;
checkForDistraction(currentTab.url, timeOnSite);
}
});
// Listen for messages from popup
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'getStats') {
sendResponse({
siteVisits,
warningCount,
sessionStartTime,
currentSite: currentTab ? currentTab.url : null,
consecutiveDistractions
});
} else if (message.type === 'resetStats') {
siteVisits = {};
warningCount = 0;
consecutiveDistractions = 0;
sessionStartTime = Date.now();
browser.browserAction.setBadgeText({ text: '' });
sendResponse({ success: true });
} else if (message.type === 'updateSettings') {
settings = { ...settings, ...message.settings };
browser.storage.local.set({ settings });
sendResponse({ success: true });
} else if (message.type === 'getSettings') {
sendResponse({ settings });
}
return true; // Keep message channel open for async response
});
// Initialize on startup
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs[0]) {
currentTab = tabs[0];
currentSiteStartTime = Date.now();
}
});
console.log('AI Focus Agent initialized');