-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
50 lines (41 loc) · 1.49 KB
/
Copy pathbackground.js
File metadata and controls
50 lines (41 loc) · 1.49 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
import { callGemini } from './ai.js';
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
// Wait for full load
setTimeout(() => {
chrome.tabs.get(tabId, (updatedTab) => {
if (
chrome.runtime.lastError || // handles rare cases like closed tabs
!updatedTab.url ||
!updatedTab.url.startsWith('http')
) {
return;
}
// 🔄 Check if the extension is enabled from popup toggle
chrome.storage.sync.get('extensionEnabled', (data) => {
const isEnabled = data.extensionEnabled ?? false; // default to disabled
if (!isEnabled) {
console.log("🚫 Extension is disabled. Skipping Gemini call.");
return;
}
try {
const url = new URL(updatedTab.url);
const domain = url.hostname.replace('www.', '');
callGemini(domain, updatedTab.title)
.then(response => {
console.log("Gemini Response:", response);
if (response.trim().toLowerCase() === "distracting") {
chrome.tabs.remove(tabId);
}
})
.catch(err => {
console.error("Gemini API Error:", err);
});
} catch (error) {
console.error("Unexpected Error:", error);
}
});
});
}, 5000); // Delay to allow page content to settle
}
});