forked from livestyle/chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-log.js
67 lines (56 loc) · 1.62 KB
/
error-log.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
'use strict';
function padNum(num) {
return (num < 10 ? '0' : '') + num;
}
function toDOM(html) {
var div = document.createElement('div');
div.innerHTML = html;
var df = document.createDocumentFragment();
while (div.firstChild) {
df.appendChild(div.firstChild);
}
return df;
}
function renderLogItem(item) {
var date = new Date(item.date);
var time = padNum(date.getHours()) + ':' + padNum(date.getMinutes());
return toDOM('<li id="' + item.messageId + '" class="log__item" data-type="' + item.type + '">'
+ '<span class="time">[' + time + ']</span> '
+ item.message.replace(/\t/g, ' ')
+ '</li>'
);
}
function updateLog(items) {
// show log items in reverse order, e.g. newer on top
items = items.reverse();
var container = document.querySelector('.log');
var currentItems = container.querySelectorAll('.log__item');
var lookup = {};
for (var i = 0, il = currentItems.length; i < il; i++) {
lookup[currentItems[i].getAttribute('id')] = currentItems[i];
}
var df = document.createDocumentFragment();
items.forEach(function(item) {
var itemId = item.messageId + '';
if (lookup[itemId]) {
df.appendChild(lookup[itemId]);
delete lookup[itemId];
} else {
df.appendChild(renderLogItem(item));
}
});
// Remove old messages
Object.keys(lookup).forEach(function(id) {
container.removeChild(lookup[id]);
});
// Insert current messages
container.appendChild(df);
}
// Listen to log updates
chrome.runtime.onMessage.addListener(function(message) {
if (message.name === 'log-updated') {
updateLog(message.data);
}
});
// Request current log
chrome.runtime.sendMessage({name: 'get-log'}, updateLog);