-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
52 lines (43 loc) 路 1.59 KB
/
Copy pathbackground.js
File metadata and controls
52 lines (43 loc) 路 1.59 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
let intervalId; // Fixed variable name consistency
function reloadPageAndCheckChanges(electives) {
clearInterval(intervalId);
intervalId = setInterval(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs[0]) return;
const tabId = tabs[0].id;
chrome.tabs.reload(tabId);
// Create a one-time listener for this specific reload
const listener = (updatedTabId, changeInfo) => {
if (changeInfo.status === 'complete' && updatedTabId === tabId) {
chrome.tabs.onUpdated.removeListener(listener);
// Add a small delay to ensure page is fully loaded
setTimeout(() => {
chrome.tabs.sendMessage(tabId, {
action: 'checkChanges',
electives
}).catch(err => {
console.log('Failed to send message to content script:', err);
});
}, 1000);
}
};
chrome.tabs.onUpdated.addListener(listener);
// Cleanup listener after 10 seconds to prevent memory leaks
setTimeout(() => {
chrome.tabs.onUpdated.removeListener(listener);
}, 10000);
});
}, 5000);
}
chrome.runtime.onMessage.addListener((message) => {
if (message.action === 'stopAutomation') {
clearInterval(intervalId);
console.log('Automation stopped');
}
});
chrome.runtime.onMessage.addListener((message) => {
if (message.action === 'startAutomation') {
console.log('Starting automation with electives:', message.electives);
reloadPageAndCheckChanges(message.electives);
}
});