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

Delta Between Two Patch Sets: options.js

Issue 6088024630755328: issue 1526 - Implement new options page design for Chrome/Opera/Safari (Closed)
Left Patch Set: Created Jan. 28, 2015, 3:59 p.m.
Right Patch Set: Comment about solution being temprorary is added to subscriptions.xml Created June 13, 2015, 12:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « options.html ('k') | skin/options.css » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 var optionSubscriptions = {}; 22 var subscriptionsMap = Object.create(null);
23 var acceptableAdsUrl = null; 23 var recommendationsMap = Object.create(null);
24 24 var filtersMap = Object.create(null);
25 var collections = Object.create(null);
26
27 function Collection(details)
28 {
29 this.details = details;
30 this.items = [];
31 }
32
33 Collection.prototype.addItems = function()
34 {
35 var length = Array.prototype.push.apply(this.items, arguments);
36 if (length == 0)
37 return;
38
39 this.items.sort(function(a, b)
40 {
41 var aValue = (a.title || a.url || a.text).toLowerCase();
42 var bValue = (b.title || b.url || a.text).toLowerCase();
43 return aValue.localeCompare(bValue);
44 });
45
46 for (var j = 0; j < this.details.length; j++)
47 {
48 var table = E(this.details[j].id);
49 var template = table.querySelector("template");
50 for (var i = 0; i < arguments.length; i++)
51 {
52 var item = arguments[i];
53 var text = item.title || item.url || item.text;
54 var listItem = document.createElement("li");
55 listItem.appendChild(document.importNode(template.content, true));
56 listItem.dataset.access = item.url || item.text;
57 listItem.querySelector(".display").textContent = text;
58 if (text)
59 listItem.dataset.search = text.toLowerCase();
60
61 var control = listItem.querySelector(".control");
62 if (control)
63 {
64 control.addEventListener("click", this.details[j].onClick, false);
65 control.checked = item.disabled == false;
66 }
67
68 if (table.hasChildNodes())
69 table.insertBefore(listItem, table.childNodes[this.items.indexOf(item) ]);
70 else
71 table.appendChild(listItem);
72 }
73 }
74 return length;
75 };
76
77 Collection.prototype.removeItem = function(item)
78 {
79 var index = this.items.indexOf(item);
80 if (index == -1)
81 return;
82
83 this.items.splice(index, 1);
84 var access = (item.url || item.text).replace(/'/g, "\\'");
85 for (var i = 0; i < this.details.length; i++)
86 {
87 var table = E(this.details[i].id);
88 var element = table.querySelector("[data-access='" + access + "']");
89 element.parentElement.removeChild(element);
90 }
91 };
92
93 Collection.prototype.clearAll = function()
94 {
95 for (var i = 0; i < this.details.length; i++)
96 {
97 var table = E(this.details[i].id);
98 var template = table.querySelector("template");
99 table.innerHTML = "";
100 table.appendChild(template);
101 }
102 this.items.length = 0;
103 };
104
105 function onToggleSubscriptionClick(e)
106 {
107 e.preventDefault();
108 var subscriptionUrl = e.target.parentNode.dataset.access;
109 if (!e.target.checked)
110 removeSubscription(subscriptionUrl);
111 else
112 addEnableSubscription(subscriptionUrl);
113 }
114
115 function onAddLanguageSubscriptionClick(e)
116 {
117 e.preventDefault();
118 var url = this.parentNode.dataset.access;
119 addEnableSubscription(url);
120 }
121
122 function onRemoveFilterClick()
123 {
124 var filter = this.parentNode.dataset.access;
125 removeFilter(filter);
126 }
127
128 collections.popular = new Collection(
129 [
130 {
131 id: "recommend-list-table",
132 onClick: onToggleSubscriptionClick
133 }
134 ]);
135 collections.langs = new Collection(
136 [
137 {
138 id: "blocking-languages-table",
139 onClick: onToggleSubscriptionClick
140 },
141 {
142 id: "blocking-languages-dialog-table"
143 }
144 ]);
145 collections.allLangs = new Collection(
146 [
147 {
148 id: "all-lang-table",
149 onClick: onAddLanguageSubscriptionClick
150 }
151 ]);
152 collections.acceptableAds = new Collection(
153 [
154 {
155 id: "acceptableads-table",
156 onClick: onToggleSubscriptionClick
157 }
158 ]);
159 collections.custom = new Collection(
160 [
161 {
162 id: "custom-list-table",
163 onClick: onToggleSubscriptionClick
164 }
165 ]);
166 collections.whitelist = new Collection(
167 [
168 {
169 id: "whitelisting-table",
170 onClick: onRemoveFilterClick
171 }
172 ]);
173
174 function updateSubscription(subscription)
175 {
176 var subscriptionUrl = subscription.url;
177 var knownSubscription = subscriptionsMap[subscriptionUrl];
178 if (knownSubscription)
179 knownSubscription.disabled = subscription.disabled;
180 else
181 {
182 getAcceptableAdsURL(function(acceptableAdsUrl)
183 {
184 function onObjectChanged()
185 {
186 var access = (subscriptionUrl || subscription.text).replace(/'/g, "\\' ");
187 var elements = document.querySelectorAll("[data-access='" + access + " ']");
188 for (var i = 0; i < elements.length; i++)
189 {
190 var element = elements[i];
191 var control = element.querySelector(".control");
192 if (control.localName == "input")
193 control.checked = subscription.disabled == false;
194 if (subscriptionUrl in recommendationsMap)
195 {
196 var recommendation = recommendationsMap[subscriptionUrl];
197 if (recommendation.isAdsType)
198 {
199 if (subscription.disabled == false)
200 {
201 collections.allLangs.removeItem(subscription);
202 collections.langs.addItems(subscription);
203 }
204 else
205 {
206 collections.allLangs.addItems(subscription);
207 collections.langs.removeItem(subscription);
208 }
209 }
210 }
211 }
212 }
213
214 if (!Object.observe)
215 {
216 // Currently only "disabled" property of subscription used for observa tion
217 // but with Advanced tab implementation we should also add more proper ties.
218 ["disabled"].forEach(function(property)
219 {
220 subscription["$" + property] = subscription[property];
221 Object.defineProperty(subscription, property,
222 {
223 get: function()
224 {
225 return this["$" + property];
226 },
227 set: function(value)
228 {
229 this["$" + property] = value;
230 onObjectChanged();
231 }
232 });
233 });
234 }
235 else
236 {
237 Object.observe(subscription, onObjectChanged);
238 }
239
240 var collection = null;
241 if (subscriptionUrl in recommendationsMap)
242 {
243 var recommendation = recommendationsMap[subscriptionUrl];
244 if (recommendation.isPopular)
245 collection = collections.popular;
246 else if (recommendation.isAdsType && subscription.disabled == false)
247 collection = collections.langs;
248 else
249 collection = collections.allLangs;
250 }
251 else if (subscriptionUrl == acceptableAdsUrl)
252 collection = collections.acceptableAds;
253 else
254 collection = collections.custom;
255
256 collection.addItems(subscription);
257 subscriptionsMap[subscriptionUrl] = subscription;
258 });
259 }
260 }
261
262 function updateFilter(filter)
263 {
264 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/);
265 if (match && !filtersMap[filter.text])
266 {
267 filter.title = match[1];
268 collections.whitelist.addItems(filter);
269 filtersMap[filter.text] = filter
270 }
271 else
272 {
273 // TODO: add `filters[i].text` to list of custom filters
274 }
275 }
276
277 function loadRecommendations()
278 {
279 var request = new XMLHttpRequest();
280 request.open("GET", "subscriptions.xml", false);
281 request.addEventListener("load", function()
282 {
283 var list = document.getElementById("subscriptionSelector");
284 var docElem = request.responseXML.documentElement;
285 var elements = docElem.getElementsByTagName("subscription");
286 for (var i = 0; i < elements.length; i++)
287 {
288 var element = elements[i];
289 var subscription = Object.create(null);
290 subscription.title = element.getAttribute("title");
291 subscription.url = element.getAttribute("url");
292 subscription.disabled = null;
293 subscription.downloadStatus = null;
294 subscription.homepage = null;
295 subscription.lastSuccess = null;
296 var recommendation = Object.create(null);
297 recommendation.isAdsType = false;
298 recommendation.isPopular = false;
299 var prefix = element.getAttribute("prefixes");
300 if (prefix)
301 {
302 var prefix = element.getAttribute("prefixes").replace(/,/g, "_");
303 subscription.title = ext.i18n.getMessage("options_language_" + prefix) ;
304 recommendation.isAdsType = true;
305 }
306 else
307 subscription.title = element.getAttribute("specialization");
308
309 if (element.getAttribute("popular"))
310 recommendation.isPopular = true;
311
312 recommendationsMap[subscription.url] = recommendation;
313 updateSubscription(subscription);
314 }
315 }, false);
316 request.send(null);
317 }
318
25 function onDOMLoaded() 319 function onDOMLoaded()
26 { 320 {
27 initTabs(); 321 var recommendationTemplate = document.querySelector("#recommend-list-table t emplate");
28 updateVersionNumber(); 322 var popularText = ext.i18n.getMessage("options_popular");
323 recommendationTemplate.content.querySelector(".popular").textContent = popul arText;
324 var languagesTemplate = document.querySelector("#all-lang-table template");
325 var buttonText = ext.i18n.getMessage("options_button_add");
326 languagesTemplate.content.querySelector(".button-add span").textContent = bu ttonText;
327
29 updateShareLink(); 328 updateShareLink();
30 populateLists(); 329 populateLists();
330
331 var tabList = document.querySelectorAll("#main-navigation-tabs li");
332 for (var i = 0; i < tabList.length; i++)
333 {
334 tabList[i].addEventListener("click", function(e)
335 {
336 document.body.dataset.tab = e.currentTarget.dataset.show;
337 }, false);
338 }
339
340 function onFindLanguageKeyUp()
341 {
342 var searchStyle = E("search-style");
343 if (!this.value)
344 searchStyle.innerHTML = "";
345 else
346 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }";
347 }
348
349 // Update version number in navigation sidebar
350 ext.backgroundPage.sendMessage(
351 {
352 method: "app.get",
353 what: "addonVersion"
354 },
355 function(addonVersion)
356 {
357 E("abp-version").textContent = addonVersion;
358 });
31 359
32 E("find-language").setAttribute("placeholder", ext.i18n.getMessage("options_ modal_language_find")); 360 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find");
33 setLinks("block-element-explanation", "#"); 361 E("find-language").setAttribute("placeholder", placeholderValue);
34 362 E("add-blocking-list").addEventListener("click", function()
35 E("add-blocking-list").addEventListener("click", Modal.open, false); 363 {
36 E("add-website-language").addEventListener("click", Modal.open, false); 364 openDialog("customlist");
37 E("modal-close").addEventListener("click", Modal.close, false);
38 E("whitelisting-add-icon").addEventListener("click", whitelistDomainBtnClick , false);
39 E("whitelisting-add-btn").addEventListener("click", whitelistDomainBtnClick, false);
40 E("whitelisting-enter-icon").addEventListener("click", whitelistDomainBtnCli ck, false);
41 E("whitelisting-textbox").addEventListener("keypress", function(e) {
42 if (e.keyCode == 13)
43 whitelistDomainBtnClick();
44 }, false); 365 }, false);
45 E("whitelisting-cancel-btn").addEventListener("click", function(){ 366 E("add-website-language").addEventListener("click", function()
46 E("whitelisting-textbox").value = ""; 367 {
368 openDialog("language");
47 }, false); 369 }, false);
48 E("allow-whitelist-cb").addEventListener("click", toggleAcceptableAds, false ); 370 E("dialog-close").addEventListener("click", function()
49 E("import-blockingList-btn").addEventListener("click", importListBtnCLick, f alse); 371 {
50 E("edit-ownBlockingList-btn").addEventListener("click", editOwnRulsBtnClick, false); 372 delete document.body.dataset.dialog;
51 E("find-language").addEventListener("keyup", searchLanguage, false); 373 }, false);
52 } 374 E("edit-ownBlockingList-button").addEventListener("click", editCustomFilters , false);
53 375 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false);
54 function initTabs() 376 E("whitelisting").addEventListener("click", function(e)
55 { 377 {
56 var showContent = function(tab) 378 var id = e.target.id;
57 { 379 if (id == "whitelisting-add-icon" || id == "whitelisting-enter-icon")
58 var tab = tab.querySelector(".tabs li.active"); 380 addWhitelistedDomain();
59 if (tab.dataset.show) 381 else if (id == "whitelisting-cancel-button")
60 E(tab.dataset.show).classList.add("active"); 382 E("whitelisting-textbox").value = "";
Thomas Greiner 2015/01/30 13:55:52 Using CSS classes instead of modifying the style a
saroyanm 2015/02/13 10:57:11 Yes, good point.
61 }; 383 }, false);
62 var optionList = document.querySelectorAll('.tabs li[data-show]'); 384 E("whitelisting-add-button").addEventListener("click", addWhitelistedDomain, false);
63 for (var i = 0; i < optionList.length; ++i) 385 E("whitelisting-textbox").addEventListener("keypress", function(e)
64 { 386 {
65 optionList[i].addEventListener("click", function(ev) 387 // e.keyCode has been deprecated so we attempt to use e.key
66 { 388 // keyCode "13" corresponds to "Enter"
67 var tab = this.parentNode.querySelector(".active"); 389 if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
68 tab.classList.remove("active"); 390 addWhitelistedDomain();
69 this.classList.add("active"); 391 }, false);
70 E(tab.dataset.show).classList.remove("active"); 392 E("import-blockingList-button").addEventListener("click", function()
71 showContent(this.parentNode); 393 {
72 }, false); 394 var url = E("blockingList-textbox").value;
73 } 395 addEnableSubscription(url);
74 showContent(E("main-navigation-tabs")); 396 delete document.body.dataset.dialog;
75 showContent(E("blocking-list-tabs")); 397 }, false);
76 } 398 }
77 399
78 var Modal = 400 function openDialog(name)
79 { 401 {
80 open: function (content) 402 document.body.dataset.dialog = name;
81 { 403 }
82 var modal = E("modal"); 404
83 var content = E(this && this.dataset ? this.dataset.show : content);
84 content.classList.add("active");
85 document.body.classList.add("modal-active");
Thomas Greiner 2015/01/30 13:55:52 You could get rid of this entire function by setti
saroyanm 2015/02/13 10:57:11 Done.
86 if (content.dataset.title)
87 E("modal-title").innerHTML = ext.i18n.getMessage(content.dataset.title);
88 modal.style.marginTop = -(modal.clientHeight/2)+"px";
89 },
90 close: function ()
91 {
92 var contents = E("modal-content").childNodes;
93 for (var i = 0; i < contents.length; ++i)
94 {
95 if (contents[i].style)
96 contents[i].classList.remove("active");
97 }
98 document.body.classList.remove("modal-active");
99 }
100 }
101
102 function populateLists() 405 function populateLists()
103 { 406 {
104 ext.backgroundPage.sendMessage({ 407 subscriptionsMap = Object.create(null);
408 filtersMap = Object.create(null);
409 recommendationsMap = Object.create(null);
410
411 // Empty collections and lists
412 for (var property in collections)
413 collections[property].clearAll();
414
415 ext.backgroundPage.sendMessage(
416 {
105 type: "subscriptions.get", 417 type: "subscriptions.get",
106 special: true 418 special: true
107 }, function(subscriptions) 419 },
108 { 420 function(subscriptions)
421 {
422 // Load filters
109 for (var i = 0; i < subscriptions.length; i++) 423 for (var i = 0; i < subscriptions.length; i++)
110 { 424 {
111 ext.backgroundPage.sendMessage({ 425 ext.backgroundPage.sendMessage(
426 {
112 type: "filters.get", 427 type: "filters.get",
113 subscriptionUrl: subscriptions[i].url 428 subscriptionUrl: subscriptions[i].url
114 }, function(filters) 429 },
115 { 430 function(filters)
116 var whitelistArray = []; 431 {
117 for (var i = 0; i < filters.length; i++) 432 for (var i = 0; i < filters.length; i++)
118 { 433 updateFilter(filters[i]);
119 var match = filters[i].text.match(/^@@\|\|([^\/:]+)\^\$document$/);
120 if (match[1])
121 {
122 whitelistArray.push(match[1]);
123 }
124 else
125 {
126 // TODO: add `filters[i].text` to list of custom filters
127 }
128 }
129
130 if (whitelistArray.length > 0)
131 {
132 whitelistArray.sort();
133 for (var i = 0; i < whitelistArray.length; i++)
134 {
135 var domain = whitelistArray[i];
136 E("whitelisting-table").appendChild(createWhitelistElem(domain));
137 }
138 }
139 }); 434 });
140 } 435 }
141 }); 436 });
142 437 loadRecommendations();
143 loadRecommendations(function(recommends) 438 getAcceptableAdsURL(function(acceptableAdsUrl)
144 { 439 {
145 ext.backgroundPage.sendMessage({ 440 var subscription = Object.create(null);
441 subscription.url = acceptableAdsUrl;
442 subscription.disabled = true;
443 subscription.title = ext.i18n.getMessage("options_acceptableAds_descriptio n");
444 updateSubscription(subscription);
445
446 // Load user subscriptions
447 ext.backgroundPage.sendMessage(
448 {
146 type: "subscriptions.get", 449 type: "subscriptions.get",
147 downloadable: true 450 downloadable: true
148 }, function(subscriptions) 451 },
149 { 452 function(subscriptions)
150 getAcceptableAdsURL(function(url) 453 {
151 { 454 for (var i = 0; i < subscriptions.length; i++)
152 acceptableAdsUrl = url; 455 onSubscriptionMessage("added", subscriptions[i]);
153 for (var i = 0; i < subscriptions.length; i++)
154 {
155 if (subscriptions[i].url == acceptableAdsUrl)
156 {
157 E("allow-whitelist-cb").checked = !subscriptions[i].disabled;
158 continue;
159 }
160
161 var subscription = recommends[subscriptions[i].url];
162 if (!subscription)
163 recommends[subscriptions[i].url] = subscriptions[i];
164 else
165 {
166 subscription.disabled = subscriptions[i].disabled;
167 if (subscription.type == "ads")
168 subscription.isAdded = true;
169 }
170 }
171 for (var key in recommends)
172 addOptionItem(recommends[key]);
173 });
174 }); 456 });
175 }); 457 });
176 } 458 }
177 459
178 function loadRecommendations(callback) 460 function addWhitelistedDomain()
179 { 461 {
180 var recommendations = {}; 462 var domain = E("whitelisting-textbox");
181 var request = new XMLHttpRequest(); 463 if (domain.value)
182 request.open("GET", "subscriptions.xml"); 464 {
Thomas Greiner 2015/01/30 13:55:52 This function call is missing the third parameter
saroyanm 2015/02/13 10:57:11 Done.
183 request.onload = function() 465 ext.backgroundPage.sendMessage(
184 { 466 {
185 var list = document.getElementById("subscriptionSelector"); 467 type: "filters.add",
186 var elements = request.responseXML.documentElement.getElementsByTagName("s ubscription"); 468 text: "@@||" + domain.value.toLowerCase() + "^$document"
187 for (var i = 0; i < elements.length; i++) 469 });
188 { 470 }
189 var element = elements[i]; 471
190 var subscription = {}; 472 domain.value = "";
Thomas Greiner 2015/01/30 13:55:52 Use `var subscription = Object.create(null);` inst
saroyanm 2015/02/13 10:57:11 Done.
191 subscription.title = element.getAttribute("title"); 473 }
192 subscription.url = element.getAttribute("url"); 474
193 subscription.disabled = true; 475 function editCustomFilters()
194 var prefix = element.getAttribute("prefixes"); 476 {
195 if (prefix) 477 //TODO: NYI
196 { 478 }
197 subscription.prefixes = element.getAttribute("prefixes"); 479
198 subscription.type = "ads";
199 subscription.display = ext.i18n.getMessage("options_language_"+subscri ption.prefixes.replace(/,/g, '_'));
Thomas Greiner 2015/01/30 13:55:52 This line doesn't need to be that long. I'd sugges
saroyanm 2015/02/13 10:57:11 Done.
200 }
201 else
202 subscription.display = element.getAttribute("specialization");
203
204 var popular = element.getAttribute("popular");
Thomas Greiner 2015/01/30 13:55:52 It's safer to convert this string into a boolean.
saroyanm 2015/02/13 10:57:11 Done.
205 if (popular)
206 subscription.popular = element.getAttribute("popular");
207
208 recommendations[subscription.url] = subscription;
209 }
210 optionSubscriptions = recommendations;
211 callback(recommendations);
212 }
213 request.send();
214 }
215
216 function searchLanguage()
217 {
218 var searchVal = this.value;
219 var items = E("all-lang-table").childNodes;
220 for (var i = 0; i < items.length; ++i)
221 {
222 var item = items[i];
223 var language = item.getElementsByTagName("span")[1].innerHTML;
Thomas Greiner 2015/01/30 13:55:52 Don't rely on the content of an element because it
224 if (language.toLowerCase().indexOf(searchVal.toLowerCase()) > -1)
225 item.style.display = "block";
226 else
227 item.style.display = "none";
228 }
229 }
230
231 function addOptionItem(subscription)
232 {
233 var display = subscription.display ? subscription.display : subscription.tit le;
Thomas Greiner 2015/01/30 13:55:52 You can reduce this down to `var display = subscri
234 var getPossition = function(elements, subscription)
Thomas Greiner 2015/01/30 13:55:52 This variable name is spelled incorrectly.
Thomas Greiner 2015/01/30 13:55:52 You were able to use a much nicer approach for pop
saroyanm 2015/02/13 10:57:11 Changed to use arrays for sorting.
235 {
236 var localArray = [];
237 for (var i = 0; i < elements.length; i++)
238 {
239 var elem = elements[i];
240 localArray.push(elem);
241 }
242
243 localArray.push(subscription);
244 return localArray.sort(function(a, b) {
245 var aPopular = a.getElementsByClassName("popular").length > 0;
246 var bPopular = b.getElementsByClassName("popular").length > 0;
247 if(aPopular == bPopular)
248 {
249 var aValue = a.getElementsByClassName("display")[0].innerHTML.toLowerC ase();
250 var bValue = b.getElementsByClassName("display")[0].innerHTML.toLowerC ase();
251 if (aValue < bValue)
252 return -1;
253 if (aValue > bValue)
254 return 1;
255 return 0;
256 }
257 if (aPopular == "true")
258 return 1;
259 else
260 return -1;
261 }).indexOf(subscription);
262 };
263
264 var checkBoxClick = function(e)
265 {
266 e.preventDefault();
267 toggleSubscription(subscription);
268 };
269
270 var appendToTable = function(table, elem)
271 {
272 var elements = table.getElementsByTagName("li");
273 if (elements.length == 0)
274 table.appendChild(elem);
275 else
276 {
277 var possition = getPossition(elements, elem);
Thomas Greiner 2015/01/30 13:55:52 This variable name is spelled incorrectly.
278 table.insertBefore(elem, table.childNodes[possition]);
279 }
280 };
281
282 if (subscription.type && subscription.type == "ads")
283 {
284 if (!subscription.isAdded)
285 {
286 var listElem = generateListElement(subscription, subscription.display, " add");
287 listElem.dataset.url = subscription.url;
288 listElem._subscription = subscription;
289 listElem.getElementsByTagName("button")[0].addEventListener("click", fun ction(e)
290 {
291 addSubscription(this.dataset.url);
292 }.bind(listElem), false);
293 appendToTable(E("all-lang-table"), listElem);
294 }
295 else
296 {
297 var listElem = generateListElement(subscription, display, "checkbox");
298 listElem.dataset.url = subscription.url;
299 listElem._subscription = subscription;
300 listElem.getElementsByTagName("input")[0].addEventListener("click", chec kBoxClick, false);
301 appendToTable(E("blocking-languages-table"), listElem);
302 var listElem = generateListElement(subscription, display);
303 listElem.dataset.url = subscription.url;
304 listElem._subscription = subscription;
305 appendToTable(E("blocking-languages-modal-table"), listElem);
306 }
307 }
308 else
309 {
310 var listElem = generateListElement(subscription, display, "checkbox");
311 listElem.dataset.url = subscription.url;
312 listElem._subscription = subscription;
313 listElem.getElementsByTagName("input")[0].addEventListener("click", checkB oxClick, false);
314 appendToTable(E("further-list-table"), listElem);
315 }
316 }
317
318 function addLanguageSubscription(subscription)
319 {
320 var optionSubscription = getOptionSubscription(subscription.url);
321 var elems = getElementsByUrl(subscription.url);
322 for (var i = 0; i < elems.length; i++)
Thomas Greiner 2015/01/30 13:55:52 Why are you removing elements when adding a subscr
saroyanm 2015/02/13 10:57:11 The item will stay there when it's added while we
323 elems[i].parentNode.removeChild(elems[i]);
324 optionSubscription.isAdded = true;
325 optionSubscription.disabled = false;
326 addOptionItem(optionSubscription);
327 }
328
329 function createWhitelistElem(domain)
330 {
331 var listElem = generateListElement(null, domain, "delete");
332 listElem.dataset.domain = domain;
333 listElem.getElementsByTagName("button")[0].addEventListener("click", removeW hitelistBtnClick.bind(listElem), false);
334 return listElem;
335 }
336
337 function addFurtherList(subscription)
Thomas Greiner 2015/01/30 13:55:52 We tend to call them "custom subscriptions" in our
saroyanm 2015/02/13 10:57:11 Done.
338 {
339 var optionSubscription = getOptionSubscription(subscription.url);
340 if (optionSubscription)
341 {
342 optionSubscription.disabled = false;
343 addOptionItem(optionSubscription);
344 }
345 else
346 {
347 optionSubscriptions[subscription.url] = subscription;
348 addOptionItem(subscription);
349 }
350 }
351
352 function updateSubscriptionState(subscription, state)
353 {
354 var elem = getElementsByUrl(subscription.url);
355 if (elem.length > 0)
356 {
357 for (var i = 0; i < elem.length; i++)
Thomas Greiner 2015/01/30 13:55:52 A subscription should only be shown once on the pa
saroyanm 2015/02/13 10:57:11 if you mean tab, then Language subscriptions shown
358 {
359 var checkbox = elem[i].getElementsByTagName("input")[0];
360 if (checkbox)
361 checkbox.checked = state;
Thomas Greiner 2015/01/30 13:55:52 Might make sense to implement some simple data-bin
362 }
363 }
364 else
365 {
366 if (subscription.url == acceptableAdsUrl)
367 E("allow-whitelist-cb").checked = state;
368 else
369 addFurtherList(subscription);
370 }
371 }
372
373 function getElementsByUrl(url)
374 {
375 return document.querySelectorAll("[data-url='"+url+"']");
376 }
377
378 function generateListElement(subscription, text, type)
379 {
380 var list = document.createElement("li");
381 if (type == "checkbox")
382 {
383 var input = document.createElement("input");
384 input.setAttribute("type", "checkbox");
385 if (subscription.disabled == false)
386 input.checked = true;
387 list.appendChild(input);
388 }
389 else if (type == "delete")
390 {
391 var button = document.createElement("button");
392 button.setAttribute("class", "delete");
393 list.appendChild(button);
394 }
395 else if (type == "add")
396 {
397 var button = document.createElement("button");
398 button.setAttribute("class", "button-add");
399 var span = document.createElement("span");
400 span.innerHTML = "+" + ext.i18n.getMessage("options_btn_add");
401 button.appendChild(span);
402 list.appendChild(button);
403 }
404 var span = document.createElement("span");
405 span.setAttribute("class", "display");
406 span.innerHTML = text;
407 list.appendChild(span);
408
409 if (subscription && subscription.popular == "true")
410 {
411 var popular = document.createElement("span");
412 popular.setAttribute("class", "popular");
413 popular.innerHTML = "popular";
414 list.appendChild(popular);
415 }
416
417 return list;
418 }
419
420 function getOptionSubscription(url)
421 {
422 return optionSubscriptions[url];
423 }
424
425 function importListBtnCLick()
426 {
427 var url = E("blockingList-textbox").value;
428 addSubscription(url);
429 Modal.close();
430 }
431
432 function whitelistDomainBtnClick()
433 {
434 var domain = E("whitelisting-textbox").value;
435 if (domain)
436 addWhitelistedDomain(domain);
437 }
438
439 function removeWhitelistBtnClick()
440 {
441 removeWhitelistedDomain(this.dataset.domain);
442 }
443
444 function editOwnRulsBtnClick()
445 {
446
447 }
448
449 function showAddSubscriptionDialog(action, subscription)
Thomas Greiner 2015/01/30 13:55:52 The `action` parameter is unused.
saroyanm 2015/02/13 10:57:11 Done.
450 {
451 E("blockingList-textbox").value = subscription.url;
452 Modal.open("further-blocking-modal");
453 }
454
455 function getAcceptableAdsURL(callback) 480 function getAcceptableAdsURL(callback)
456 { 481 {
457 ext.backgroundPage.sendMessage({ 482 ext.backgroundPage.sendMessage(
483 {
458 type: "prefs.get", 484 type: "prefs.get",
459 key: "subscriptions_exceptionsurl" 485 key: "subscriptions_exceptionsurl"
460 }, function(value) 486 },
487 function(value)
461 { 488 {
462 getAcceptableAdsURL = function(callback) 489 getAcceptableAdsURL = function(callback)
463 { 490 {
464 callback(value); 491 callback(value);
465 } 492 }
466 getAcceptableAdsURL(callback); 493 getAcceptableAdsURL(callback);
467 }); 494 });
468 } 495 }
469 496
470 function toggleSubscription(subscription) 497 function addEnableSubscription(url, title, homepage)
471 { 498 {
472 ext.backgroundPage.sendMessage({ 499 var messageType = null;
473 type: "subscriptions.toggle", 500 var knownSubscription = subscriptionsMap[url];
474 url: subscription.url, 501 if (knownSubscription && knownSubscription.disabled == true)
475 title: subscription.title, 502 messageType = "subscriptions.toggle"
476 homepage: subscription.homepage 503 else
477 }); 504 messageType = "subscriptions.add"
478 } 505
479
480 function toggleAcceptableAds(e)
481 {
482 e.preventDefault();
483 var acceptableCheckbox = this;
Thomas Greiner 2015/01/30 13:55:52 This variable is not necessary. You can access the
484 getAcceptableAdsURL(function(url)
485 {
486 var isChecked = !acceptableCheckbox.checked;
487 var title = "Allow non-intrusive advertising";
Thomas Greiner 2015/01/30 13:55:52 Those two values are only used once so no need to
488 if (isChecked)
489 removeSubscription(url);
490 else
491 addSubscription(url, title);
492 });
493 }
494
495 function addSubscription(url, title, homepage)
496 {
497 var message = { 506 var message = {
498 type: "subscriptions.add", 507 type: messageType,
499 url: url 508 url: url
500 }; 509 };
501 if (title) 510 if (title)
502 message.title = title; 511 message.title = title;
503 if (homepage) 512 if (homepage)
504 message.homepage = homepage; 513 message.homepage = homepage;
505 514
506 ext.backgroundPage.sendMessage(message); 515 ext.backgroundPage.sendMessage(message);
507 } 516 }
508 517
509 function removeSubscription(url) 518 function removeSubscription(url)
510 { 519 {
511 ext.backgroundPage.sendMessage({ 520 ext.backgroundPage.sendMessage(
521 {
512 type: "subscriptions.remove", 522 type: "subscriptions.remove",
513 url: url 523 url: url
514 }); 524 });
515 } 525 }
516 526
517 function addWhitelistedDomain(domain) 527 function removeFilter(filter)
518 { 528 {
519 ext.backgroundPage.sendMessage({ 529 ext.backgroundPage.sendMessage(
520 type: "filters.add", 530 {
521 text: "@@||" + domain.toLowerCase() + "^$document"
522 });
523 }
524
525 function removeWhitelistedDomain(domain)
526 {
527 ext.backgroundPage.sendMessage({
528 type: "filters.remove", 531 type: "filters.remove",
529 text: "@@||" + domain.toLowerCase() + "^$document" 532 text: filter
530 }); 533 });
531 } 534 }
532 535
533 function onFilterMessage(action, filter) 536 function onFilterMessage(action, filter)
534 { 537 {
535 switch (action) 538 switch (action)
536 { 539 {
537 case "added": 540 case "added":
538 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 541 updateFilter(filter);
539 if (match[1]) 542 updateShareLink();
Thomas Greiner 2015/01/30 13:55:52 `match` could be null which can cause an exception
saroyanm 2015/02/13 10:57:11 Done.
540 {
541 var whitelistTbl = E("whitelisting-table");
542 var items = whitelistTbl.getElementsByClassName("display");
543 var domains = [];
544 for (var i = 0; i < items.length; i++)
545 {
546 domains.push(items[i].innerHTML);
Thomas Greiner 2015/01/30 13:55:52 The content of an HTML element might change so bet
saroyanm 2015/02/13 10:57:11 Done.
547 }
548 var domain = match[1];
549 domains.push(domain);
550 domains.sort();
551
552 whitelistTbl.insertBefore(createWhitelistElem(domain), whitelistTbl.ch ildNodes[domains.indexOf(domain)]);
553 E("whitelisting-textbox").value = "";
554 }
555 else
556 {
557 // TODO: add `filters[i].text` to list of custom filters
558 }
559 break; 543 break;
560 case "loaded": 544 case "loaded":
561 populateLists(); 545 populateLists();
562 break; 546 break;
563 case "removed": 547 case "removed":
564 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 548 var knownFilter = filtersMap[filter.text];
565 if (match[1]) 549 collections.whitelist.removeItem(knownFilter);
Thomas Greiner 2015/01/30 13:55:52 As mentioned above, `match` could be null so bette
saroyanm 2015/02/13 10:57:11 Done.
566 { 550 delete filtersMap[filter.text];
567 var elem = document.querySelector("[data-domain='"+match[1]+"']"); 551 updateShareLink();
Thomas Greiner 2015/01/30 13:55:52 We're not restricted by bandwidth so you don't nee
Thomas Greiner 2015/01/30 13:55:52 Nit: Missing spaces around `+`s
568 elem.parentNode.removeChild(elem); 552 break;
569 } 553 }
570 break; 554 }
571 } 555
572 }
573
574 function onSubscriptionMessage(action, subscription) 556 function onSubscriptionMessage(action, subscription)
575 { 557 {
576 switch (action) 558 switch (action)
577 { 559 {
578 case "added": 560 case "added":
579 var optionSubscription = getOptionSubscription(subscription.url);
580 if (optionSubscription)
581 {
582 var isAdsType = optionSubscription.type && optionSubscription.type == "ads";
583 if (isAdsType && !optionSubscription.isAdded)
584 addLanguageSubscription(subscription);
585 else
586 updateSubscriptionState(subscription, true);
587 }
588 else if (subscription.url == acceptableAdsUrl)
589 updateSubscriptionState(subscription, true);
590 else
591 addFurtherList(subscription);
592 break;
593 case "disabled": 561 case "disabled":
594 updateSubscriptionState(subscription, false); 562 updateSubscription(subscription);
563 updateShareLink();
595 break; 564 break;
596 case "homepage": 565 case "homepage":
597 // TODO: NYI 566 // TODO: NYI
598 break; 567 break;
599 case "removed": 568 case "removed":
600 updateSubscriptionState(subscription, false); 569 getAcceptableAdsURL(function(acceptableAdsUrl)
570 {
571 if (subscription.url == acceptableAdsUrl)
572 {
573 subscription.disabled = true;
574 updateSubscription(subscription);
575 }
576 else
577 {
578 var knownSubscription = subscriptionsMap[subscription.url];
579 if (subscription.url in recommendationsMap)
580 knownSubscription.disabled = true;
581 else
582 {
583 collections.custom.removeItem(knownSubscription);
584 delete subscriptionsMap[subscription.url];
585 }
586 }
587 updateShareLink();
588 });
601 break; 589 break;
602 case "title": 590 case "title":
603 // TODO: NYI 591 // TODO: NYI
604 break; 592 break;
605 } 593 }
606 } 594 }
607 595
596 function showAddSubscriptionDialog(subscription)
597 {
598 E("blockingList-textbox").value = subscription.url;
599 openDialog("customlist");
600 }
601
608 function updateShareLink() 602 function updateShareLink()
609 { 603 {
610 ext.backgroundPage.sendMessage({ 604 ext.backgroundPage.sendMessage(
605 {
611 type: "filters.blocked", 606 type: "filters.blocked",
612 url: "https://platform.twitter.com/widgets/", 607 url: "https://platform.twitter.com/widgets/",
613 requestType: "SCRIPT", 608 requestType: "SCRIPT",
614 docDomain: "adblockplus.org", 609 docDomain: "adblockplus.org",
615 thirdParty: true 610 thirdParty: true
616 }, function(blocked) 611 },
612 function(blocked)
617 { 613 {
618 // TODO: modify "share" link accordingly 614 // TODO: modify "share" link accordingly
619 }); 615 });
620 } 616 }
621 617
622 function updateVersionNumber()
623 {
624 ext.backgroundPage.sendMessage({
625 method: "app.get",
Thomas Greiner 2015/01/30 13:55:52 Nit: Adjust indentation level
saroyanm 2015/02/13 10:57:11 Done.
626 what: "addonVersion"
627 }, function(addonVersion)
628 {
629 E("abp-version").innerHTML = addonVersion;
Thomas Greiner 2015/01/30 13:55:52 You're only changing the text content so use `text
saroyanm 2015/02/13 10:57:11 Done.
630 });
631 }
632
633 function getDocLink(link, callback)
634 {
635 ext.backgroundPage.sendMessage({
636 type: "app.get",
637 what: "doclink",
638 link: link
639 }, callback);
640 }
641
642 function setLinks(id)
Thomas Greiner 2015/01/30 13:55:52 Seems like you should be able to get rid of this f
643 {
644 var element = E(id);
645 if (!element)
646 {
647 return;
648 }
649
650 var links = element.getElementsByTagName("a");
651
652 for (var i = 0; i < links.length; i++)
653 {
654 if (typeof arguments[i + 1] == "string")
655 {
656 links[i].href = arguments[i + 1];
657 links[i].setAttribute("target", "_blank");
658 }
659 else if (typeof arguments[i + 1] == "function")
660 {
661 links[i].href = "javascript:void(0);";
662 links[i].addEventListener("click", arguments[i + 1], false);
663 }
664 }
665 }
666
667 function E(id) 618 function E(id)
668 { 619 {
669 return document.getElementById(id); 620 return document.getElementById(id);
670 } 621 }
671 622
672 ext.onMessage.addListener(function(message) 623 ext.onMessage.addListener(function(message)
673 { 624 {
674 switch (message.type) 625 switch (message.type)
675 { 626 {
676 case "app.listen": 627 case "app.listen":
677 if (message.action == "addSubscription") 628 if (message.action == "addSubscription")
678 { 629 showAddSubscriptionDialog(message.args[0]);
679 message.args.unshift(message.action);
Thomas Greiner 2015/01/30 13:55:52 You don't even use `message.action` in `showAddSub
saroyanm 2015/02/13 10:57:11 Done.
680 showAddSubscriptionDialog.apply(null, message.args);
Thomas Greiner 2015/01/30 13:55:52 `showAddSubscription` only requires the first argu
saroyanm 2015/02/13 10:57:11 Done.
681 }
682 break; 630 break;
683 case "filters.listen": 631 case "filters.listen":
684 message.args.unshift(message.action); 632 onFilterMessage(message.action, message.args[0]);
685 onFilterMessage.apply(null, message.args);
Thomas Greiner 2015/01/30 13:55:52 `onFilterMessage` only expects two parameters so l
saroyanm 2015/02/13 10:57:11 Done.
686 break; 633 break;
687 case "subscriptions.listen": 634 case "subscriptions.listen":
688 message.args.unshift(message.action); 635 onSubscriptionMessage(message.action, message.args[0]);
689 onSubscriptionMessage.apply(null, message.args);
690 break; 636 break;
691 } 637 }
692 }); 638 });
693 639
694 ext.backgroundPage.sendMessage({ 640 ext.backgroundPage.sendMessage(
641 {
695 type: "app.listen", 642 type: "app.listen",
696 filter: ["addSubscription"] 643 filter: ["addSubscription"]
697 }); 644 });
698 ext.backgroundPage.sendMessage({ 645 ext.backgroundPage.sendMessage(
646 {
699 type: "filters.listen", 647 type: "filters.listen",
700 filter: ["added", "loaded", "removed"] 648 filter: ["added", "loaded", "removed"]
701 }); 649 });
702 ext.backgroundPage.sendMessage({ 650 ext.backgroundPage.sendMessage(
651 {
703 type: "subscriptions.listen", 652 type: "subscriptions.listen",
704 filter: ["added", "disabled", "homepage", "removed", "title"] 653 filter: ["added", "disabled", "homepage", "removed", "title"]
705 }); 654 });
706 655
707 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 656 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
708 })(); 657 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld