-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
110 lines (96 loc) · 3.68 KB
/
background.js
File metadata and controls
110 lines (96 loc) · 3.68 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
// Keep track of active alerts and their tabs
let activeAlerts = {};
let checkInterval = null;
// Function to check a specific tab for alerts
function checkTabForAlerts(tabId, url) {
// Get all alerts from storage
chrome.storage.sync.get(['alerts'], (result) => {
if (!result.alerts) return;
// Check each alert configuration
Object.entries(result.alerts).forEach(([alertId, config]) => {
if (url.includes(config.targetUrl)) {
// Execute content script to check for the word
chrome.scripting.executeScript({
target: { tabId: tabId },
func: (targetWord) => {
return document.body.innerText.includes(targetWord);
},
args: [config.targetWord]
}, (results) => {
if (results && results[0] && results[0].result) {
// Word found, trigger alarm
chrome.runtime.sendMessage({
action: "triggerAlarm",
alertId: alertId,
config: config
});
}
});
}
});
});
}
// Set up periodic checks for all tabs
function setupPeriodicChecks() {
// Clear any existing intervals
if (checkInterval) {
clearInterval(checkInterval);
}
// Get all alerts and their intervals
chrome.storage.sync.get(['alerts'], (result) => {
if (!result.alerts) return;
// Find the minimum interval to use as our check frequency
const minInterval = Math.min(...Object.values(result.alerts).map(a => a.refreshInterval)) * 1000;
// Set up periodic check
checkInterval = setInterval(() => {
// Get all tabs
chrome.tabs.query({}, (tabs) => {
// Check each tab
tabs.forEach(tab => {
if (tab.url && tab.url.startsWith('http')) {
checkTabForAlerts(tab.id, tab.url);
}
});
});
}, minInterval);
});
}
// Listen for tab updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && tab.url.startsWith('http')) {
checkTabForAlerts(tabId, tab.url);
}
});
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && changes.alerts) {
setupPeriodicChecks();
}
});
// Listen for alarm triggers
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "triggerAlarm") {
// Show notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon.png',
title: 'Word Watcher Alert',
message: `Found "${message.config.targetWord}" on ${message.config.targetUrl}`
});
// Play sound in all active tabs
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, { action: "playAlertSound" });
});
});
// Open notification page
const notificationUrl = chrome.runtime.getURL('notification.html') +
`?alertId=${message.alertId}` +
`&alertName=${encodeURIComponent(message.config.alertName)}` +
`&targetWord=${encodeURIComponent(message.config.targetWord)}` +
`&targetUrl=${encodeURIComponent(message.config.targetUrl)}`;
chrome.tabs.create({ url: notificationUrl });
}
});
// Initial setup
setupPeriodicChecks();