-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
56 lines (46 loc) · 1.28 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
/* global chrome */
/**
* Handle devtool connections.
*/
const devToolConnections = {}
chrome.runtime.onConnect.addListener((devToolConnection) => {
let tabId = -1
const listener = (message, sender, sendResponse) => {
switch (message.command) {
case `initialize`:
tabId = message.tabId
devToolConnections[tabId] = devToolConnection
chrome.tabs.executeScript(tabId, { code: `initializeStore()` })
return
default:
console.warn(`Unrecognized command:`, message)
return
}
}
devToolConnection.onMessage.addListener(listener)
devToolConnection.onDisconnect.addListener((devToolConnection) => {
devToolConnection.onMessage.removeListener(listener)
delete devToolConnections[tabId]
})
})
/**
* Pass messages from content script to devtools.
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (sender.tab) {
const tabId = sender.tab.id
if (tabId in devToolConnections) {
devToolConnections[tabId].postMessage(message)
}
}
return true
})
/**
* Delete connection when removed or replaced.
*/
chrome.tabs.onRemoved.addListener(tabId => {
delete devToolConnections[tabId]
})
chrome.tabs.onReplaced.addListener((newTabId, tabId) => {
delete devToolConnections[tabId]
})