/
background.js
57 lines (50 loc) · 1.77 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
'use strict';
var enabled = false;
chrome.browserAction.onClicked.addListener(function(tab) {
//console.log(enabled);
enabled = !enabled;
if (!enabled) {
generateFile(eventRecordList);
eventRecordList = [];
}
else {
chrome.tabs.executeScript(tab.id, {file: "third-party/jquery-1.12.3.min.js"}, function () {
chrome.tabs.executeScript(tab.id, {file: "third-party/d3.v3.min.js"}, function () {
chrome.tabs.executeScript(tab.id, {file: "content.js"});
});
});
}
});
var eventRecordList = [];
// Receive msg from content script
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
eventRecordList = eventRecordList.concat(message['data']);
if (eventRecordList.length > 20000) {
generateFile(eventRecordList);
eventRecordList = [];
}
});
// generate a text file to download when the records are more than the threshold
function generateFile(tempList) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var newList = tempList.map(function (x) {
var tempX = [x['timestamp'], x['type'], x['cursor'][0], x['cursor'][1], x['curElement']];
for (var i = 0; i < x['modifiedElements'].length; i++) {
tempX = tempX.concat(x['modifiedElements'][i]);
}
return tempX.map(function (y) {
return y.toString();
}).join(",")
}).join("\n");
var blob = new Blob([newList], {type: "application/octet-binary"}),
url = window.URL.createObjectURL(blob);
chrome.downloads.download({
url: url,
filename: "mouse_event_records.txt",
conflictAction: 'uniquify',
saveAs: true
}
);
}