-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
56 lines (46 loc) · 1.43 KB
/
content_script.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
var API = {
_log: function() {
var logEntry = ["JSKit"];
for (var i = 0, l = arguments.length; i < l; i++)
logEntry.push(arguments[i]);
console.log.apply(console, logEntry);
},
_onRequest: function(action, params, sendResponse) {
if (typeof action == 'string'
&& typeof this[action] == 'function') {
this[action].call(this, params, sendResponse);
}
},
buildJSTag: function(scriptSrc, notifyLoaded) {
var scriptEl = document.createElement('script');
scriptEl.type = 'text/javascript';
scriptEl.src = scriptSrc;
scriptEl.onload = function() {
API._log("loaded", scriptSrc);
notifyLoaded();
};
return scriptEl;
},
buildCSSTag: function(scriptSrc, notifyLoaded) {
var linkEl = document.createElement('link');
linkEl.type = 'text/css';
linkEl.rel = 'stylesheet';
linkEl.href = scriptSrc;
API._log("loaded", scriptSrc);
notifyLoaded();
return linkEl;
},
attachScript: function(scriptSrc, notifyLoaded) {
var target = document.getElementsByTagName('head');
if (target.length == 0)
target = document.getElementsByTagName('body');
if (target.length == 0)
return;
var isJS = scriptSrc.split('.').pop().toLowerCase() == 'js';
var el = this[isJS ? 'buildJSTag' : 'buildCSSTag'](scriptSrc, notifyLoaded);
target[0].appendChild(el);
}
};
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
API._onRequest(request.action, request.params, sendResponse);
});