-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
50 lines (44 loc) · 1.27 KB
/
background.js
File metadata and controls
50 lines (44 loc) · 1.27 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
const BADGE_SUCCESS = { text: '✓', color: '#4CAF50' };
const BADGE_ERROR = { text: '✗', color: '#f44336' };
const BADGE_CLEAR_DELAY = 2000;
const DEFAULT_ICON = {
16: 'icons/icon16.png',
48: 'icons/icon48.png',
128: 'icons/icon128.png'
};
async function handleActionClick(tab) {
try {
const results = await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
if (results && results[0] && results[0].result) {
showBadge(tab.id, BADGE_SUCCESS);
}
} catch {
showBadge(tab.id, BADGE_ERROR);
}
}
function showBadge(tabId, { text, color }) {
chrome.action.setIcon({ path: DEFAULT_ICON, tabId });
chrome.action.setBadgeText({ text, tabId });
chrome.action.setBadgeBackgroundColor({ color, tabId });
setTimeout(() => {
chrome.action.setBadgeText({ text: '', tabId });
}, BADGE_CLEAR_DELAY);
}
// Register listener when loaded as extension
if (typeof chrome !== 'undefined' && chrome.action && chrome.action.onClicked) {
chrome.action.onClicked.addListener(handleActionClick);
}
// Export for testing
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
handleActionClick,
showBadge,
BADGE_SUCCESS,
BADGE_ERROR,
BADGE_CLEAR_DELAY,
DEFAULT_ICON
};
}