forked from cnumr/GreenIT-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
95 lines (79 loc) · 2.9 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (C) 2019-2021 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let connections = {};
/*
* Listen for message form tab and send it to devtools
**/
const notify = (message, sender, sendResponse) => {
if (sender.tab) {
let tabId = sender.tab.id;
if (tabId in connections) connections[tabId].postMessage(message);
else console.warn("Tab not found in connection list.");
}
else console.warn("sender.tab not defined.");
}
chrome.runtime.onMessage.addListener(notify);
console.log("start background process");
// Listen to message from devTools
chrome.runtime.onConnect.addListener((devToolsConnection) => {
console.log("received onConnect");
// assign the listener function to a variable so we can remove it later
let devToolsListener = (message, sender, sendResponse) => {
// in case message form devtools is to clean cache
if (message.clearBrowserCache) {
clearBrowserCache();
return;
}
// Otherwise message is to inject script
else {
// Inject a content script into the identified tab
console.log("received script to execute form tabId " + message.tabId);
if (!connections[message.tabId]) connections[message.tabId] = devToolsConnection;
chrome.tabs.executeScript(message.tabId,
{code: "var analyseBestPractices=" + message.analyseBestPractices + ";", allFrames: true});
chrome.tabs.executeScript(message.tabId,
{file: message.scriptToInject, allFrames: true});
}
}
// add the listener
devToolsConnection.onMessage.addListener(devToolsListener);
devToolsConnection.onDisconnect.addListener((port) => {
devToolsConnection.onMessage.removeListener(devToolsListener);
Object.keys(connections).map(tab => {
if (connections[tab] == port) {
delete connections[tab];
return false;
}
});
});
});
function clearBrowserCache()
{
chrome.browsingData.remove({
}, {
"cache": true,
"cookies": false,
"downloads": true,
"formData": false,
"history": false,
"indexedDB": false,
"localStorage": false,
"passwords": false,
"serviceWorkers": true,
}, console.log("Cache cleaning done"));
}