This repository was archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbackground.js
More file actions
110 lines (98 loc) · 2.91 KB
/
background.js
File metadata and controls
110 lines (98 loc) · 2.91 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
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
/*global chrome: true*/
(function () {
'use strict';
// TODO: Shortcut keys
// TODO: Optionally remove ALL existing CSS?
// global object to track on/off state of each tab
var state = {},
storage = window.localStorage;
// Displays error messages
function error(msg) {
window.alert('ERROR: ' + msg);
}
// Checks if the supplied url is valid
// TODO: make this more robust
function isUrlValid(url) {
return (url && url.length > 3) ? true : false;
}
// send 'load/unload' request to the embedded content script
function sendInjectionRequest(tabId, tabState, callback) {
chrome.tabs.sendRequest(
tabId,
{
// state of current tab
state: tabState,
// id of <link> element to inject
id: 'cssinject-injected-cssfile',
// css file path specified by user in options plus a
// timestamp cache buster.
href: storage.cssfile + '?' + (new Date()).getTime()
},
function(resp) {
// something went wrong
if (!resp.ok) {
error('Could not load CSS file');
}
if (callback) {
callback();
}
}
);
}
// Turn on the plugin badge and inject the css
function turnOn(tabId) {
// error check
if (!isUrlValid(storage.cssfile)) {
error('No CSS url specified. Please specify url in extesion options.');
return;
}
// update state
state[tabId] = 'on';
// send request to content script
sendInjectionRequest(tabId, state[tabId], function() {
// update badge
chrome.browserAction.setBadgeText({text: 'on', tabId: tabId});
});
}
// Turn off the plugin badge
function turnOff(tabId) {
// just delete the property if it exists
delete state[tabId];
// send request to content script
sendInjectionRequest(tabId, 'off', function() {
// update badge
chrome.browserAction.setBadgeText({text: '', tabId: tabId});
});
}
// toggles css injection on/off
function toggleInjection(tab) {
// toggle state on click
if (state[tab.id] === 'on') {
turnOff(tab.id);
} else {
turnOn(tab.id);
}
}
// restore state on page reloads
function restoreState(req, sender, sendResponse) {
// first get current window
chrome.windows.getCurrent(function(win) {
// then get current tab
chrome.tabs.getSelected(win.id, function(tab) {
// check the tab's state
if (state[tab.id] === 'on') {
// if it should be on turn it on
turnOn(tab.id);
// notify content script that all is good
sendResponse({ok: true});
}
});
});
}
// EVENT HANDLERS
// User clicked the activate action button,
// kick off all the injection goodness.
chrome.browserAction.onClicked.addListener(toggleInjection);
// Handle requests from embedded content script.
chrome.extension.onRequest.addListener(restoreState);
}());