-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.js
executable file
·224 lines (200 loc) · 7.19 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
const KEY_EXCLUDE = "exclude",
KEY_INCLUDE = "include",
INCLUDE_OR_EXCLUDE_ALL = "include-all-or-exclude-all";
let radio_includeAll = document.getElementById( 'include-all' ),
radio_excludeAll = document.getElementById( 'exclude-all' ),
textarea_inclusionsList = document.getElementById( 'inclusions-list' ),
textarea_exclusionsList = document.getElementById( 'exclusions-list' ),
labelFor_inclusionsList = document.querySelectorAll('label[for="inclusions-list"]')[0],
labelFor_exclusionsList = document.querySelectorAll('label[for="exclusions-list"]')[0];
// Listeners for days
radio_includeAll.addEventListener( 'change', listener_includeAll );
radio_excludeAll.addEventListener( 'change', listener_excludeAll );
document.addEventListener( 'DOMContentLoaded', reset_options );
document.getElementById( 'save' ).addEventListener( 'click', save_options );
document.getElementById( 'reset' ).addEventListener( 'click', reset_options );
// Validate and save settings. Display user-facing errors where appropriate.
function save_options()
{
clearErrorMessages();
const REGEX_VALID_DOMAIN = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;
let onPageExclusions = textarea_exclusionsList.value.split('\n'),
onPageInclusions = textarea_inclusionsList.value.split('\n'),
validExclusionDomains = [],
validInclusionDomains = [],
invalidExclusionDomains = '',
invalidInclusionDomains = '',
includeOrExcludeState;
// Exclusion logic
for( let exclusion in onPageExclusions )
{
// Grab the current exclusion and trim the whitespace off
let currentExclusion = onPageExclusions[exclusion].trim();
// Ignore empty strings
if( currentExclusion === '' );
// Collect valid exclusion domains to save
else if( REGEX_VALID_DOMAIN.test( currentExclusion ) )
{
validExclusionDomains.push( currentExclusion );
}
// Collect invalid exclusion domains to display in error
else
{
invalidExclusionDomains += '<li>' + currentExclusion + '</li>';
}
}
// Display an error message if there's invalid exclusion domains
if( invalidExclusionDomains !== '' )
{
document.querySelectorAll('#exclusion-domain-errors ul')[0].innerHTML = invalidExclusionDomains;
let exclusionDomainErrors = document.getElementById('exclusion-domain-errors');
exclusionDomainErrors.classList.remove( 'undisplayed' );
setTimeout(function() // This allows the transition animation to play.
{
exclusionDomainErrors.classList.remove( 'hidden' );
}, 0);
}
// Inclusion logic
for( let inclusion in onPageInclusions )
{
// Grab the current inclusion and trim the whitespace off
let currentInclusion = onPageInclusions[inclusion].trim();
// Ignore empty strings and whitespace-only strings
if( currentInclusion === '' );
// Collect valid inclusion domains to save
else if( REGEX_VALID_DOMAIN.test( currentInclusion ) )
{
validInclusionDomains.push( currentInclusion );
}
// Collect invalid inclusion domains to display in error
else
{
invalidInclusionDomains += '<li>' + currentInclusion + '</li>';
}
}
// Display an error message if there's invalid inclusion domains
if( invalidInclusionDomains !== '' )
{
document.querySelectorAll('#inclusion-domain-errors ul')[0].innerHTML = invalidInclusionDomains;
let inclusionDomainErrors = document.getElementById('inclusion-domain-errors');
inclusionDomainErrors.classList.remove( 'undisplayed' );
setTimeout(function() // This allows the transition animation to play.
{
inclusionDomainErrors.classList.remove( 'hidden' );
}, 0);
// document.getElementById('inclusion-domain-errors').style.display = 'block';
}
if( radio_includeAll.checked )
{
includeOrExcludeState = KEY_INCLUDE;
}
else if( radio_excludeAll.checked )
{
includeOrExcludeState = KEY_EXCLUDE;
}
else // default to 'include'
{
includeOrExcludeState = KEY_INCLUDE;
}
// Save valid settings to storage
chrome.storage.sync.set(
{
'includeList': validInclusionDomains,
'excludeList': validExclusionDomains,
[INCLUDE_OR_EXCLUDE_ALL]: includeOrExcludeState
}, handleErrors);
}
// Reset dom with settings saved in storage
function reset_options()
{
clearErrorMessages();
textarea_exclusionsList.value = "";
textarea_inclusionsList.value = "";
chrome.storage.sync.get( null, function( storageItems )
{
for( let storageItemKey in storageItems )
{
// Grab the value of the current storageItemKey
let storageItemValue = storageItems[ storageItemKey ];
if( storageItemKey === INCLUDE_OR_EXCLUDE_ALL )
{
if( storageItemValue === KEY_EXCLUDE )
{
radio_includeAll.checked = false;
radio_excludeAll.checked = true;
set_excludeAll();
}
else if( storageItemValue === KEY_INCLUDE )
{
radio_includeAll.checked = true;
radio_excludeAll.checked = false;
set_includeAll();
}
}
else if( storageItemKey === "excludeList" )
{
// Move through the list of domains
for( let domain in storageItemValue )
{
textarea_exclusionsList.value += storageItemValue[domain] + "\n";
}
}
else if( storageItemKey === "includeList" )
{
for( let domain in storageItemValue )
{
// Move through the list of domains
textarea_inclusionsList.value += storageItemValue[domain] + "\n";
}
}
}
} );
}
function listener_includeAll( event )
{
if( event.target.checked === true )
{
set_includeAll();
}
}
function listener_excludeAll( event )
{
if( event.target.checked === true )
{
set_excludeAll();
}
}
function set_includeAll()
{
textarea_inclusionsList.disabled = true;
textarea_exclusionsList.disabled = false;
labelFor_exclusionsList.className = "";
labelFor_inclusionsList.className = "disabled";
}
function set_excludeAll()
{
textarea_inclusionsList.disabled = false;
textarea_exclusionsList.disabled = true;
labelFor_inclusionsList.className = "";
labelFor_exclusionsList.className = "disabled";
}
function clearErrorMessages()
{
let exclusionDomainErrors = document.getElementById('exclusion-domain-errors'),
inclusionDomainErrors = document.getElementById('inclusion-domain-errors');
// Get rid of any lingering error messages
document.querySelectorAll('#exclusion-domain-errors ul')[0].innerHTML = '';
exclusionDomainErrors.classList.add( 'hidden' );
exclusionDomainErrors.classList.add( 'undisplayed' );
document.querySelectorAll('#inclusion-domain-errors ul')[0].innerHTML = '';
inclusionDomainErrors.classList.add( 'hidden' );
inclusionDomainErrors.classList.add( 'undisplayed' );
}
function handleErrors()
{
if( chrome.runtime.lastError )
{
console.error( chrome.runtime.lastError.message );
}
}