-
Notifications
You must be signed in to change notification settings - Fork 16
/
options.js
97 lines (88 loc) · 2.92 KB
/
options.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
var defaultSites = {
'The Age': 'theage.com.au',
'Baltimore Sun': 'baltimoresun.com',
'Barron\'s': 'barrons.com',
'Crain\'s Chicago Business': 'chicagobusiness.com',
'Chicago Tribune': 'chicagotribune.com',
'Daily Press': 'dailypress.com',
'The Economist': 'economist.com',
'Financial Times': 'ft.com',
'Hartford Courant': 'courant.com',
'Harvard Business Review': 'hbr.org',
'Inc.com': 'inc.com',
'Los Angeles Times': 'latimes.com',
'Medscape': 'medscape.com',
'The Morning Call': 'mcall.com',
'The Nation': 'thenation.com',
'The New Yorker': 'newyorker.com',
'OrlandoSentinel': 'orlandosentinel.com',
'Quora': 'quora.com',
'SunSentinel': 'sun-sentinel.com',
'The Sydney Morning Herald': 'smh.com.au',
'The Telegraph': 'telegraph.co.uk',
'The Washington Post': 'washingtonpost.com',
'The Wall Street Journal': 'wsj.com'
};
// Saves options to chrome.storage
function save_options() {
var gh_url = document.getElementById('bypass_sites').value;
var inputEls = document.querySelectorAll('#bypass_sites input');
var sites = {};
var sites = Array.from(inputEls).reduce(function(memo, inputEl) {
if (inputEl.checked) {
memo[inputEl.dataset.key] = inputEl.dataset.value;
}
return memo;
}, {});
chrome.storage.sync.set({
sites: sites
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
// Restores checkbox input states using the preferences
// stored in chrome.storage.
function renderOptions() {
chrome.storage.sync.get({
sites: {}
}, function(items) {
var sites = items.sites;
var sitesEl = document.getElementById('bypass_sites');
for (var key in defaultSites) {
if (!defaultSites.hasOwnProperty(key)) {
continue;
}
var value = defaultSites[key];
var labelEl = document.createElement('label');
var inputEl = document.createElement('input');
inputEl.type = 'checkbox';
inputEl.dataset.key = key;
inputEl.dataset.value = value;
inputEl.checked = key in sites;
labelEl.appendChild(inputEl);
labelEl.appendChild(document.createTextNode(' '+key));
sitesEl.appendChild(labelEl);
}
});
}
function selectAll() {
var inputEls = Array.from(document.querySelectorAll('input'));
inputEls.forEach(function(inputEl) {
inputEl.checked = true;
});
}
function selectNone() {
var inputEls = Array.from(document.querySelectorAll('input'));
inputEls.forEach(function(inputEl) {
inputEl.checked = false;
});
}
document.addEventListener('DOMContentLoaded', renderOptions);
document.getElementById('save').addEventListener('click', save_options);
document.getElementById('select-all').addEventListener('click', selectAll);
document.getElementById('select-none').addEventListener('click', selectNone);