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

Side by Side Diff: options.js

Issue 29339314: Issue 3870 - Rewrite legacy options page to use async messages (Closed)
Patch Set: Update for adblockplusui changes and work around around a race condition with Downloading... label Created April 7, 2016, 11:46 a.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 | « lib/notificationHelper.js ('k') | safari/ext/background.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var backgroundPage = ext.backgroundPage.getWindow(); 18 "use strict";
19 var require = backgroundPage.require;
20 19
21 with(require("filterClasses")) 20 /**
21 * Creates a wrapping function used to conveniently send a type of message.
22 *
23 * @param {Object} baseMessage The part of the message that's always sent
24 * @param {..string} paramKeys Any message keys that have dynamic values. The
25 * returned function will take the corresponding
26 * values as arguments.
27 * @return The generated messaging function, optionally taking any values as
28 * specified by the paramKeys and finally an optional callback.
29 * (Although the value arguments are optional their index must be
30 * maintained. E.g. if you omit the first value you must omit the
31 * second too.)
32 */
33 function wrapper(baseMessage /* , [paramKeys] */)
22 { 34 {
23 this.Filter = Filter; 35 var paramKeys = [];
24 this.WhitelistFilter = WhitelistFilter; 36 for (var i = 1; i < arguments.length; i++)
37 paramKeys.push(arguments[i]);
38
39 return function(/* [paramValues], callback */)
40 {
41 var message = Object.create(null);
42 for (var key in baseMessage)
43 if (baseMessage.hasOwnProperty(key))
44 message[key] = baseMessage[key];
45
46 var paramValues = [];
47 var callback;
48
49 if (arguments.length > 0)
50 {
51 var lastArg = arguments[arguments.length - 1];
52 if (typeof lastArg == "function")
53 callback = lastArg;
54
55 for (var i = 0; i < arguments.length - (callback ? 1 : 0); i++)
56 message[paramKeys[i]] = arguments[i];
57 }
58
59 ext.backgroundPage.sendMessage(message, callback);
60 };
25 } 61 }
26 with(require("subscriptionClasses"))
27 {
28 this.Subscription = Subscription;
29 this.SpecialSubscription = SpecialSubscription;
30 this.DownloadableSubscription = DownloadableSubscription;
31 }
32 with(require("filterValidation"))
33 {
34 this.parseFilter = parseFilter;
35 this.parseFilters = parseFilters;
36 }
37 var FilterStorage = require("filterStorage").FilterStorage;
38 var FilterNotifier = require("filterNotifier").FilterNotifier;
39 var Prefs = require("prefs").Prefs;
40 var Synchronizer = require("synchronizer").Synchronizer;
41 var Utils = require("utils").Utils;
42 var NotificationStorage = require("notification").Notification;
43 var info = require("info");
44 62
45 // Loads options from localStorage and sets UI elements accordingly 63 var getDocLink = wrapper({type: "app.get", what: "doclink"}, "link");
64 var getInfo = wrapper({type: "app.get"}, "what");
65 var getPref = wrapper({type: "prefs.get"}, "key");
66 var togglePref = wrapper({type: "prefs.toggle"}, "key");
67 var getSubscriptions = wrapper({type: "subscriptions.get"},
68 "downloadable", "special");
69 var removeSubscription = wrapper({type: "subscriptions.remove"}, "url");
70 var addSubscription = wrapper({type: "subscriptions.add"},
71 "url", "title", "homepage");
72 var toggleSubscription = wrapper({type: "subscriptions.toggle"},
73 "url", "keepInstalled");
74 var updateSubscription = wrapper({type: "subscriptions.update"}, "url");
75 var importRawFilters = wrapper({type: "filters.importRaw"},
76 "text", "removeExisting");
77 var addFilter = wrapper({type: "filters.add"}, "text");
78 var getFilters = wrapper({type: "filters.get"}, "subscriptionUrl");
79 var removeFilter = wrapper({type: "filters.remove"}, "text");
80
81 var i18n = ext.i18n;
82 var whitelistedDomainRegexp = /^@@\|\|([^\/:]+)\^\$document$/;
83 var delayedSubscriptionSelection = null;
84
85 // Loads options and sets UI elements accordingly
46 function loadOptions() 86 function loadOptions()
47 { 87 {
48 // Set page title to i18n version of "Adblock Plus Options" 88 // Set page title to i18n version of "Adblock Plus Options"
49 document.title = i18n.getMessage("options"); 89 document.title = i18n.getMessage("options");
50 90
51 // Set links 91 // Set links
52 $("#acceptableAdsLink").attr("href", Prefs.subscriptions_exceptionsurl); 92 getPref("subscriptions_exceptionsurl", function(url)
53 $("#acceptableAdsDocs").attr("href", Utils.getDocLink("acceptable_ads")); 93 {
54 setLinks("filter-must-follow-syntax", Utils.getDocLink("filterdoc")); 94 $("#acceptableAdsLink").attr("href", url);
55 setLinks("found-a-bug", Utils.getDocLink(info.application + "_support")); 95 });
96 getDocLink("acceptable_ads", function(url)
97 {
98 $("#acceptableAdsDocs").attr("href", url);
99 });
100 getDocLink("filterdoc", function(url)
101 {
102 setLinks("filter-must-follow-syntax", url);
103 });
104 getInfo("application", function(application)
105 {
106 getInfo("platform", function(platform)
107 {
108 if (platform == "chromium" && application != "opera")
109 application = "chrome";
110
111 getDocLink(application + "_support", function(url)
112 {
113 setLinks("found-a-bug", url);
114 });
115 });
116 });
56 117
57 // Add event listeners 118 // Add event listeners
58 window.addEventListener("unload", unloadOptions, false);
59 $("#updateFilterLists").click(updateFilterLists); 119 $("#updateFilterLists").click(updateFilterLists);
60 $("#startSubscriptionSelection").click(startSubscriptionSelection); 120 $("#startSubscriptionSelection").click(startSubscriptionSelection);
61 $("#subscriptionSelector").change(updateSubscriptionSelection); 121 $("#subscriptionSelector").change(updateSubscriptionSelection);
62 $("#addSubscription").click(addSubscription); 122 $("#addSubscription").click(addSubscriptionClicked);
63 $("#acceptableAds").click(allowAcceptableAds); 123 $("#acceptableAds").click(toggleAcceptableAds);
64 $("#whitelistForm").submit(addWhitelistDomain); 124 $("#whitelistForm").submit(addWhitelistDomain);
65 $("#removeWhitelist").click(removeSelectedExcludedDomain); 125 $("#removeWhitelist").click(removeSelectedExcludedDomain);
66 $("#customFilterForm").submit(addTypedFilter); 126 $("#customFilterForm").submit(addTypedFilter);
67 $("#removeCustomFilter").click(removeSelectedFilters); 127 $("#removeCustomFilter").click(removeSelectedFilters);
68 $("#rawFiltersButton").click(toggleFiltersInRawFormat); 128 $("#rawFiltersButton").click(toggleFiltersInRawFormat);
69 $("#importRawFilters").click(importRawFiltersText); 129 $("#importRawFilters").click(importRawFiltersText);
70 130
71 FilterNotifier.on("load", reloadFilters);
72 FilterNotifier.on("subscription.title", onSubscriptionChange);
73 FilterNotifier.on("subscription.disabled", onSubscriptionChange);
74 FilterNotifier.on("subscription.homepage", onSubscriptionChange);
75 FilterNotifier.on("subscription.lastDownload", onSubscriptionChange);
76 FilterNotifier.on("subscription.downloadStatus", onSubscriptionChange);
77 FilterNotifier.on("subscription.added", onSubscriptionAdded);
78 FilterNotifier.on("subscription.removed", onSubscriptionRemoved);
79 FilterNotifier.on("filter.added", onFilterAdded);
80 FilterNotifier.on("filter.removed", onFilterRemoved);
81
82 // Display jQuery UI elements 131 // Display jQuery UI elements
83 $("#tabs").tabs(); 132 $("#tabs").tabs();
84 $("button").button(); 133 $("button").button();
85 $(".refreshButton").button("option", "icons", {primary: "ui-icon-refresh"}); 134 $(".refreshButton").button("option", "icons", {primary: "ui-icon-refresh"});
86 $(".addButton").button("option", "icons", {primary: "ui-icon-plus"}); 135 $(".addButton").button("option", "icons", {primary: "ui-icon-plus"});
87 $(".removeButton").button("option", "icons", {primary: "ui-icon-minus"}); 136 $(".removeButton").button("option", "icons", {primary: "ui-icon-minus"});
88 137
89 // Popuplate option checkboxes 138 // Popuplate option checkboxes
90 initCheckbox("shouldShowBlockElementMenu"); 139 initCheckbox("shouldShowBlockElementMenu");
91 initCheckbox("show_devtools_panel"); 140 initCheckbox("show_devtools_panel");
92 initCheckbox("shouldShowNotifications", { 141 initCheckbox("shouldShowNotifications", {
93 get: function() 142 key: "notifications_ignoredcategories",
143 get: function(ignoredcategories)
94 { 144 {
95 return Prefs.notifications_ignoredcategories.indexOf("*") == -1; 145 return ignoredcategories.indexOf("*") == -1;
96 },
97 toggle: function()
98 {
99 NotificationStorage.toggleIgnoreCategory("*");
100 return this.get();
101 } 146 }
102 }); 147 });
103 148
104 if (info.platform != "chromium") 149 getInfo("features", function(features)
105 document.getElementById("showDevtoolsPanelContainer").hidden = true; 150 {
106 if (!Prefs.notifications_showui) 151 if (!features.devToolsPanel)
107 document.getElementById("shouldShowNotificationsContainer").hidden = true; 152 document.getElementById("showDevtoolsPanelContainer").hidden = true;
153 });
154 getPref("notifications_showui", function(notifications_showui)
155 {
156 if (!notifications_showui)
157 document.getElementById("shouldShowNotificationsContainer").hidden = true;
158 });
108 159
109 ext.onMessage.addListener(onMessage); 160 // Register listeners in the background message responder
110 ext.backgroundPage.sendMessage({ 161 ext.backgroundPage.sendMessage({
111 type: "app.listen", 162 type: "app.listen",
112 filter: ["addSubscription"] 163 filter: ["addSubscription", "focusSection"]
164 });
165 ext.backgroundPage.sendMessage(
166 {
167 type: "filters.listen",
168 filter: ["added", "loaded", "removed"]
169 });
170 ext.backgroundPage.sendMessage(
171 {
172 type: "prefs.listen",
173 filter: ["notifications_ignoredcategories", "notifications_showui",
174 "safari_contentblocker", "show_devtools_panel",
175 "shouldShowBlockElementMenu"]
176 });
177 ext.backgroundPage.sendMessage(
178 {
179 type: "subscriptions.listen",
180 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
181 "title", "downloadStatus", "downloading"]
113 }); 182 });
114 183
115 // Load recommended subscriptions 184 // Load recommended subscriptions
116 loadRecommendations(); 185 loadRecommendations();
117 186
118 // Show user's filters 187 // Show user's filters
119 reloadFilters(); 188 reloadFilters();
120 } 189 }
121 $(loadOptions); 190 $(loadOptions);
122 191
123 function onMessage(msg)
124 {
125 if (msg.type == "app.listen")
126 {
127 if (msg.action == "addSubscription")
128 {
129 var subscription = msg.args[0];
130 startSubscriptionSelection(subscription.title, subscription.url);
131 }
132 }
133 else if (msg.type == "focus-section")
134 {
135 var tabs = document.getElementsByClassName("ui-tabs-panel");
136 for (var i = 0; i < tabs.length; i++)
137 {
138 var found = tabs[i].querySelector("[data-section='" + msg.section + "']");
139 if (!found)
140 continue;
141
142 var previous = document.getElementsByClassName("focused");
143 if (previous.length > 0)
144 previous[0].classList.remove("focused");
145
146 var tab = $("[href='#" + tabs[i].id + "']");
147 $("#tabs").tabs("select", tab.parent().index());
148 found.classList.add("focused");
149 }
150 }
151 };
152
153 // Reloads the displayed subscriptions and filters 192 // Reloads the displayed subscriptions and filters
154 function reloadFilters() 193 function reloadFilters()
155 { 194 {
156 // Load user filter URLs 195 // Load user filter URLs
157 var container = document.getElementById("filterLists"); 196 var container = document.getElementById("filterLists");
158 while (container.lastChild) 197 while (container.lastChild)
159 container.removeChild(container.lastChild); 198 container.removeChild(container.lastChild);
160 199
161 var hasAcceptable = false; 200 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl)
162 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
163 { 201 {
164 var subscription = FilterStorage.subscriptions[i]; 202 getSubscriptions(true, false, function(subscriptions)
165 if (subscription instanceof SpecialSubscription)
166 continue;
167
168 if (subscription.url == Prefs.subscriptions_exceptionsurl)
169 { 203 {
170 hasAcceptable = true; 204 for (var i = 0; i < subscriptions.length; i++)
171 continue; 205 {
172 } 206 var subscription = subscriptions[i];
173 207 if (subscription.url == acceptableAdsUrl)
174 addSubscriptionEntry(subscription); 208 $("#acceptableAds").prop("checked", !subscription.disabled);
175 } 209 else
176 210 addSubscriptionEntry(subscription);
177 $("#acceptableAds").prop("checked", hasAcceptable); 211 }
212 });
213 });
178 214
179 // User-entered filters 215 // User-entered filters
180 var userFilters = backgroundPage.getUserFilters(); 216 getSubscriptions(false, true, function(subscriptions)
181 populateList("userFiltersBox", userFilters.filters); 217 {
182 populateList("excludedDomainsBox", userFilters.exceptions); 218 clearListBox("userFiltersBox");
183 } 219 clearListBox("excludedDomainsBox");
184 220
185 // Cleans up when the options window is closed 221 for (var i = 0; i < subscriptions.length; i++)
186 function unloadOptions() 222 getFilters(subscriptions[i].url, function(filters)
187 { 223 {
188 FilterNotifier.off("load", reloadFilters); 224 for (var j = 0; j < filters.length; j++)
189 FilterNotifier.off("subscription.title", onSubscriptionChange); 225 {
190 FilterNotifier.off("subscription.disabled", onSubscriptionChange); 226 var filter = filters[j].text;
191 FilterNotifier.off("subscription.homepage", onSubscriptionChange); 227 if (whitelistedDomainRegexp.test(filter))
192 FilterNotifier.off("subscription.lastDownload", onSubscriptionChange); 228 appendToListBox("excludedDomainsBox", RegExp.$1);
193 FilterNotifier.off("subscription.downloadStatus", onSubscriptionChange); 229 else
194 FilterNotifier.off("subscription.added", onSubscriptionAdded); 230 appendToListBox("userFiltersBox", filter);
195 FilterNotifier.off("subscription.removed", onSubscriptionRemoved); 231 }
196 FilterNotifier.off("filter.added", onFilterAdded); 232 });
197 FilterNotifier.off("filter.removed", onFilterRemoved); 233 });
198 } 234 }
199 235
200 function initCheckbox(id, descriptor) 236 function initCheckbox(id, descriptor)
201 { 237 {
202 var checkbox = document.getElementById(id); 238 var checkbox = document.getElementById(id);
203 if (descriptor && descriptor.get) 239 var key = descriptor && descriptor.key || id;
204 checkbox.checked = descriptor.get(); 240 getPref(key, function(value)
205 else 241 {
206 checkbox.checked = Prefs[id]; 242 if (descriptor && descriptor.get)
243 checkbox.checked = descriptor.get(value);
244 else
245 checkbox.checked = value;
246 });
207 247
208 checkbox.addEventListener("click", function() 248 checkbox.addEventListener("click", function()
209 { 249 {
210 if (descriptor && descriptor.toggle) 250 if (descriptor && descriptor.toggle)
211 checkbox.checked = descriptor.toggle(); 251 checkbox.checked = descriptor.toggle();
212 252 togglePref(key);
213 Prefs[id] = checkbox.checked;
214 }, false); 253 }, false);
215 } 254 }
216 255
217 var delayedSubscriptionSelection = null;
218
219 function loadRecommendations() 256 function loadRecommendations()
220 { 257 {
221 fetch("subscriptions.xml") 258 fetch("subscriptions.xml")
222 .then(function(response) 259 .then(function(response)
223 { 260 {
224 return response.text(); 261 return response.text();
225 }) 262 })
226 .then(function(text) 263 .then(function(text)
227 { 264 {
228 var selectedIndex = 0; 265 var selectedIndex = 0;
229 var selectedPrefix = null; 266 var selectedPrefix = null;
230 var matchCount = 0; 267 var matchCount = 0;
231 268
232 var list = document.getElementById("subscriptionSelector"); 269 var list = document.getElementById("subscriptionSelector");
233 var doc = new DOMParser().parseFromString(text, "application/xml"); 270 var doc = new DOMParser().parseFromString(text, "application/xml");
234 var elements = doc.documentElement.getElementsByTagName("subscription"); 271 var elements = doc.documentElement.getElementsByTagName("subscription");
235 272
236 for (var i = 0; i < elements.length; i++) 273 for (var i = 0; i < elements.length; i++)
237 { 274 {
238 var element = elements[i]; 275 var element = elements[i];
239 var option = new Option(); 276 var option = new Option();
240 option.text = element.getAttribute("title") + " (" + 277 option.text = element.getAttribute("title") + " (" +
241 element.getAttribute("specialization") + ")"; 278 element.getAttribute("specialization") + ")";
242 option._data = { 279 option._data = {
243 title: element.getAttribute("title"), 280 title: element.getAttribute("title"),
244 url: element.getAttribute("url"), 281 url: element.getAttribute("url"),
245 homepage: element.getAttribute("homepage") 282 homepage: element.getAttribute("homepage")
246 }; 283 };
247 284
248 var prefixes = element.getAttribute("prefixes"); 285 var prefix = element.getAttribute("prefixes");
249 var prefix = Utils.checkLocalePrefixMatch(prefixes);
250 if (prefix) 286 if (prefix)
251 { 287 {
288 prefix = prefix.replace(/\W/g, "_");
252 option.style.fontWeight = "bold"; 289 option.style.fontWeight = "bold";
253 option.style.backgroundColor = "#E0FFE0"; 290 option.style.backgroundColor = "#E0FFE0";
254 option.style.color = "#000000"; 291 option.style.color = "#000000";
255 if (!selectedPrefix || selectedPrefix.length < prefix.length) 292 if (!selectedPrefix || selectedPrefix.length < prefix.length)
256 { 293 {
257 selectedIndex = i; 294 selectedIndex = i;
258 selectedPrefix = prefix; 295 selectedPrefix = prefix;
259 matchCount = 1; 296 matchCount = 1;
260 } 297 }
261 else if (selectedPrefix && selectedPrefix.length == prefix.length) 298 else if (selectedPrefix && selectedPrefix.length == prefix.length)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 var data = list.options[list.selectedIndex]._data; 355 var data = list.options[list.selectedIndex]._data;
319 if (data) 356 if (data)
320 $("#customSubscriptionContainer").hide(); 357 $("#customSubscriptionContainer").hide();
321 else 358 else
322 { 359 {
323 $("#customSubscriptionContainer").show(); 360 $("#customSubscriptionContainer").show();
324 $("#customSubscriptionTitle").focus(); 361 $("#customSubscriptionTitle").focus();
325 } 362 }
326 } 363 }
327 364
328 function addSubscription() 365 function addSubscriptionClicked()
329 { 366 {
330 var list = document.getElementById("subscriptionSelector"); 367 var list = document.getElementById("subscriptionSelector");
331 var data = list.options[list.selectedIndex]._data; 368 var data = list.options[list.selectedIndex]._data;
332 if (data) 369 if (data)
333 doAddSubscription(data.url, data.title, data.homepage); 370 addSubscription(data.url, data.title, data.homepage);
334 else 371 else
335 { 372 {
336 var url = document.getElementById("customSubscriptionLocation").value.trim() ; 373 var url = document.getElementById("customSubscriptionLocation").value.trim() ;
337 if (!/^https?:/i.test(url)) 374 if (!/^https?:/i.test(url))
338 { 375 {
339 alert(i18n.getMessage("global_subscription_invalid_location")); 376 alert(i18n.getMessage("global_subscription_invalid_location"));
340 $("#customSubscriptionLocation").focus(); 377 $("#customSubscriptionLocation").focus();
341 return; 378 return;
342 } 379 }
343 380
344 var title = document.getElementById("customSubscriptionTitle").value.trim(); 381 var title = document.getElementById("customSubscriptionTitle").value.trim();
345 if (!title) 382 if (!title)
346 title = url; 383 title = url;
347 384
348 doAddSubscription(url, title, null); 385 addSubscription(url, title, null);
349 } 386 }
350 387
351 $("#addSubscriptionContainer").hide(); 388 $("#addSubscriptionContainer").hide();
352 $("#customSubscriptionContainer").hide(); 389 $("#customSubscriptionContainer").hide();
353 $("#addSubscriptionButton").show(); 390 $("#addSubscriptionButton").show();
354 } 391 }
355 392
356 function doAddSubscription(url, title, homepage) 393 function toggleAcceptableAds()
357 { 394 {
358 if (url in FilterStorage.knownSubscriptions) 395 getPref("subscriptions_exceptionsurl", function(url)
359 return;
360
361 var subscription = Subscription.fromURL(url);
362 if (!subscription)
363 return;
364
365 subscription.title = title;
366 if (homepage)
367 subscription.homepage = homepage;
368 FilterStorage.addSubscription(subscription);
369
370 if (subscription instanceof DownloadableSubscription && !subscription.lastDown load)
371 Synchronizer.execute(subscription);
372 }
373
374 function allowAcceptableAds(event)
375 {
376 var subscription = Subscription.fromURL(Prefs.subscriptions_exceptionsurl);
377 if (!subscription)
378 return;
379
380 subscription.disabled = false;
381 subscription.title = "Allow non-intrusive advertising";
382 if ($("#acceptableAds").prop("checked"))
383 { 396 {
384 FilterStorage.addSubscription(subscription); 397 toggleSubscription(url, true);
385 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload) 398 });
386 Synchronizer.execute(subscription);
387 }
388 else
389 FilterStorage.removeSubscription(subscription);
390 } 399 }
391 400
392 function findSubscriptionElement(subscription) 401 function findSubscriptionElement(subscription)
393 { 402 {
394 var children = document.getElementById("filterLists").childNodes; 403 var children = document.getElementById("filterLists").childNodes;
395 for (var i = 0; i < children.length; i++) 404 for (var i = 0; i < children.length; i++)
396 if (children[i]._subscription == subscription) 405 if (children[i]._subscription.url == subscription.url)
397 return children[i]; 406 return children[i];
398 return null; 407 return null;
399 } 408 }
400 409
401 function updateSubscriptionInfo(element) 410 function updateSubscriptionInfo(element, subscription)
402 { 411 {
403 var subscription = element._subscription; 412 if (subscription)
413 element._subscription = subscription;
414 else
415 subscription = element._subscription;
404 416
405 var title = element.getElementsByClassName("subscriptionTitle")[0]; 417 var title = element.getElementsByClassName("subscriptionTitle")[0];
406 title.textContent = subscription.title; 418 title.textContent = subscription.title;
407 title.setAttribute("title", subscription.url); 419 title.setAttribute("title", subscription.url);
408 if (subscription.homepage) 420 if (subscription.homepage)
409 title.href = subscription.homepage; 421 title.href = subscription.homepage;
410 else 422 else
411 title.href = subscription.url; 423 title.href = subscription.url;
412 424
413 var enabled = element.getElementsByClassName("subscriptionEnabled")[0]; 425 var enabled = element.getElementsByClassName("subscriptionEnabled")[0];
414 enabled.checked = !subscription.disabled; 426 enabled.checked = !subscription.disabled;
415 427
416 var lastUpdate = element.getElementsByClassName("subscriptionUpdate")[0]; 428 var lastUpdate = element.getElementsByClassName("subscriptionUpdate")[0];
417 lastUpdate.classList.remove("error"); 429 lastUpdate.classList.remove("error");
418 if (Synchronizer.isExecuting(subscription.url)) 430
431 var downloadStatus = subscription.downloadStatus;
432 if (subscription.isDownloading)
433 {
419 lastUpdate.textContent = i18n.getMessage("filters_subscription_lastDownload_ inProgress"); 434 lastUpdate.textContent = i18n.getMessage("filters_subscription_lastDownload_ inProgress");
420 else if (subscription.downloadStatus && subscription.downloadStatus != "synchr onize_ok") 435 }
436 else if (downloadStatus && downloadStatus != "synchronize_ok")
421 { 437 {
422 var map = 438 var map =
423 { 439 {
424 "synchronize_invalid_url": "filters_subscription_lastDownload_invalidURL", 440 "synchronize_invalid_url": "filters_subscription_lastDownload_invalidURL" ,
425 "synchronize_connection_error": "filters_subscription_lastDownload_connect ionError", 441 "synchronize_connection_error": "filters_subscription_lastDownload_connec tionError",
426 "synchronize_invalid_data": "filters_subscription_lastDownload_invalidData ", 442 "synchronize_invalid_data": "filters_subscription_lastDownload_invalidDat a",
427 "synchronize_checksum_mismatch": "filters_subscription_lastDownload_checks umMismatch" 443 "synchronize_checksum_mismatch": "filters_subscription_lastDownload_check sumMismatch"
428 }; 444 };
429 if (subscription.downloadStatus in map) 445 if (downloadStatus in map)
430 lastUpdate.textContent = i18n.getMessage(map[subscription.downloadStatus]) ; 446 lastUpdate.textContent = i18n.getMessage(map[downloadStatus]);
431 else 447 else
432 lastUpdate.textContent = subscription.downloadStatus; 448 lastUpdate.textContent = downloadStatus;
433 lastUpdate.classList.add("error"); 449 lastUpdate.classList.add("error");
434 } 450 }
435 else if (subscription.lastDownload > 0) 451 else if (subscription.lastDownload > 0)
436 { 452 {
437 var timeDate = i18n_timeDateStrings(subscription.lastDownload * 1000); 453 var timeDate = i18n_timeDateStrings(subscription.lastDownload * 1000);
438 var messageID = (timeDate[1] ? "last_updated_at" : "last_updated_at_today"); 454 var messageID = (timeDate[1] ? "last_updated_at" : "last_updated_at_today");
439 lastUpdate.textContent = i18n.getMessage(messageID, timeDate); 455 lastUpdate.textContent = i18n.getMessage(messageID, timeDate);
440 } 456 }
441 } 457 }
442 458
443 function onSubscriptionChange(subscription) 459 function onSubscriptionMessage(action, subscription)
444 { 460 {
445 var element = findSubscriptionElement(subscription); 461 var element = findSubscriptionElement(subscription);
446 if (element)
447 updateSubscriptionInfo(element);
448 }
449 462
450 function onSubscriptionAdded(subscription) 463 switch (action)
451 {
452 if (subscription instanceof SpecialSubscription)
453 { 464 {
454 for (var i = 0; i < subscription.filters.length; i++) 465 case "disabled":
455 onFilterAdded(subscription.filters[i]); 466 case "downloading":
456 } 467 case "downloadStatus":
457 else if (subscription.url == Prefs.subscriptions_exceptionsurl) 468 case "homepage":
458 $("#acceptableAds").prop("checked", true); 469 case "lastDownload":
459 else if (!findSubscriptionElement(subscription)) 470 case "title":
460 addSubscriptionEntry(subscription); 471 if (element)
461 } 472 updateSubscriptionInfo(element, subscription);
473 break;
474 case "added":
475 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl)
476 {
477 // Hack - Assume newly added subscriptions are currently downloading as
478 // we process the downloading message before we have finished
479 // adding the subscription's element!
480 subscription.isDownloading = true;
Sebastian Noack 2016/04/07 12:49:14 That is because you asynchronously retrieve the Ac
kzar 2016/04/07 13:04:53 Much better solution and works fine. Done.
462 481
463 function onSubscriptionRemoved(subscription) 482 if (subscription.url == acceptableAdsUrl)
464 { 483 $("#acceptableAds").prop("checked", true);
465 if (subscription instanceof SpecialSubscription) 484 else if (!element)
466 { 485 addSubscriptionEntry(subscription);
467 for (var i = 0; i < subscription.filters.length; i++) 486 });
468 onFilterRemoved(subscription.filters[i]); 487 break;
469 } 488 case "removed":
470 else if (subscription.url == Prefs.subscriptions_exceptionsurl) 489 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl)
471 $("#acceptableAds").prop("checked", false); 490 {
472 else 491 if (subscription.url == acceptableAdsUrl)
473 { 492 $("#acceptableAds").prop("checked", false);
474 var element = findSubscriptionElement(subscription); 493 else if (element)
475 if (element) 494 element.parentNode.removeChild(element);
476 element.parentNode.removeChild(element); 495 });
496 break;
477 } 497 }
478 } 498 }
479 499
480 function onFilterAdded(filter) 500 function onPrefMessage(key, value)
481 { 501 {
482 if (filter instanceof WhitelistFilter && 502 switch (key)
483 /^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 503 {
484 appendToListBox("excludedDomainsBox", RegExp.$1); 504 case "notifications_showui":
485 else 505 document.getElementById("shouldShowNotificationsContainer").hidden = !valu e;
486 appendToListBox("userFiltersBox", filter.text); 506 return;
507 case "notifications_ignoredcategories":
508 key = "shouldShowNotifications";
509 value = value.indexOf("*") == -1;
510 break;
511 }
512
513 var checkbox = document.getElementById(key);
514 if (checkbox)
515 checkbox.checked = value;
487 } 516 }
488 517
489 function onFilterRemoved(filter) 518 function onFilterMessage(action, filter)
490 { 519 {
491 if (filter instanceof WhitelistFilter && 520 switch (action)
492 /^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 521 {
493 removeFromListBox("excludedDomainsBox", RegExp.$1); 522 case "loaded":
494 else 523 reloadFilters();
495 removeFromListBox("userFiltersBox", filter.text); 524 break;
525 case "added":
526 if (whitelistedDomainRegexp.test(filter.text))
527 appendToListBox("excludedDomainsBox", RegExp.$1);
528 else
529 appendToListBox("userFiltersBox", filter.text);
530 break;
531 case "removed":
532 if (whitelistedDomainRegexp.test(filter.text))
533 removeFromListBox("excludedDomainsBox", RegExp.$1);
534 else
535 removeFromListBox("userFiltersBox", filter.text);
536 break;
537 }
496 } 538 }
497 539
498 // Populates a list box with a number of entries 540 function clearListBox(id)
499 function populateList(id, entries)
500 { 541 {
501 var list = document.getElementById(id); 542 var list = document.getElementById(id);
502 while (list.lastChild) 543 while (list.lastChild)
503 list.removeChild(list.lastChild); 544 list.removeChild(list.lastChild);
504
505 entries.sort();
506 for (var i = 0; i < entries.length; i++)
507 {
508 var option = new Option();
509 option.text = entries[i];
510 option.value = entries[i];
511 list.appendChild(option);
512 }
513 } 545 }
514 546
515 // Add a filter string to the list box. 547 // Add a filter string to the list box.
516 function appendToListBox(boxId, text) 548 function appendToListBox(boxId, text)
517 { 549 {
518 var elt = new Option(); /* Note: document.createElement("option") is unreliab le in Opera */ 550 // Note: document.createElement("option") is unreliable in Opera
551 var elt = new Option();
519 elt.text = text; 552 elt.text = text;
520 elt.value = text; 553 elt.value = text;
521 document.getElementById(boxId).appendChild(elt); 554 document.getElementById(boxId).appendChild(elt);
522 } 555 }
523 556
524 // Remove a filter string from a list box. 557 // Remove a filter string from a list box.
525 function removeFromListBox(boxId, text) 558 function removeFromListBox(boxId, text)
526 { 559 {
527 var list = document.getElementById(boxId); 560 var list = document.getElementById(boxId);
528 for (var i = 0; i < list.length; i++) 561 for (var i = 0; i < list.length; i++)
529 if (list.options[i].value == text) 562 if (list.options[i].value == text)
530 list.remove(i--); 563 list.remove(i--);
531 } 564 }
532 565
533 function addWhitelistDomain(event) 566 function addWhitelistDomain(event)
534 { 567 {
535 event.preventDefault(); 568 event.preventDefault();
536 569
537 var domain = document.getElementById("newWhitelistDomain").value.replace(/\s/g , ""); 570 var domain = document.getElementById("newWhitelistDomain").value.replace(/\s/g , "");
538 document.getElementById("newWhitelistDomain").value = ""; 571 document.getElementById("newWhitelistDomain").value = "";
539 if (!domain) 572 if (!domain)
540 return; 573 return;
541 574
542 var filterText = "@@||" + domain + "^$document"; 575 var filterText = "@@||" + domain + "^$document";
543 FilterStorage.addFilter(Filter.fromText(filterText)); 576 addFilter(filterText);
544 } 577 }
545 578
546 // Adds filter text that user typed to the selection box 579 // Adds filter text that user typed to the selection box
547 function addTypedFilter(event) 580 function addTypedFilter(event)
548 { 581 {
549 event.preventDefault(); 582 event.preventDefault();
550 583
551 var element = document.getElementById("newFilter"); 584 var element = document.getElementById("newFilter");
552 var result = parseFilter(element.value); 585 addFilter(element.value, function(errors)
553
554 if (result.error)
555 { 586 {
556 alert(result.error); 587 if (errors.length > 0)
557 return; 588 alert(errors.join("\n"));
558 } 589 else
559 590 element.value = "";
560 if (result.filter) 591 });
561 FilterStorage.addFilter(result.filter);
562
563 element.value = "";
564 } 592 }
565 593
566 // Removes currently selected whitelisted domains 594 // Removes currently selected whitelisted domains
567 function removeSelectedExcludedDomain(event) 595 function removeSelectedExcludedDomain(event)
568 { 596 {
569 event.preventDefault(); 597 event.preventDefault();
570 var excludedDomainsBox = document.getElementById("excludedDomainsBox"); 598 var excludedDomainsBox = document.getElementById("excludedDomainsBox");
571 var remove = []; 599 var remove = [];
572 for (var i = 0; i < excludedDomainsBox.length; i++) 600 for (var i = 0; i < excludedDomainsBox.length; i++)
573 if (excludedDomainsBox.options[i].selected) 601 if (excludedDomainsBox.options[i].selected)
574 remove.push(excludedDomainsBox.options[i].value); 602 remove.push(excludedDomainsBox.options[i].value);
575 if (!remove.length) 603 if (!remove.length)
576 return; 604 return;
577 605
578 for (var i = 0; i < remove.length; i++) 606 for (var i = 0; i < remove.length; i++)
579 FilterStorage.removeFilter(Filter.fromText("@@||" + remove[i] + "^$document" )); 607 removeFilter("@@||" + remove[i] + "^$document");
580 } 608 }
581 609
582 // Removes all currently selected filters 610 // Removes all currently selected filters
583 function removeSelectedFilters(event) 611 function removeSelectedFilters(event)
584 { 612 {
585 event.preventDefault(); 613 event.preventDefault();
586 var userFiltersBox = document.getElementById("userFiltersBox"); 614 var userFiltersBox = document.getElementById("userFiltersBox");
587 var remove = []; 615 var remove = [];
588 for (var i = 0; i < userFiltersBox.length; i++) 616 for (var i = 0; i < userFiltersBox.length; i++)
589 if (userFiltersBox.options[i].selected) 617 if (userFiltersBox.options[i].selected)
590 remove.push(userFiltersBox.options[i].value); 618 remove.push(userFiltersBox.options[i].value);
591 if (!remove.length) 619 if (!remove.length)
592 return; 620 return;
593 621
594 for (var i = 0; i < remove.length; i++) 622 for (var i = 0; i < remove.length; i++)
595 FilterStorage.removeFilter(Filter.fromText(remove[i])); 623 removeFilter(remove[i]);
596 } 624 }
597 625
598 // Shows raw filters box and fills it with the current user filters 626 // Shows raw filters box and fills it with the current user filters
599 function toggleFiltersInRawFormat(event) 627 function toggleFiltersInRawFormat(event)
600 { 628 {
601 event.preventDefault(); 629 event.preventDefault();
602 630
603 $("#rawFilters").toggle(); 631 $("#rawFilters").toggle();
604 if ($("#rawFilters").is(":visible")) 632 if ($("#rawFilters").is(":visible"))
605 { 633 {
606 var userFiltersBox = document.getElementById("userFiltersBox"); 634 var userFiltersBox = document.getElementById("userFiltersBox");
607 var text = ""; 635 var text = "";
608 for (var i = 0; i < userFiltersBox.length; i++) 636 for (var i = 0; i < userFiltersBox.length; i++)
609 text += userFiltersBox.options[i].value + "\n"; 637 text += userFiltersBox.options[i].value + "\n";
610 document.getElementById("rawFiltersText").value = text; 638 document.getElementById("rawFiltersText").value = text;
611 } 639 }
612 } 640 }
613 641
614 // Imports filters in the raw text box 642 // Imports filters in the raw text box
615 function importRawFiltersText() 643 function importRawFiltersText()
616 { 644 {
617 var text = document.getElementById("rawFiltersText").value; 645 var text = document.getElementById("rawFiltersText").value;
618 var result = parseFilters(text);
619 646
620 var errors = result.errors.filter(function(e) 647 importRawFilters(text, true, function(errors)
621 { 648 {
622 return e.type != "unexpected-filter-list-header"; 649 if (errors.length > 0)
650 alert(errors.join("\n"));
651 else
652 $("#rawFilters").hide();
623 }); 653 });
624
625 if (errors.length > 0)
626 {
627 alert(errors.join("\n"));
628 return;
629 }
630
631 var seenFilter = Object.create(null);
632 for (var i = 0; i < result.filters.length; i++)
633 {
634 var filter = result.filters[i];
635 FilterStorage.addFilter(filter);
636 seenFilter[filter.text] = null;
637 }
638
639 var remove = [];
640 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
641 {
642 var subscription = FilterStorage.subscriptions[i];
643 if (!(subscription instanceof SpecialSubscription))
644 continue;
645
646 for (var j = 0; j < subscription.filters.length; j++)
647 {
648 var filter = subscription.filters[j];
649 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$document$/.t est(filter.text))
650 continue;
651
652 if (!(filter.text in seenFilter))
653 remove.push(filter);
654 }
655 }
656
657 for (var i = 0; i < remove.length; i++)
658 FilterStorage.removeFilter(remove[i]);
659
660 $("#rawFilters").hide();
661 } 654 }
662 655
663 // Called when user explicitly requests filter list updates 656 // Called when user explicitly requests filter list updates
664 function updateFilterLists() 657 function updateFilterLists()
665 { 658 {
666 for (var i = 0; i < FilterStorage.subscriptions.length; i++) 659 // Without the URL parameter this will update all subscriptions
667 { 660 updateSubscription();
668 var subscription = FilterStorage.subscriptions[i];
669 if (subscription instanceof DownloadableSubscription)
670 Synchronizer.execute(subscription, true, true);
671 }
672 } 661 }
673 662
674 // Adds a subscription entry to the UI. 663 // Adds a subscription entry to the UI.
675 function addSubscriptionEntry(subscription) 664 function addSubscriptionEntry(subscription)
676 { 665 {
677 var template = document.getElementById("subscriptionTemplate"); 666 var template = document.getElementById("subscriptionTemplate");
678 var element = template.cloneNode(true); 667 var element = template.cloneNode(true);
679 element.removeAttribute("id"); 668 element.removeAttribute("id");
680 element._subscription = subscription; 669 element._subscription = subscription;
681 670
682 var removeButton = element.getElementsByClassName("subscriptionRemoveButton")[ 0]; 671 var removeButton = element.getElementsByClassName("subscriptionRemoveButton")[ 0];
683 removeButton.setAttribute("title", removeButton.textContent); 672 removeButton.setAttribute("title", removeButton.textContent);
684 removeButton.textContent = "\xD7"; 673 removeButton.textContent = "\xD7";
685 removeButton.addEventListener("click", function() 674 removeButton.addEventListener("click", function()
686 { 675 {
687 if (!confirm(i18n.getMessage("global_remove_subscription_warning"))) 676 if (!confirm(i18n.getMessage("global_remove_subscription_warning")))
688 return; 677 return;
689 678
690 FilterStorage.removeSubscription(subscription); 679 removeSubscription(subscription.url);
691 }, false); 680 }, false);
692 if (Prefs.additional_subscriptions.indexOf(subscription.url) != -1) 681
693 removeButton.style.visibility = "hidden"; 682 getPref("additional_subscriptions", function(additionalSubscriptions)
683 {
684 if (additionalSubscriptions.indexOf(subscription.url) != -1)
685 removeButton.style.visibility = "hidden";
686 });
694 687
695 var enabled = element.getElementsByClassName("subscriptionEnabled")[0]; 688 var enabled = element.getElementsByClassName("subscriptionEnabled")[0];
696 enabled.addEventListener("click", function() 689 enabled.addEventListener("click", function()
697 { 690 {
698 if (subscription.disabled == !enabled.checked) 691 subscription.disabled = !subscription.disabled;
699 return; 692 toggleSubscription(subscription.url, true);
700
701 subscription.disabled = !enabled.checked;
702 }, false); 693 }, false);
703 694
704 updateSubscriptionInfo(element); 695 updateSubscriptionInfo(element);
705 696
706 document.getElementById("filterLists").appendChild(element); 697 document.getElementById("filterLists").appendChild(element);
707 } 698 }
708 699
709 function setLinks(id) 700 function setLinks(id)
710 { 701 {
711 var element = document.getElementById(id); 702 var element = document.getElementById(id);
712 if (!element) 703 if (!element)
713 return; 704 return;
714 705
715 var links = element.getElementsByTagName("a"); 706 var links = element.getElementsByTagName("a");
716 for (var i = 0; i < links.length; i++) 707 for (var i = 0; i < links.length; i++)
717 { 708 {
718 if (typeof arguments[i + 1] == "string") 709 if (typeof arguments[i + 1] == "string")
719 { 710 {
720 links[i].href = arguments[i + 1]; 711 links[i].href = arguments[i + 1];
721 links[i].setAttribute("target", "_blank"); 712 links[i].setAttribute("target", "_blank");
722 } 713 }
723 else if (typeof arguments[i + 1] == "function") 714 else if (typeof arguments[i + 1] == "function")
724 { 715 {
725 links[i].href = "javascript:void(0);"; 716 links[i].href = "javascript:void(0);";
726 links[i].addEventListener("click", arguments[i + 1], false); 717 links[i].addEventListener("click", arguments[i + 1], false);
727 } 718 }
728 } 719 }
729 } 720 }
721
722 ext.onMessage.addListener(function(message)
723 {
724 switch (message.type)
725 {
726 case "app.respond":
727 switch (message.action)
728 {
729 case "addSubscription":
730 var subscription = message.args[0];
731 startSubscriptionSelection(subscription.title, subscription.url);
732 break;
733 case "focusSection":
734 var tabs = document.getElementsByClassName("ui-tabs-panel");
735 for (var i = 0; i < tabs.length; i++)
736 {
737 var found = tabs[i].querySelector(
738 "[data-section='" + message.args[0] + "']"
739 );
740 if (!found)
741 continue;
742
743 var previous = document.getElementsByClassName("focused");
744 if (previous.length > 0)
745 previous[0].classList.remove("focused");
746
747 var tab = $("[href='#" + tabs[i].id + "']");
748 $("#tabs").tabs("select", tab.parent().index());
749 found.classList.add("focused");
750 }
751 break;
752 }
753 break;
754 case "filters.respond":
755 onFilterMessage(message.action, message.args[0]);
756 break;
757 case "prefs.respond":
758 onPrefMessage(message.action, message.args[0]);
759 break;
760 case "subscriptions.respond":
761 onSubscriptionMessage(message.action, message.args[0]);
762 break;
763 }
764 });
OLDNEW
« no previous file with comments | « lib/notificationHelper.js ('k') | safari/ext/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld