Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ function initializeExtension() {
// Start tracking current tab
// startContinuousTracking();
// setInterval(monitorIfBlocked, 3000);
chrome.alarms.create('timeTracker', { periodInMinutes: 1 / 60 }); // 1 second
chrome.alarms.create('blocker', { periodInMinutes: 3 / 60 }); // 3 seconds
if (chrome.alarms) {
chrome.alarms.create('timeTracker', { periodInMinutes: 1 / 60 }); // 1 second
chrome.alarms.create('blocker', { periodInMinutes: 3 / 60 }); // 3 seconds
} else {
console.error('chrome.alarms API is not available. Check manifest.json permissions.');
}
}

function trackActiveTab() {
Expand Down Expand Up @@ -126,36 +130,50 @@ function updateIconBadge() {
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log('tabs onUpdated', { changeInfo, tab });

if (!changeInfo.url) {
// Check both changeInfo.url and tab.url to catch all navigation events
const urlToCheck = changeInfo.url || (changeInfo.status === 'loading' && tab.url);

if (!urlToCheck) {
return;
}

// Skip chrome:// and extension pages
if (urlToCheck.startsWith('chrome://') ||
urlToCheck.startsWith('chrome-extension://') ||
urlToCheck.startsWith('about:')) {
return;
}

chrome.storage.local.get(['isBlocking', 'blockedWebsites'], (result) => {
const isBlocking = result.isBlocking === undefined ? true : result.isBlocking;
const blockedWebsites = result.blockedWebsites || [];

if (!isBlocking) {
if (!isBlocking || blockedWebsites.length === 0) {
return;
}

const url = new URL(changeInfo.url);
const domain = url.hostname;
try {
const url = new URL(urlToCheck);
const domain = url.hostname;

const isBlocked = blockedWebsites.some((blockedSite) => {
if (blockedSite.startsWith('*.')) {
return domain.endsWith(blockedSite.substring(2));
}
return domain === blockedSite;
});
const isBlocked = blockedWebsites.some((blockedSite) => {
if (blockedSite.startsWith('*.')) {
return domain.endsWith(blockedSite.substring(2));
}
return domain === blockedSite || domain === 'www.' + blockedSite;
});

if (isBlocked) {
chrome.tabs.update(tabId, { url: chrome.runtime.getURL('blocked.html') });
if (isBlocked) {
chrome.tabs.update(tabId, { url: chrome.runtime.getURL('blocked.html') });

chrome.storage.local.get('gotchaStats', (result) => {
const stats = result.gotchaStats || {};
stats[domain] = (stats[domain] || 0) + 1;
chrome.storage.local.set({ gotchaStats: stats });
});
chrome.storage.local.get('gotchaStats', (result) => {
const stats = result.gotchaStats || {};
stats[domain] = (stats[domain] || 0) + 1;
chrome.storage.local.set({ gotchaStats: stats });
});
}
} catch (error) {
console.log('Error checking blocked site:', error);
}
});
});
Expand Down Expand Up @@ -240,12 +258,18 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
}
});

chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'timeTracker') {
trackActiveTab();
} else if (alarm.name === 'blocker') {
monitorIfBlocked();
}
});
if (chrome.alarms) {
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'timeTracker') {
trackActiveTab();
} else if (alarm.name === 'blocker') {
monitorIfBlocked();
}
});
} else {
console.error('chrome.alarms API is not available. Using setInterval as fallback.');
setInterval(trackActiveTab, 1000);
setInterval(monitorIfBlocked, 3000);
}

console.log('Background script loaded');
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Developer Distractor Destroyer",
"version": "1.0",
"description": "Block distracting websites and track time spent browsing",
"permissions": ["storage", "activeTab", "tabs", "idle", "webNavigation"],
"permissions": ["storage", "activeTab", "tabs", "idle", "webNavigation", "alarms"],
"host_permissions": ["<all_urls>"],
"background": {
"service_worker": "background.js"
Expand Down
Loading