-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.js
43 lines (38 loc) · 1.3 KB
/
logging.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
var mylogger = {
log: function(message){
chrome.storage.sync.get('option-usagelog', function(result){
//don't log anything if the options isn't enabled
if (!result['option-usagelog'])
return;
//get a unique ID for this browser
chrome.storage.local.get('id', function(result){
if (!result['id'])
{
//'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
result['id'] = 'xxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
chrome.storage.local.set(result)
}
var id = result['id'];
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
//build a timestamp
date = new Date();
time = date.getFullYear() + "/" + pad(1+date.getMonth(),2) + "/" + pad(date.getDate(),2) + ' ' + pad(date.getHours(),2) + ":" + pad(date.getMinutes(),2) + ":" + pad(date.getSeconds(),2);
//append a log entry
chrome.storage.sync.get('log-' + id, function(result){
list = result['log-' + id] || [];
list.push(time + ": " + message);
result['log-' + id] = list;
chrome.storage.sync.set(result);
});
});
});
//this is disgusting. so much nesting for asynchronous access :(
}
};