Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: mobile-options.js

Issue 29488575: Issue 5384 - Introduced dedicated mobile options page (Closed)
Patch Set: Added ID constants Created Aug. 28, 2017, 2:51 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mobile-options.html ('k') | skin/fonts/Source-Sans-Pro/300.woff2 » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* globals getDocLink */
19
20 "use strict";
21
22 {
23 const {getMessage} = ext.i18n;
24
25 const dialogSubscribe = "subscribe";
26 const idAcceptableAds = "acceptableAds";
27 const idRecommended = "subscriptions-recommended";
28 let whitelistFilter = null;
29 let promisedAcceptableAdsUrl = getAcceptableAdsUrl();
30
31 /* Utility functions */
32
33 function get(selector, origin)
34 {
35 return (origin || document).querySelector(selector);
36 }
37
38 function getAll(selector, origin)
39 {
40 return (origin || document).querySelectorAll(selector);
41 }
42
43 function create(parent, tagName, content, attributes, onclick)
44 {
45 let element = document.createElement(tagName);
46
47 if (typeof content == "string")
48 {
49 element.textContent = content;
50 }
51
52 if (attributes)
53 {
54 for (let name in attributes)
55 {
56 element.setAttribute(name, attributes[name]);
57 }
58 }
59
60 if (onclick)
61 {
62 element.addEventListener("click", (ev) =>
63 {
64 onclick(ev);
65 ev.stopPropagation();
66 });
67 }
68
69 parent.appendChild(element);
70 return element;
71 }
72
73 /* Extension interactions */
74
75 function getInstalled()
76 {
77 return new Promise((resolve, reject) =>
78 {
79 ext.backgroundPage.sendMessage(
80 {type: "subscriptions.get", downloadable: true},
81 resolve
82 );
83 });
84 }
85
86 function getAcceptableAdsUrl()
87 {
88 return new Promise((resolve, reject) =>
89 {
90 ext.backgroundPage.sendMessage(
91 {type: "prefs.get", key: "subscriptions_exceptionsurl"},
92 resolve
93 );
94 });
95 }
96
97 function getRecommendedAds()
98 {
99 return fetch("subscriptions.xml")
100 .then((response) => response.text())
101 .then((text) =>
102 {
103 let doc = new DOMParser().parseFromString(text, "application/xml");
104 let elements = Array.from(doc.getElementsByTagName("subscription"));
105
106 return elements
107 .filter((element) => element.getAttribute("type") == "ads")
108 .map((element) =>
109 {
110 return {
111 title: element.getAttribute("title"),
112 url: element.getAttribute("url")
113 };
114 });
115 });
116 }
117
118 function installSubscription(url, title)
119 {
120 ext.backgroundPage.sendMessage({type: "subscriptions.add", url, title});
121 }
122
123 function uninstallSubscription(url)
124 {
125 ext.backgroundPage.sendMessage({type: "subscriptions.remove", url});
126 }
127
128 /* Actions */
129
130 function setSubscription({disabled, title, url}, shouldAdd)
131 {
132 if (disabled)
133 return;
134
135 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
136 {
137 if (url == acceptableAdsUrl)
138 {
139 get(`#${idAcceptableAds}`).checked = true;
140 return;
141 }
142
143 let listInstalled = get("#subscriptions-installed");
144 let installed = get(`[data-url="${url}"]`, listInstalled);
145
146 if (installed)
147 {
148 let titleElement = get("span", installed);
149 titleElement.textContent = title || url;
150 }
151 else if (shouldAdd)
152 {
153 let element = create(listInstalled, "li", null, {"data-url": url});
154 create(element, "span", title || url);
155 create(element, "button", null, {class: "remove"},
156 () => uninstallSubscription(url)
157 );
158
159 let recommended = get(`#${idRecommended} [data-url="${url}"]`);
160 if (recommended)
161 {
162 recommended.classList.add("installed");
163 }
164 }
165 });
166 }
167
168 function removeSubscription(url)
169 {
170 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
171 {
172 if (url == acceptableAdsUrl)
173 {
174 get(`#${idAcceptableAds}`).checked = false;
175 return;
176 }
177
178 let installed = get(`#subscriptions-installed [data-url="${url}"]`);
179 if (installed)
180 {
181 installed.parentNode.removeChild(installed);
182 }
183
184 let recommended = get(`#${idRecommended} [data-url="${url}"]`);
185 if (recommended)
186 {
187 recommended.classList.remove("installed");
188 }
189 });
190 }
191
192 function setDialog(id, options)
193 {
194 if (!id)
195 {
196 delete document.body.dataset.dialog;
197 return;
198 }
199
200 let fields = getAll(`#dialog-${id} input`);
201 for (let field of fields)
202 {
203 let {name} = field;
204 field.value = (options && name in options) ? options[name] : "";
205 }
206 setError(id, null);
207
208 document.body.dataset.dialog = id;
209 }
210
211 function setError(dialogId, fieldName)
212 {
213 let dialog = get(`#dialog-${dialogId}`);
214 if (fieldName)
215 {
216 dialog.dataset.error = fieldName;
217 }
218 else
219 {
220 delete dialog.dataset.error;
221 }
222 }
223
224 function populateLists()
225 {
226 Promise.all([getInstalled(), getRecommendedAds()])
227 .then(([installed, recommended]) =>
228 {
229 let listRecommended = get(`#${idRecommended}`);
230 for (let {title, url} of recommended)
231 {
232 create(listRecommended, "li", title, {"data-url": url},
233 (ev) =>
234 {
235 if (ev.target.classList.contains("installed"))
236 return;
237
238 setDialog(dialogSubscribe, {title, url});
239 }
240 );
241 }
242
243 for (let subscription of installed)
244 {
245 if (subscription.disabled)
246 continue;
247
248 setSubscription(subscription, true);
249 }
250 })
251 .catch((err) => console.error(err));
252 }
253
254 /* Listeners */
255
256 function onChange(ev)
257 {
258 if (ev.target.id != idAcceptableAds)
259 return;
260
261 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
262 {
263 if (ev.target.checked)
264 {
265 installSubscription(acceptableAdsUrl, null);
266 }
267 else
268 {
269 uninstallSubscription(acceptableAdsUrl);
270 }
271 });
272 }
273 document.addEventListener("change", onChange);
274
275 function toggleWhitelistFilter(toggle)
276 {
277 if (whitelistFilter)
278 {
279 ext.backgroundPage.sendMessage(
280 {
281 type: (toggle.checked) ? "filters.remove" : "filters.add",
282 text: whitelistFilter
283 },
284 (errors) =>
285 {
286 if (errors.length < 1)
287 return;
288
289 console.error(errors);
290 toggle.checked = !toggle.checked;
291 }
292 );
293 }
294 else
295 {
296 console.error("Whitelist filter hasn't been initialized yet");
297 }
298 }
299
300 function onClick(ev)
301 {
302 switch (ev.target.dataset.action)
303 {
304 case "close-dialog":
305 setDialog(null);
306 break;
307 case "open-dialog":
308 setDialog(ev.target.dataset.dialog);
309 break;
310 case "toggle-enabled":
311 toggleWhitelistFilter(ev.target);
312 ev.preventDefault();
313 break;
314 }
315 }
316 document.addEventListener("click", onClick);
317
318 function onSubmit(ev)
319 {
320 let fields = ev.target.elements;
321 let title = fields.title.value;
322 let url = fields.url.value;
323
324 if (!title)
325 {
326 setError(dialogSubscribe, "title");
327 }
328 else if (!url)
329 {
330 setError(dialogSubscribe, "url");
331 }
332 else
333 {
334 installSubscription(url, title);
335 setDialog(null);
336 }
337
338 ev.preventDefault();
339 }
340 document.addEventListener("submit", onSubmit);
341
342 function onMessage(msg)
343 {
344 switch (msg.type)
345 {
346 case "app.respond": {
347 switch (msg.action)
348 {
349 case "addSubscription":
350 let [subscription] = msg.args;
351 setDialog(dialogSubscribe, {
352 title: subscription.title,
353 url: subscription.url
354 });
355 break;
356 case "showPageOptions":
357 let [{host, whitelisted}] = msg.args;
358 whitelistFilter = `@@||${host}^$document`;
359
360 ext.i18n.setElementText(
361 get("#enabled-label"),
362 "mops_enabled_label",
363 [host]
364 );
365
366 let toggle = get("#enabled");
367 toggle.checked = !whitelisted;
368
369 get("#enabled-container").hidden = false;
370 break;
371 }
372 break;
373 }
374 case "filters.respond": {
375 let [filter] = msg.args;
376 if (!whitelistFilter || filter.text != whitelistFilter)
377 break;
378
379 get("#enabled").checked = (msg.action == "removed");
380 break;
381 }
382 case "subscriptions.respond": {
383 let [subscription] = msg.args;
384 switch (msg.action)
385 {
386 case "added":
387 setSubscription(subscription, true);
388 break;
389 case "disabled":
390 if (subscription.disabled)
391 {
392 removeSubscription(subscription.url);
393 }
394 else
395 {
396 setSubscription(subscription, true);
397 }
398 break;
399 case "removed":
400 removeSubscription(subscription.url);
401 break;
402 case "title":
403 // We're also receiving these messages for subscriptions that are
404 // not installed so we shouldn't add those by accident
405 setSubscription(subscription, false);
406 break;
407 }
408 break;
409 }
410 }
411 }
412 ext.onMessage.addListener(onMessage);
413
414 ext.backgroundPage.sendMessage({
415 type: "app.listen",
416 filter: ["addSubscription", "showPageOptions"]
417 });
418
419 ext.backgroundPage.sendMessage({
420 type: "filters.listen",
421 filter: ["added", "removed"]
422 });
423
424 ext.backgroundPage.sendMessage({
425 type: "subscriptions.listen",
426 filter: ["added", "disabled", "removed", "title"]
427 });
428
429 /* Initialization */
430
431 populateLists();
432
433 getDocLink("acceptable_ads", (link) =>
434 {
435 get("#acceptableAds-more").href = link;
436 });
437
438 get("#dialog-subscribe [name='title']").setAttribute(
439 "placeholder",
440 getMessage("mops_subscribe_title")
441 );
442
443 get("#dialog-subscribe [name='url']").setAttribute(
444 "placeholder",
445 getMessage("mops_subscribe_url")
446 );
447 }
OLDNEW
« no previous file with comments | « mobile-options.html ('k') | skin/fonts/Source-Sans-Pro/300.woff2 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld