forked from sym3tri/CSS-Inject
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload.js
More file actions
66 lines (49 loc) · 1.97 KB
/
Copy pathload.js
File metadata and controls
66 lines (49 loc) · 1.97 KB
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
// All DOM manipulation must exist inside this "content script" in order to execute in the proper context of the page
// inject the files into the head element
// plus a timestamp cache buster.
function appendStyleNode(id, href) {
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.id = id;
cssNode.href = href+'prototype.css' + '?' + (new Date()).getTime();
document.getElementsByTagName('head')[0].appendChild(cssNode);
var jsNode = document.createElement('script');
jsNode.type = 'text/javascript';
jsNode.id = id;
jsNode.src = href+'prototype.js' + '?' + (new Date()).getTime();
document.getElementsByTagName('head')[0].appendChild(jsNode);
}
// removes the css
function removeStyleNode(id) {
var node = document.getElementById(id);
node && node.parentNode.removeChild(node);
}
// currently does nothing but alert if error
function restoreStateCallback(resp) {
if (!resp.ok) {
alert('Error re-injecting file(s) on refresh. Try pushing the button again');
}
}
// EVENT LISTENERS
// override window onload event to check the state of the current tab on each page load
var oldWindowOnload = window.onload;
window.onload = function() {
// send request to background page to restore the state (since only it knows about state)
chrome.extension.sendRequest({action: 'restoreState'}, restoreStateCallback);
// execute any previously existing window onload events
if (oldWindowOnload && typeof(oldWindowOnload) === 'function') {
oldWindowOnload();
}
};
// listen to injections/removal requests from background.html
chrome.extension.onRequest.addListener(
// depending on state value, injext/remove css
function(req, sender, sendResponse) {
req.state === 'on'
? appendStyleNode(req.id, req.href)
: removeStyleNode(req.id);
// notify of no problems
sendResponse({ok: true});
}
);