-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
55 lines (48 loc) · 2.03 KB
/
background.js
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
function checkPage(tabId, keywords) {
browser.tabs.executeScript(tabId, { code: 'var keywords = ' + JSON.stringify(keywords) + ';' }, () => {
browser.tabs.executeScript(tabId, { file: 'content.js' });
});
}
function monitorPage() {
browser.storage.local.get(['urlToMonitor', 'keywordsToMonitor', 'monitoringSuspended']).then(result => {
const urlToMonitor = result.urlToMonitor;
const keywordsToMonitor = result.keywordsToMonitor || ["Open", "Acknowledge", "Customer-Reply"];
const monitoringSuspended = result.monitoringSuspended || false;
if (monitoringSuspended) {
console.log('Monitoring is suspended.');
return;
}
// Check if the URL is already open in any tab
browser.tabs.query({ url: urlToMonitor }, (tabs) => {
if (tabs.length > 0) {
checkPage(tabs[0].id, keywordsToMonitor);
} else {
browser.tabs.create({ url: urlToMonitor }, (tab) => {
browser.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === tab.id && changeInfo.status === 'complete') {
checkPage(tabId, keywordsToMonitor);
browser.tabs.onUpdated.removeListener(listener);
}
});
});
}
});
});
}
// Set the interval to check the page every minute
setInterval(monitorPage, 60000);
// Automatically start the monitoring when the extension is loaded
monitorPage();
browser.runtime.onMessage.addListener((message) => {
if (message.keyword_found) {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.runtime.getURL("icons/icon.png"),
"title": "WHMCS Monitor",
"message": "New Ticket Reply.",
"priority": 2
});
let audio = new Audio(browser.extension.getURL("sounds/mixkit-bell-notification-933.wav"));
audio.play();
}
});