-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
56 lines (46 loc) · 1.68 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
/*--------------------------------------------------------------
>>> BACKGROUND
----------------------------------------------------------------
# Global variables
# Runtime
# On connect
--------------------------------------------------------------*/
/*--------------------------------------------------------------
# GLOBAL VARIABLES
--------------------------------------------------------------*/
var tabs = {};
/*--------------------------------------------------------------
# RUNTIME
--------------------------------------------------------------*/
/*--------------------------------------------------------------
# ON CONNECT
--------------------------------------------------------------*/
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === 'popup') {
port.onDisconnect.addListener(function() {
chrome.tabs.query({}, function (tabs) {
for (var i = 0, l = tabs.length; i < l; i++) {
chrome.tabs.sendMessage(tabs[i].id, {
type: 'reset'
});
}
});
});
port.onMessage.addListener(function (message, port) {
if (message && message.type === 'tab') {
port.postMessage({
type: 'tab',
value: tabs[message.id]
});
}
});
}
});
/*--------------------------------------------------------------
# ON MESSAGE
--------------------------------------------------------------*/
chrome.runtime.onMessage.addListener(function (message, sender) {
if (message && message.type === 'background') {
tabs[sender.tab.id] = true;
}
});