-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
73 lines (66 loc) · 2.15 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// background.js - Background service worker
// Open onboarding page on install
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.tabs.create({
url: 'onboarding.html'
});
}
});
chrome.action.onClicked.addListener(async (tab) => {
if (tab.url.startsWith("http")) { // Ensure the tab is a valid webpage
try {
console.log("Injecting content.js into tab:", tab.id);
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
// Call the checkLastModified function
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
if (window.checkLastModified) {
window.checkLastModified();
} else {
console.error("checkLastModified function not found.");
}
}
});
} catch (error) {
console.error("Error injecting content script:", error);
}
} else {
console.warn("Cannot inject script into this tab:", tab.url);
}
});
// Listen for messages from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.timestamp) {
// Update the extension badge with info
chrome.action.setBadgeText({
text: "✓",
tabId: sender.tab.id
});
chrome.action.setBadgeBackgroundColor({
color: "#4CAF50",
tabId: sender.tab.id
});
chrome.action.setTitle({
title: `Last Modified: ${message.timestamp}`,
tabId: sender.tab.id
});
} else {
chrome.action.setBadgeText({
text: "✗",
tabId: sender.tab.id
});
chrome.action.setBadgeBackgroundColor({
color: "#F44336",
tabId: sender.tab.id
});
chrome.action.setTitle({
title: "No timestamp found",
tabId: sender.tab.id
});
}
});