forked from johnmichel/Library-Detector-for-Chrome
/
main.js
130 lines (115 loc) · 2.92 KB
/
main.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
description: Responds to notification from the Content Script and displays the icon
**/
var libraries, library, tabId;
var Libraries = d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests;
/**
* Parse the data from the meta tag
*/
function parseLibraries(libs) {
if (libs.length === 0) return [];
var libkeys = [];
libs = libs.split(',');
for (var i=0; i<libs.length; i++) {
var libdata = libs[i].split(':');
libkeys.push({
name: libdata[0],
version: libdata[1]
});
}
return libkeys;
}
/**
* Add in the static properties that go with the runtime library data
*/
function getLibraries(libs) { //name, version, icon, url
libraries = parseLibraries(libs);
for (var i=0; i<libraries.length; i++) {
lib = libraries[i];
lib.url = Libraries[lib.name].url;
lib.icon = Libraries[lib.name].icon;
}
return libraries;
}
/**
* Set no icon
*/
function setNoIcon() {
chrome.pageAction.setIcon({
tabId: tabId,
path: '../icons/null.png'
});
chrome.pageAction.setTitle({
tabId: tabId,
title: ''
});
chrome.pageAction.setPopup({
'tabId': tabId,
'popup': ''
});
chrome.pageAction.show(tabId);
}
/**
* Dispatch the program
*/
function run(libs, tab) {
tabId = tab;
libraries = getLibraries(libs);
if (libraries.length === 0) {
setNoIcon();
return;
}
library = libraries[0];
getIcon(library.icon, libraries.length);
};
/**
* Callback to finish rendering after canvases are done loading
*/
function dispatch(pixelData) {
chrome.pageAction.setIcon({
tabId: tabId,
imageData: pixelData
});
chrome.pageAction.setTitle({
tabId: tabId,
title: libraries.length > 1 ? 'Multiple libraries' : library.name + ' ' + library.version
});
chrome.pageAction.setPopup({
'tabId': tabId,
'popup': '../popups/libraries.html'
});
localStorage.setItem('libraries_' + tabId, JSON.stringify(libraries));
chrome.pageAction.show(tabId);
};
/**
* Use a canvas to add an overlay to the icon, if necessary, return the pixel data
*/
function getIcon(iconName, count) {
var image = document.createElement('canvas');
image.width = 16;
image.height = 16;
var context = image.getContext('2d');
var icon = new Image;
icon.src = '../icons/'+iconName+'.png';
icon.addEventListener('load', function() {
context.drawImage(icon, 0, 0, 16, 16);
if (count > 1) {
// overlay circle
context.fillStyle = '#fff';
context.beginPath();
context.arc(12.5, 11, 5.5, 0, Math.PI*2, true);
context.closePath();
context.fill();
// overlay number
context.font = '10px Arial';
context.fillStyle = '#ff0000';
context.textBaseline = 'bottom';
context.textAlign = 'right';
context.fillText(count, 15.75, 16);
}
dispatch(context.getImageData(0, 0, 16, 16));
}, false);
};
chrome.extension.onRequest.addListener(function(library, sender, sendResponse) {
run(library, sender.tab.id);
});