-
Notifications
You must be signed in to change notification settings - Fork 89
/
background.js
67 lines (61 loc) · 2.09 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
61
62
63
64
65
66
/**
* Because of this Chromium bug (https://code.google.com/p/chromium/issues/detail?id=234497)
* we are unable to use chrome.storage API in the context, other than background page.
* Luckily, we can work around this issue with the help of the chrome.runtime API:
*/
chrome.runtime.onConnect.addListener(function(port) {
if(port.name == "chrome.storage port"){
port.onMessage.addListener(function(msg) {
var obj = chrome.storage.sync // chrome.storage.local ?
, method = obj[msg.method] // 'get' or 'set'
, keys = msg.keys
, message_id = msg.message_id;
method.call(obj, keys, function(items){
items = items || {};
items.message_id = message_id;
port.postMessage(items);
})
});
}else if(port.name == "chrome.webNavigation port"){
var tabId;
port.onMessage.addListener(function(msg) {
tabId = msg.tabId;
webNavigationListeners[tabId] = port;
});
port.onDisconnect.addListener(function(){
delete webNavigationListeners[tabId];
})
}else if(port.name == "chrome.nativeMessaging port"){
var nmPort = null;
port.onMessage.addListener(function(msg) {
if(msg.event == 'init'){
nmPort = chrome.runtime.connectNative("com.dfilimonov.devtoolsterminal");
console.log('CONNECT')
nmPort.onDisconnect.addListener(function(){
nmPort = null;
console.log('disconnect')
if(chrome.runtime.lastError){
console.log(chrome.runtime.lastError.message);
port.postMessage({event: 'nm-error', data: chrome.runtime.lastError.message})
}
port.disconnect();
});
nmPort.onMessage.addListener(function(data){
console.log('MESSAGE')
port.postMessage(data);
});
}
if(nmPort){
console.log('POST',msg)
nmPort.postMessage(msg);
}
});
port.onDisconnect.addListener(function(){
console.log('disconnect');
if(nmPort){
nmPort.postMessage({event: 'disconnect', data:{}});
nmPort.disconnect();
}
})
}
});