-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.js
93 lines (82 loc) · 3.19 KB
/
background.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
!function (global) {
// Ensure that the default theme is set, defaulting to Dark.
if (['dark', 'light'].indexOf(global.localStorage.getItem('theme')) === -1) {
global.localStorage.setItem('theme', 'dark');
}
// Add some other default values to be on the safe side.
if (!global.localStorage.getItem('exceptions')) {
global.localStorage.setItem('exceptions', '');
}
if (global.localStorage.getItem('darken') !== 'false') {
global.localStorage.setItem('darken', true);
}
// Chrome extensions don't currently let you listen to the extension button
// from content scripts, so our background script acts as a dispatcher to
// the active tab.
function toggleClient (tab, skipExclusion) {
var cb = skipExclusion
? function (response) {response && setIcon(response.isDark);}
: handleManualToggle;
chrome.tabs.sendMessage(tab.id, {type: 'com.rileyjshaw.dte__TOGGLE'}, cb);
}
chrome.browserAction.onClicked.addListener(toggleClient);
// If the toggle button is clicked manually, we add or remove the active
// site's subdomain to/from the exceptions list. This ensures that the
// toggled state persists on future visits to the page.
function handleManualToggle (response) {
var url = response.url;
var isDark = response.isDark;
// Strip the protocol and trailing slashes.
var toStrip = /(?:.*:\/\/)?(?:www\.)?(.*?)\/*$/;
var exceptions = getExceptions();
var newExceptions = isException(url, exceptions)
? exceptions.filter(function (exception) {
return exception.replace(toStrip, '$1') !== url.replace(toStrip, '$1');
})
: exceptions.concat(url);
global.localStorage.setItem('exceptions', newExceptions.join('\n'));
setIcon(isDark);
}
// The active tab will, in turn, let the background script know when it has
// loaded new content so that we can re-initialize the tab.
chrome.runtime.onMessage.addListener(
function (request, sender) {
// Early exit if the message isn't coming from a content script.
var tab = sender.tab;
if (request.type !== 'com.rileyjshaw.dte__READY' || !tab) {return;}
var theme = global.localStorage.getItem('theme');
var darken = global.localStorage.getItem('darken') === 'true';
// XOR
var isDark = isException(sender.url) !== (theme === 'dark');
setIcon(isDark);
if (!isDark) {toggleClient(tab, true);}
if (!darken) {
chrome.tabs.sendMessage(
tab.id, {type: 'com.rileyjshaw.dte__REMOVE_MEDIA_FILTERS'});
}
}
);
// Returns a formatted list of exception URLs.
function getExceptions () {
return global.localStorage.getItem('exceptions')
// Remove spaces.
.replace(/ /g, '')
// Split on commas or newlines.
.split(/,|\n/)
// Remove blank lines.
.filter(function (exception) {return exception;})
;
}
// Accepts a URL and an optional exceptions list. Returns true if any of the
// list's fragments match the URL.
function isException (url, exceptions) {
return (exceptions || getExceptions()).some(function (exception) {
return url.search(exception) !== -1;
});
}
// Helper function to set the browser action icon.
function setIcon (isDark) {
var file = 'icon38' + (isDark ? '' : '-light') + '.png';
chrome.browserAction.setIcon({path: chrome.extension.getURL(file)});
}
}(this);