-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
145 lines (136 loc) · 4.54 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const downs = {};
chrome.alarms.create("History routine",
{ delayInMinutes : 5, periodInMinutes: 30}
);
chrome.alarms.create("Bookmarks routine",
{ delayInMinutes: 5, periodInMinutes: 1440}
);
chrome.downloads.onCreated.addListener(function (downloadItem) {
downs[downloadItem.id] = {
id: downloadItem.id,
startTime: downloadItem.startTime,
totalBytes: downloadItem.totalBytes,
receivedBytes: 0,
identity: null,
mime: downloadItem.mime,
danger: downloadItem.danger,
url: downloadItem.url,
incognito: downloadItem.incognito,
referrer: downloadItem.referrer,
endTime: null,
status: null,
};
});
async function processDownloadDelta(downloadDelta) {
if (downloadDelta.state) {
if (downloadDelta.state.current === "complete" || downloadDelta.state.current === "interrupted") {
downs[downloadDelta.id].status = downloadDelta.state.current;
try {
const userInfo = await new Promise((resolve) => {
chrome.identity.getProfileUserInfo(resolve);
});
downs[downloadDelta.id].identity = userInfo;
} catch (error) {
console.error("Error getting user profile info:", error);
}
downs[downloadDelta.id].endTime = downloadDelta.endTime.current;
const data = { download: downs[downloadDelta.id] };
userDownloads(data);
delete downs[downloadDelta.id];
}
}
}
function processAlarmData(alarm) {
if (alarm.name === "History routine") {
const recordedTime = new Date().getTime() - 30 * 60 * 1000;
chrome.history.search({ text: '', maxResults: 2, startTime: recordedTime}, async function (pastData) {
const userInfo = await new Promise((resolve) => {
chrome.identity.getProfileUserInfo(resolve);
});
data = {
history: pastData,
recordedAt: recordedTime,
identity: userInfo
}
routineData(data, 'periodicHistory');
});
}
else if(alarm.name === "Bookmarks routine") {
const recordedTime = new Date().getTime() - 30 * 60 * 1000;
chrome.bookmarks.getTree(async function (bookMarks) {
const userInfo = await new Promise((resolve) => {
chrome.identity.getProfileUserInfo(resolve);
});
data = {
bookmarks: bookMarks,
recordedAt: recordedTime,
identity: userInfo
}
routineData(data, 'periodicBookmarks');
});
}
}
chrome.downloads.onChanged.addListener(function (downloadDelta) {
processDownloadDelta(downloadDelta);
});
chrome.alarms.onAlarm.addListener(function (alarm) {
processAlarmData(alarm);
});
function routineData(data, routine) {
if (routine === 'periodicHistory') {
fetch('http://127.0.0.1:8000/routine/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'data': data,
'routine': routine
})
})
.then(response => response.json())
.then(data => {
console.log('Successfully sent user history to server:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
else if (routine === 'periodicBookmarks') {
fetch('http://127.0.0.1:8000/routine/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'data': data,
'routine': routine
})
})
.then(response => response.json())
.then(data => {
console.log('Successfully sent user bookmark data to server:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
}
function userDownloads(data) {
fetch('http://127.0.0.1:8000/downloads/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log('Successfully sent user download data to server:', data);
}
)
.catch(error => {
console.error('Error:', error);
}
);
}