-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground.js
61 lines (56 loc) · 2.05 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
var cpu_history = {'overall': []};
var icon_draw_context;
var done_init = false;
function init() {
if (done_init) { return; }
done_init = true;
chrome.processes.onUpdatedWithMemory.addListener(receiveProcessInfo);
icon_draw_context = document.getElementById('canvas').getContext('2d');
icon_draw_context.fillStyle = '#f6f6f6';
icon_draw_context.fillRect(0, 0, 19, 19);
chrome.browserAction.setIcon({imageData: icon_draw_context.getImageData(0, 0, 19, 19)});
}
function receiveProcessInfo(processes) {
var totalCPU = 0;
for (pid in processes) {
totalCPU += processes[pid].cpu;
if (!cpu_history[pid]) {
cpu_history[pid] = [];
}
cpu_history[pid].unshift(processes[pid].cpu);
if (cpu_history[pid].length > 350) {
cpu_history[pid].pop();
}
}
for (pid in cpu_history) {
if ((pid != 'overall') && !processes[pid]) {
delete cpu_history[pid];
}
}
cpu_history['overall'].unshift(totalCPU);
if (cpu_history['overall'].length > 350) {
cpu_history['overall'].pop();
}
draw_cpu_graph(cpu_history['overall'], icon_draw_context, 19, 19, 8, 1, 0);
chrome.browserAction.setIcon({ imageData: icon_draw_context.getImageData(0, 0, 19, 19) });
padding = totalCPU < 10 ? ' ' : '';
chrome.browserAction.setBadgeText({text: padding + Math.floor(totalCPU).toString() + '%' + padding});
chrome.browserAction.setBadgeBackgroundColor({color:get_color_for_cpu(totalCPU)});
}
function draw_cpu_graph(data, context, width, height, height_offset, col_width, gap_width) {
context.fillStyle = '#f6f6f6';
context.fillRect(0, 0, width, height);
for (var i = 0; i < data.length; i++) {
var x = width - (i * (col_width + gap_width));
if (x < 0) break;
context.strokeStyle = get_color_for_cpu(data[i]);
context.beginPath();
context.moveTo(x, height);
context.lineTo(x, height - height_offset - (Math.min(data[i], 100)*(height - height_offset)/100));
context.stroke();
}
}
function get_color_for_cpu(cpu) {
return cpu > 30 ? '#F00' : '#228B22';
}
document.addEventListener('DOMContentLoaded', init);