-
Notifications
You must be signed in to change notification settings - Fork 3
/
which_host.js
74 lines (60 loc) · 1.61 KB
/
which_host.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
68
69
70
71
72
73
74
var container;
var cache = {};
var needRender = false;
var getIPClassName = function (ip) {
var arr;
if (ip === 'fromCache') {
return 'cache';
}
else {
arr = ip.split('.');
if (arr[0] === '10') {
return 'internal';
}
if (arr[0] === '172' && arr[1] >= 16 && arr[1] <= 31) {
return 'internal';
}
if (arr[0] === '192' && arr[1] === '168') {
return 'internal';
}
return ' ';
}
};
var render = function () {
var result = [];
var hostname
if (!container) {
container = document.createElement('div');
container.setAttribute('id', 'which-host-container');
container.classList.add('right');
document.body.appendChild(container);
function toggleClass() {
if (container.classList.contains('left')) {
container.classList.remove('left');
container.classList.add('right');
} else {
container.classList.remove('right');
container.classList.add('left');
}
}
container.addEventListener('mouseenter', toggleClass, false);
}
Object.keys(cache).forEach(function (hostname) {
result.push('<div class="item"><span class="hostname">' + hostname + '</span>' + cache[hostname].map(function (ip) {return '<span class="ip ' + getIPClassName(ip) + '">' + ip + '</span>'}).join('') + '</div>');
});
container.innerHTML = result.join(' ');
needRender = false;
};
chrome.runtime.onMessage.addListener(function(message){
if (!cache[message.hostname]) {
needRender = true;
cache[message.hostname] = [message.ip];
}
else if (cache[message.hostname].indexOf(message.ip) == -1) {
needRender = true;
cache[message.hostname].push(message.ip);
}
if (needRender) {
render();
}
});