-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (64 loc) · 1.64 KB
/
background.js
File metadata and controls
84 lines (64 loc) · 1.64 KB
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
var syncRun = false;
var data = {};
function clearData() {
chrome.storage.sync.clear();
data = {};
sendState();
}
function sendState() {
chrome.runtime.sendMessage({type: "BACKGROUND_DATA_UPDATED", data: data});
}
function saveState(){
var toBeSaved= {};
toBeSaved["appData"] = data
chrome.storage.local.set(toBeSaved);
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.type != "CLEAR_DATA"){
return;
}
var from = request.from;
var to = request.to;
delete data[from]
saveState();
sendState();
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.type != "SCRIPT_START"){
return;
}
sendState();
}
);
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(syncRun && request.type == "TIME_TO_DEST"){
var from = request.from;
var to = request.to;
var url = request.url
if(!data[from]) {
data[from] = {};
}
if(!data[from][to]){
data[from][to] = [];
}
data[from][to][data[from][to].length] = request;
saveState();
sendState();
}
});
chrome.storage.local.get("appData", function(localData){
var recoveredData = localData.appData;
console.log("localData", localData)
console.log("here have some data", data)
if(recoveredData) {
console.log("setting data to this", recoveredData)
data = recoveredData;
}
console.log("here have some data2", data)
syncRun = true;
console.log("loaded")
sendState()
});