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: Encapsulated mobile-options.js script Created July 18, 2017, 5:38 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/mobile-options.css » ('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-2017 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 let whitelistFilter = null;
26 let promisedAcceptableAdsUrl = getAcceptableAdsUrl();
27
28 /* Utility functions */
29
30 function get(selector, origin)
31 {
32 return (origin || document).querySelector(selector);
33 }
34
35 function getAll(selector, origin)
36 {
37 return (origin || document).querySelectorAll(selector);
38 }
39
40 function create(parent, tagName, content, attributes, onclick)
41 {
42 let element = document.createElement(tagName);
43
44 if (typeof content == "string")
45 {
46 element.textContent = content;
47 }
48
49 if (attributes)
50 {
51 for (let name in attributes)
52 {
53 element.setAttribute(name, attributes[name]);
54 }
55 }
56
57 if (onclick)
58 {
59 element.addEventListener("click", (ev) =>
60 {
61 onclick(ev);
62 ev.stopPropagation();
63 });
64 }
65
66 parent.appendChild(element);
67 return element;
68 }
69
70 /* Extension interactions */
71
72 function getInstalled()
73 {
74 return new Promise((resolve, reject) =>
75 {
76 ext.backgroundPage.sendMessage(
77 {type: "subscriptions.get", downloadable: true},
78 resolve
79 );
80 });
81 }
82
83 function getAcceptableAdsUrl()
84 {
85 return new Promise((resolve, reject) =>
86 {
87 ext.backgroundPage.sendMessage(
88 {type: "prefs.get", key: "subscriptions_exceptionsurl"},
89 resolve
90 );
91 });
92 }
93
94 function getRecommended()
95 {
96 return fetch("subscriptions.xml")
97 .then((resp) => resp.text())
98 .then((text) =>
99 {
100 let doc = new DOMParser().parseFromString(text, "application/xml");
101 let elements = Array.from(doc.getElementsByTagName("subscription"));
102
103 return elements
104 .filter((element) => element.getAttribute("type") == "ads")
105 .map((element) =>
106 {
107 return {
108 title: element.getAttribute("title"),
109 url: element.getAttribute("url")
110 };
111 });
112 });
113 }
114
115 function installSubscription(url, title)
116 {
117 ext.backgroundPage.sendMessage({type: "subscriptions.add", url, title});
118 }
119
120 function uninstallSubscription(url)
121 {
122 ext.backgroundPage.sendMessage({type: "subscriptions.remove", url});
123 }
124
125 /* Actions */
126
127 function setSubscription({disabled, title, url}, shouldAdd)
128 {
129 if (disabled)
130 return;
131
132 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
133 {
134 if (url == acceptableAdsUrl)
135 {
136 get("#acceptableAds").checked = true;
137 return;
138 }
139
140 let listInstalled = get("#subscriptions-installed");
141 let installed = get(`[data-url="${url}"]`, listInstalled);
142
143 if (installed)
144 {
145 let titleElement = get("span", installed);
146 titleElement.textContent = title || url;
147 }
148 else if (shouldAdd)
149 {
150 let element = create(listInstalled, "li", null, {"data-url": url});
151 create(element, "span", title || url);
152 create(element, "button", null, {class: "remove"},
153 () => uninstallSubscription(url)
154 );
155
156 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`);
157 if (recommended)
158 {
159 recommended.classList.add("installed");
160 }
161 }
162 });
163 }
164
165 function removeSubscription(url)
166 {
167 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
168 {
169 if (url == acceptableAdsUrl)
170 {
171 get("#acceptable-ads").checked = false;
172 return;
173 }
174
175 let installed = get(`#subscriptions-installed [data-url="${url}"]`);
176 if (installed)
177 {
178 installed.parentNode.removeChild(installed);
179 }
180
181 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`);
182 if (recommended)
183 {
184 recommended.classList.remove("installed");
185 }
186 });
187 }
188
189 function setDialog(id, options)
190 {
191 if (!id)
192 {
193 delete document.body.dataset.dialog;
194 return;
195 }
196
197 let fields = getAll(`#dialog-${id} input`);
198 for (let field of fields)
199 {
200 field.value = (options && field.name in options) ? options[field.name] : " ";
201 }
202 setError(id, null);
203
204 document.body.dataset.dialog = id;
205 }
206
207 function setError(dialogId, message)
208 {
209 let dialog = get(`#dialog-${dialogId}`);
210 if (message)
211 {
212 dialog.dataset.error = message;
213 }
214 else
215 {
216 delete dialog.dataset.error;
217 }
218 }
219
220 function populateLists()
221 {
222 Promise.all([getInstalled(), getRecommended()])
223 .then(([installed, recommended]) =>
224 {
225 let listRecommended = get("#subscriptions-recommended");
226 for (let {title, url} of recommended)
227 {
228 create(listRecommended, "li", title, {"data-url": url},
229 (ev) =>
230 {
231 if (ev.target.classList.contains("installed"))
232 return;
233
234 setDialog("subscribe", {title, url});
235 }
236 );
237 }
238
239 for (let subscription of installed)
240 {
241 if (subscription.disabled)
242 continue;
243
244 setSubscription(subscription, true);
245 }
246 })
247 .catch((err) => console.error(err));
248 }
249
250 /* Listeners */
251
252 function onChange(ev)
253 {
254 if (ev.target.id != "acceptable-ads")
255 return;
256
257 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
258 {
259 if (ev.target.checked)
260 {
261 installSubscription(acceptableAdsUrl, null);
262 }
263 else
264 {
265 uninstallSubscription(acceptableAdsUrl);
266 }
267 });
268 }
269 document.addEventListener("change", onChange);
270
271 function onClick(ev)
272 {
273 switch (ev.target.dataset.action)
274 {
275 case "close-dialog":
276 setDialog(null);
277 break;
278 case "open-dialog":
279 setDialog(ev.target.dataset.dialog);
280 break;
281 }
282 }
283 document.addEventListener("click", onClick);
284
285 function onSubmit(ev)
286 {
287 let fields = ev.target.elements;
288 let title = fields.title.value;
289 let url = fields.url.value;
290
291 if (!title)
292 {
293 setError("subscribe", "title");
294 }
295 else if (!url)
296 {
297 setError("subscribe", "url");
298 }
299 else
300 {
301 installSubscription(url, title);
302 setDialog(null);
303 }
304
305 ev.preventDefault();
306 }
307 document.addEventListener("submit", onSubmit);
308
309 function onToggleWhitelistFilter(ev)
310 {
311 let checkbox = ev.target;
312 ext.backgroundPage.sendMessage(
313 {
314 type: (checkbox.checked) ? "filters.remove" : "filters.add",
315 text: whitelistFilter
316 }, (errors) =>
317 {
318 if (errors.length < 1)
319 return;
320
321 console.error(errors);
322 checkbox.checked = !checkbox.checked;
323 }
324 );
325 ev.preventDefault();
326 }
327
328 function onMessage(msg)
329 {
330 switch (msg.type)
331 {
332 case "app.respond": {
333 switch (msg.action)
334 {
335 case "addSubscription":
336 let [subscription] = msg.args;
337 setDialog("subscribe", {
338 title: subscription.title,
339 url: subscription.url
340 });
341 break;
342 case "showPageOptions":
343 let [{host, whitelisted}] = msg.args;
344 whitelistFilter = `@@||${host}^$document`;
345
346 ext.i18n.setElementText(
347 get("#enabled-label"),
348 "mops_enabled_label",
349 [host]
350 );
351
352 let checkbox = get("#enabled");
353 checkbox.checked = !whitelisted;
354 checkbox.addEventListener("click", onToggleWhitelistFilter);
355
356 get("#enabled-container").hidden = false;
357 break;
358 }
359 break;
360 }
361 case "filters.respond": {
362 let [filter] = msg.args;
363 if (!whitelistFilter || filter.text != whitelistFilter)
364 break;
365
366 get("#enabled").checked = (msg.action == "removed");
367 break;
368 }
369 case "subscriptions.respond": {
370 let [subscription] = msg.args;
371 switch (msg.action)
372 {
373 case "added":
374 setSubscription(subscription, true);
375 break;
376 case "disabled":
377 if (subscription.disabled)
378 {
379 removeSubscription(subscription.url);
380 }
381 else
382 {
383 setSubscription(subscription, true);
384 }
385 break;
386 case "removed":
387 removeSubscription(subscription.url);
388 break;
389 case "title":
390 // We're also receiving these messages for subscriptions that are no t
391 // installed so we shouldn't add those by accident
392 setSubscription(subscription, false);
393 break;
394 }
395 break;
396 }
397 }
398 }
399 ext.onMessage.addListener(onMessage);
400
401 ext.backgroundPage.sendMessage({
402 type: "app.listen",
403 filter: ["addSubscription", "showPageOptions"]
404 });
405
406 ext.backgroundPage.sendMessage({
407 type: "filters.listen",
408 filter: ["added", "removed"]
409 });
410
411 ext.backgroundPage.sendMessage({
412 type: "subscriptions.listen",
413 filter: ["added", "disabled", "removed", "title"]
414 });
415
416 /* Initialization */
417
418 populateLists();
419
420 getDocLink("acceptable_ads", (link) =>
421 {
422 get("#acceptableAds-more").href = link;
423 });
424
425 get("#dialog-subscribe [name='title']").setAttribute(
426 "placeholder",
427 getMessage("mops_subscribe_title")
428 );
429
430 get("#dialog-subscribe [name='url']").setAttribute(
431 "placeholder",
432 getMessage("mops_subscribe_url")
433 );
434 }
OLDNEW
« no previous file with comments | « mobile-options.html ('k') | skin/mobile-options.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld