-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
63 lines (54 loc) · 2.1 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
// Chrome automatically creates a background.html page for this to execute.
// This can access the inspected page via executeScript
//
// Can use:
// chrome.tabs.*
// chrome.extension.*
var connections = {};
chrome.runtime.onConnect.addListener(function (port) {
var extensionListener = function (message, sender, sendResponse) {
if(message.tabId && message.content) {
connections[message.tabId] = port;
//Evaluate script in inspectedPage
if(message.action === 'code') {
chrome.tabs.executeScript(message.tabId, {code: message.content});
//Attach script to inspectedPage
} else if(message.action === 'script') {
chrome.tabs.executeScript(message.tabId, {file: message.content});
//Pass message to inspectedPage
} else {
chrome.tabs.sendMessage(message.tabId, message, sendResponse);
}
// This accepts messages from the inspectedPage and
// sends them to the panel
}else {
if (message.action == "audit_report") {
if (localStorage) {
// Save the name in localStorage.
localStorage.setItem('audit_results', JSON.stringify(message.results));
}
}
console.log(message);
port.postMessage(message);
}
// sendResponse(message);
}
// Listens to messages sent from the panel
chrome.runtime.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function(port) {
chrome.runtime.onMessage.removeListener(extensionListener);
var tabs = Object.keys(connections);
for (var i=0, len=tabs.length; i<len;i++) {
if (connections[tab[i]] == port) {
delete connections[tab[i]];
break;
}
}
});
// port.onMessage.addListener(function (message) {
// port.postMessage(message);
// });
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
return true;
});