Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: ui/accessibility/extensions/colorenhancer/src/common.js

Issue 984833004: Add color enhancer as a chromium accessibility extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add docs and TODOs. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // TODO(wnwen): Move most of these functions to their own page rather than
6 // common, which should be shared with content script.
7
8 // TODO(wnwen): Revamp storage, move to chrome.storage.local, wrap calls, and
9 // add JsDocs.
10
11 /**
12 * TODO(wnwen): Remove this and use actual web API.
13 */
14 function $(id) {
15 return document.getElementById(id);
16 }
17
18
19 /**
20 * TODO(wnwen): Remove this, it's terrible.
21 */
22 function siteFromUrl(url) {
23 var a = document.createElement('a');
24 a.href = url;
25 return a.hostname;
26 }
27
28 /**
29 * The filter should not apply to these URLs.
kevers 2015/03/10 14:07:51 @param
Peter Wen 2015/03/10 19:00:55 Done.
30 */
31 function isDisallowedUrl(url) {
32 return url.indexOf('chrome') == 0 || url.indexOf('about') == 0;
33 }
34
35
36 var DEFAULT_DELTA = 1.0;
37 var LOCAL_STORAGE_TAG_DELTA = 'cvd_delta';
38 var LOCAL_STORAGE_TAG_SITE_DELTA = 'cvd_site_deltas';
39
40
41 function validDelta(delta) {
42 return delta >= 0 && delta <= 1;
kevers 2015/03/10 14:07:51 This method looks overly specialized. OK with fix
Peter Wen 2015/03/10 19:00:55 Acknowledged.
43 }
44
45
46 function getDefaultDelta() {
47 var delta = localStorage[LOCAL_STORAGE_TAG_DELTA];
48 if (validDelta(delta)) {
49 return delta;
50 }
51 delta = DEFAULT_DELTA;
52 localStorage[LOCAL_STORAGE_TAG_DELTA] = delta;
53 return delta;
54 }
55
56
57 function setDefaultDelta(delta) {
58 if (!validDelta(delta)) {
59 delta = DEFAULT_DELTA;
60 }
61 localStorage[LOCAL_STORAGE_TAG_DELTA] = delta;
62 }
63
64
65 function getSiteDelta(site) {
66 var delta = getDefaultDelta();
67 try {
68 var siteDeltas = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_DELTA]);
69 delta = siteDeltas[site];
70 if (!validDelta(delta)) {
71 delta = getDefaultDelta();
72 }
73 } catch (e) {
74 delta = getDefaultDelta();
75 }
76 return delta;
77 }
78
79
80 function setSiteDelta(site, delta) {
81 if (!validDelta(delta)) {
82 delta = getDefaultDelta();
83 }
84 var siteDeltas = {};
85 try {
86 siteDeltas = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_DELTA]);
87 } catch (e) {
88 siteDeltas = {};
89 }
90 siteDeltas[site] = delta;
91 localStorage[LOCAL_STORAGE_TAG_SITE_DELTA] = JSON.stringify(siteDeltas);
92 }
93
94
95 function resetSiteDeltas() {
96 var siteDeltas = {};
97 localStorage[LOCAL_STORAGE_TAG_SITE_DELTA] = JSON.stringify(siteDeltas);
98 }
99
100
101 // ======= Severity setting =======
102
103 var DEFAULT_SEVERITY = 1.0;
104 var LOCAL_STORAGE_TAG_SEVERITY = 'cvd_severity';
105 var LOCAL_STORAGE_TAG_SITE_SEVERITY = 'cvd_severities';
106
107
108 function validSeverity(severity) {
kevers 2015/03/10 14:07:51 See note above for validDelta.
Peter Wen 2015/03/10 19:00:55 Agreed, quite a bit of this storage code can be DR
109 return severity >= 0 && severity <= 1;
110 }
111
112
113 function getDefaultSeverity() {
114 var severity = localStorage[LOCAL_STORAGE_TAG_SEVERITY];
115 if (validSeverity(severity)) {
116 return severity;
117 }
118 severity = DEFAULT_SEVERITY;
119 localStorage[LOCAL_STORAGE_TAG_SEVERITY] = severity;
120 return severity;
121 }
122
123
124 function setDefaultSeverity(severity) {
125 if (!validSeverity(severity)) {
126 severity = DEFAULT_SEVERITY;
127 }
128 localStorage[LOCAL_STORAGE_TAG_SEVERITY] = severity;
129 }
130
131
132 // TODO(mustaq): Remove site-specific severity setting.
133 function getSiteSeverity(site) {
134 var severity = getDefaultSeverity();
135 try {
136 var siteSeverities =
137 JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY]);
138 severity = siteSeverities[site];
139 if (!validSeverity(severity)) {
140 severity = getDefaultSeverity();
141 }
142 } catch (e) {
143 severity = getDefaultSeverity();
144 }
145 return severity;
146 }
147
148
149 function setSiteSeverity(site, severity) {
150 if (!validSeverity(severity)) {
151 severity = getDefaultSeverity();
152 }
153 var siteSeverities = {};
154 try {
155 siteSeverities = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY]);
156 } catch (e) {
157 siteSeverities = {};
158 }
159 siteSeverities[site] = severity;
160 localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY] =
161 JSON.stringify(siteSeverities);
162 }
163
164
165 function resetSiteSeverities() {
166 var siteSeverities = {};
167 localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY] =
168 JSON.stringify(siteSeverities);
169 }
170
171
172 // ======= Type setting =======
173
174 var DEFAULT_TYPE = 'PROTANOMALY';
kevers 2015/03/10 14:07:51 Less error prone to use an enumeration. i.e.: /**
Peter Wen 2015/03/10 19:00:55 Agreed. Added TODO. In Chrome 42 we can do var ot
175 var LOCAL_STORAGE_TAG_TYPE = 'cvd_type';
176
177
178 function validType(type) {
179 return type === 'PROTANOMALY' ||
180 type === 'DEUTERANOMALY' ||
181 type === 'TRITANOMALY';
182 }
183
184
185 function getDefaultType() {
186 var type = localStorage[LOCAL_STORAGE_TAG_TYPE];
187 if (validType(type)) {
188 return type;
189 }
190 type = DEFAULT_TYPE;
191 localStorage[LOCAL_STORAGE_TAG_TYPE] = type;
192 return type;
193 }
194
195
196 function setDefaultType(type) {
197 if (!validType(type)) {
198 type = DEFAULT_TYPE;
199 }
200 localStorage[LOCAL_STORAGE_TAG_TYPE] = type;
201 }
202
203
204 // ======= Simulate setting =======
205
206 var DEFAULT_SIMULATE = false;
207 var LOCAL_STORAGE_TAG_SIMULATE = 'cvd_simulate';
208
209
210 function validSimulate(simulate) {
211 return simulate == true || simulate == false;
212 }
213
214
215 function getDefaultSimulate() {
216 var simulate = localStorage[LOCAL_STORAGE_TAG_SIMULATE];
217
218 //Make it a boolean if possible
219 if (simulate === 'true') {
220 simulate = true;
221 } else if (simulate === 'false') {
222 simulate = false;
223 } else {
224 simulate = 'undef';
225 }
226
227 if (validSimulate(simulate)) {
228 return simulate;
229 }
230 simulate = DEFAULT_SIMULATE;
231 localStorage[LOCAL_STORAGE_TAG_SIMULATE] = simulate;
232 return simulate;
233 }
234
235
236 function setDefaultSimulate(simulate) {
237 if (!validSimulate(simulate)) {
238 simulate = DEFAULT_SIMULATE;
239 }
240 localStorage[LOCAL_STORAGE_TAG_SIMULATE] = simulate;
241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698