-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
55 lines (49 loc) · 1.4 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
let lastTabId = -1
let confettiShown = false
const requiredFiles = [
'lib/confetti.browser.js',
'src/main/utils.js',
'src/main/canvas.js',
'src/main/confetti.js',
'src/main/main.js'
]
function pageIsAllowed(url) {
return !url.startsWith('chrome://') && !url.startsWith('chrome-extension://')
}
// On first install, set confettiEveryPage to true
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ confettiEveryPage: true }, () => {})
})
// On new tab created, reset confettiShown
chrome.tabs.onCreated.addListener((tab) => {
confettiShown = false
lastTabId = tab.id
})
// On tab update, show confetti if required
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
chrome.storage.sync.get('confettiEveryPage', ({ confettiEveryPage }) => {
if (confettiEveryPage && pageIsAllowed(tab.url) && tabId === lastTabId && !confettiShown) {
chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
files: requiredFiles,
})
confettiShown = true
}
})
})
// Show confetti when extension icon clicked
chrome.action.onClicked.addListener(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0]
if (pageIsAllowed(tab.url)) {
chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
files: requiredFiles,
})
}
})
})