-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessor.js
More file actions
105 lines (90 loc) · 3.66 KB
/
Copy pathpreprocessor.js
File metadata and controls
105 lines (90 loc) · 3.66 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// load the JavaScript part of sqlite.js:
if ( !self["initSqlJs"] ) {
if ( self.importScripts ) {
self.importScripts( "https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.5.0/sql-wasm.min.js" );
} else {
let script = document.createElement("script");
script.src = "https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.5.0/sql-wasm.min.js";
document.head.appendChild(script);
}
}
/**
* @param {Blob|File} file - file to load
* Add the contents of the file to the list of imported records
*/
function preprocess_activity_log(file) {
return preprocess_activity_log.load_sql.then(
SQL => new Promise( (resolve,reject) => {
let file_reader = new FileReader(),
activities = [],
add_activity = time => activities.push({
"ActivityStart": time,
"ActivityEnd" : time,
})
;
file_reader.onload = () => {
let db = new SQL.Database(new Uint8Array(file_reader.result));
/*
* Browser history
* Browsers generally manage their history with SQLite
* Run queries to extract visit times...
*/
if (
[
"SELECT visit_time/1000-11644473600000 FROM visits", // Chrome
"SELECT visit_date/1000 FROM moz_historyvisits", // Firefox
"SELECT (visit_time+978307200)*1000 FROM history_visits", // Safari
].some( query => {
try {
let st = db.prepare(query);
while ( st.step() ) add_activity(st.get()[0]);
return true;
} catch (e) {
return false;
}
})
) {
return resolve(activities);
}
/*
* iCalendar
* iCalendar is a simple text format
*/
// check if this looks like an iCalendar file, without stringifying:
const ical_header = "BEGIN:VCALENDAR\n";
if (
new Uint8Array(file_reader.result,0,ical_header.length)
.some( (c,n) => ical_header.charCodeAt(n) != c )
) {
return reject("File not recognised");
}
file_reader.onload = () => {
file_reader.result
.replace(
/\nDT(START|END):([^\r\n]*)/g,
(_,se,date) => {
if ( se == "end" && activities.length ) {
activities[activities.length-1]["ActivityEnd"] = date;
} else {
add_activity(date);
}
})
;
return resolve(activities);
}
file_reader.readAsText(file);
}
file_reader.readAsArrayBuffer(file);
}));
}
// load the WebAssembly part of sqlite.js:
preprocess_activity_log.load_sql = new Promise( (resolve,reject) => {
let interval = setInterval(() => {
if ( self.hasOwnProperty("initSqlJs") ) {
clearInterval(interval);
initSqlJs(
{ locateFile: filename => 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.5.0/'+filename }
).then(resolve,reject);
}
}, 500 );
})