-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
60 lines (55 loc) · 1.6 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
let loadGame = false
let highScore = 0
chrome.browserAction.setIcon({ path: 'assets/logoOff.png' })
chrome.extension.onRequest.addListener((request) => {
switch (request.command) {
case 'getLoadGame':
if (loadGame) {
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
command: 'loadGame',
value: loadGame,
})
})
}
break
case 'setScore':
if (request.score > highScore) {
highScore = request.score
chrome.browserAction.setBadgeText({
text: highScore.toString(),
})
}
break
}
})
chrome.browserAction.onClicked.addListener(function (tab) {
loadGame = !loadGame
if (loadGame) {
chrome.browserAction.setIcon({ path: 'assets/logoOn.png' })
chrome.tabs.sendMessage(tab.id, {
command: 'loadGame',
value: loadGame,
})
if (highScore) {
chrome.browserAction.setBadgeText({ text: highScore.toString() })
}
} else {
chrome.tabs.sendMessage(tab.id, { command: 'cleanUp' })
chrome.browserAction.setBadgeText({ text: '' })
chrome.browserAction.setIcon({ path: 'assets/logoOff.png' })
}
})
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == 'install' || details.reason == 'update') {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.executeScript(tab.id, { file: './phaser.js' }, () => {
chrome.tabs.executeScript(tab.id, {
file: './contentScript.js',
})
})
})
})
}
})