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. 21, 2015, 6:52 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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
Thomas Greiner 2015/01/22 17:56:30 Nit: Change to "https"
saroyanm 2015/01/23 18:18:39 Done.
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");
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
28 updateShareLink(); 328 updateShareLink();
29 populateLists(); 329 populateLists();
30 330
31 E("add-blocking-list").addEventListener("click", Modal.open, false); 331 var tabList = document.querySelectorAll("#main-navigation-tabs li");
32 E("add-website-language").addEventListener("click", Modal.open, false); 332 for (var i = 0; i < tabList.length; i++)
33 E("modal-close").addEventListener("click", Modal.close, false); 333 {
34 E("whitelisting-add-icon").addEventListener("click", whitelistDomainBtnClick , false); 334 tabList[i].addEventListener("click", function(e)
35 E("whitelisting-add-btn").addEventListener("click", whitelistDomainBtnClick, false); 335 {
36 E("whitelisting-enter-icon").addEventListener("click", whitelistDomainBtnCli ck, false); 336 document.body.dataset.tab = e.currentTarget.dataset.show;
37 E("whitelisting-textbox").addEventListener("keypress", function(e) { 337 }, false);
38 if (e.keyCode == 13) 338 }
39 whitelistDomainBtnClick(); 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 });
359
360 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find");
361 E("find-language").setAttribute("placeholder", placeholderValue);
362 E("add-blocking-list").addEventListener("click", function()
363 {
364 openDialog("customlist");
40 }, false); 365 }, false);
41 E("whitelisting-cancel-btn").addEventListener("click", function(){ 366 E("add-website-language").addEventListener("click", function()
42 E("whitelisting-textbox").value = ""; 367 {
368 openDialog("language");
43 }, false); 369 }, false);
44 E("allow-whitelist-cb").addEventListener("click", toggleAcceptableAds, false ); 370 E("dialog-close").addEventListener("click", function()
45 E("import-blockingList-btn").addEventListener("click", importListBtnCLick, f alse); 371 {
46 E("edit-ownBlockingList-btn").addEventListener("click", editOwnRulsBtnClick, false); 372 delete document.body.dataset.dialog;
47 E("find-language").addEventListener("keyup", searchLanguage, false); 373 }, false);
48 } 374 E("edit-ownBlockingList-button").addEventListener("click", editCustomFilters , false);
49 375 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false);
50 function initTabs() 376 E("whitelisting").addEventListener("click", function(e)
51 { 377 {
52 var showContent = function(tab) 378 var id = e.target.id;
53 { 379 if (id == "whitelisting-add-icon" || id == "whitelisting-enter-icon")
54 var tab = tab.querySelector(".tabs li.active"); 380 addWhitelistedDomain();
55 if (tab.dataset.show) 381 else if (id == "whitelisting-cancel-button")
56 E(tab.dataset.show).style.display = "block"; 382 E("whitelisting-textbox").value = "";
57 }; 383 }, false);
58 var optionList = document.querySelectorAll('.tabs li[data-show]'); 384 E("whitelisting-add-button").addEventListener("click", addWhitelistedDomain, false);
59 for (var i = 0; i < optionList.length; ++i) 385 E("whitelisting-textbox").addEventListener("keypress", function(e)
60 { 386 {
61 optionList[i].addEventListener("click", function(ev) 387 // e.keyCode has been deprecated so we attempt to use e.key
62 { 388 // keyCode "13" corresponds to "Enter"
63 var tab = this.parentNode.querySelector(".active"); 389 if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
64 tab.classList.remove("active"); 390 addWhitelistedDomain();
65 this.classList.add("active"); 391 }, false);
66 E(tab.dataset.show).style.display = "none";; 392 E("import-blockingList-button").addEventListener("click", function()
67 showContent(this.parentNode); 393 {
68 }, false); 394 var url = E("blockingList-textbox").value;
69 } 395 addEnableSubscription(url);
70 showContent(E("main-navigation-tabs")); 396 delete document.body.dataset.dialog;
71 showContent(E("blocking-list-tabs")); 397 }, false);
72 } 398 }
73 399
74 var Modal = 400 function openDialog(name)
75 { 401 {
76 open: function (content) 402 document.body.dataset.dialog = name;
77 { 403 }
78 var modal = E("modal"); 404
79 var content = E(this && this.dataset ? this.dataset.show : content);
80 content.style.display = "block";
81 modal.style.visibility = "visible";
82 E("modal-background").style.display = "block";
83 if (content.dataset.title)
84 E("modal-title").innerHTML = ext.i18n.getMessage(content.dataset.title);
85 modal.style.marginTop = -(modal.clientHeight/2)+"px";
86 },
87 close: function ()
88 {
89 var contents = E("modal-content").childNodes;
90 for (var i = 0; i < contents.length; ++i)
91 {
92 if (contents[i].style)
93 contents[i].style.display = "none";
94 }
95 E("modal-background").style.display = "none";
96 E("modal").style.visibility = "hidden";
97 }
98 }
99
100 function populateLists() 405 function populateLists()
101 { 406 {
102 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 {
103 type: "subscriptions.get", 417 type: "subscriptions.get",
104 special: true 418 special: true
105 }, function(subscriptions) 419 },
106 { 420 function(subscriptions)
421 {
422 // Load filters
107 for (var i = 0; i < subscriptions.length; i++) 423 for (var i = 0; i < subscriptions.length; i++)
108 { 424 {
109 ext.backgroundPage.sendMessage({ 425 ext.backgroundPage.sendMessage(
426 {
110 type: "filters.get", 427 type: "filters.get",
111 subscriptionUrl: subscriptions[i].url 428 subscriptionUrl: subscriptions[i].url
112 }, function(filters) 429 },
113 { 430 function(filters)
114 var whitelistArray = []; 431 {
115 for (var i = 0; i < filters.length; i++) 432 for (var i = 0; i < filters.length; i++)
116 { 433 updateFilter(filters[i]);
117 var match = filters[i].text.match(/^@@\|\|([^\/:]+)\^\$document$/);
118 if (match[1])
119 {
120 whitelistArray.push(match[1]);
121 }
122 else
123 {
124 // TODO: add `filters[i].text` to list of custom filters
125 }
126 }
127
128 if (whitelistArray.length > 0)
129 {
130 whitelistArray.sort();
131 for (var i = 0; i < whitelistArray.length; i++)
132 {
133 var domain = whitelistArray[i];
134 E("whitelisting-table").appendChild(createWhitelistElem(domain));
135 }
136 }
137 }); 434 });
138 } 435 }
139 }); 436 });
140 437 loadRecommendations();
141 loadRecommendations(function(recommends) 438 getAcceptableAdsURL(function(acceptableAdsUrl)
142 { 439 {
143 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 {
144 type: "subscriptions.get", 449 type: "subscriptions.get",
145 downloadable: true 450 downloadable: true
146 }, function(subscriptions) 451 },
147 { 452 function(subscriptions)
148 getAcceptableAdsURL(function(url) 453 {
149 { 454 for (var i = 0; i < subscriptions.length; i++)
150 acceptableAdsUrl = url; 455 onSubscriptionMessage("added", subscriptions[i]);
151 for (var i = 0; i < subscriptions.length; i++)
152 {
153 if (subscriptions[i].url == acceptableAdsUrl)
154 {
155 E("allow-whitelist-cb").previousSibling.checked = !subscriptions[i ].disabled;
156 continue;
157 }
158
159 var subscription = recommends[subscriptions[i].url];
160 if (!subscription)
161 recommends[subscriptions[i].url] = subscriptions[i];
162 else
163 {
164 subscription.disabled = subscriptions[i].disabled;
165 if (subscription.type == "ads")
166 subscription.isAdded = true;
167 }
168 }
169 for (var key in recommends)
170 addOptionItem(recommends[key]);
171 });
172 }); 456 });
173 }); 457 });
174 } 458 }
175 459
176 function loadRecommendations(callback) 460 function addWhitelistedDomain()
177 { 461 {
178 var recommendations = {}; 462 var domain = E("whitelisting-textbox");
179 var request = new XMLHttpRequest(); 463 if (domain.value)
180 request.open("GET", "subscriptions.xml"); 464 {
181 request.onload = function() 465 ext.backgroundPage.sendMessage(
182 { 466 {
183 var list = document.getElementById("subscriptionSelector"); 467 type: "filters.add",
184 var elements = request.responseXML.documentElement.getElementsByTagName("s ubscription"); 468 text: "@@||" + domain.value.toLowerCase() + "^$document"
185 for (var i = 0; i < elements.length; i++) 469 });
186 { 470 }
187 var element = elements[i]; 471
188 var subscription = {}; 472 domain.value = "";
189 subscription.title = element.getAttribute("title"); 473 }
190 subscription.url = element.getAttribute("url"); 474
191 subscription.disabled = true; 475 function editCustomFilters()
192 var prefix = element.getAttribute("prefixes"); 476 {
193 if (prefix) 477 //TODO: NYI
194 { 478 }
195 subscription.prefixes = element.getAttribute("prefixes"); 479
196 subscription.type = "ads";
197 subscription.display = ext.i18n.getMessage("options_language_"+subscri ption.prefixes.replace(/,/g, '_'));
198 }
199 else
200 subscription.display = element.getAttribute("specialization");
201
202 var popular = element.getAttribute("popular");
203 if (popular)
204 subscription.popular = element.getAttribute("popular");
205
206 recommendations[subscription.url] = subscription;
207 }
208 optionSubscriptions = recommendations;
209 callback(recommendations);
210 }
211 request.send();
212 }
213
214 function searchLanguage()
215 {
216 var searchVal = this.value;
217 var items = E("all-lang-table").childNodes;
218 for (var i = 0; i < items.length; ++i)
219 {
220 var item = items[i];
221 var language = item.getElementsByTagName("span")[1].innerHTML;
222 if (language.toLowerCase().indexOf(searchVal.toLowerCase()) > -1)
223 item.style.display = "block";
224 else
225 item.style.display = "none";
226 }
227 }
228
229 function addOptionItem(subscription)
230 {
231 var display = subscription.display ? subscription.display : subscription.tit le;
232 var getPossition = function(elements, subscription)
233 {
234 var localArray = [];
235 for (var i = 0; i < elements.length; i++)
236 {
237 var elem = elements[i];
238 localArray.push(elem);
239 }
240
241 localArray.push(subscription);
242 return localArray.sort(function(a, b) {
243 var aPopular = a.getElementsByClassName("popular").length > 0;
244 var bPopular = b.getElementsByClassName("popular").length > 0;
245 if(aPopular == bPopular)
246 {
247 var aValue = a.getElementsByClassName("display")[0].innerHTML.toLowerC ase();
248 var bValue = b.getElementsByClassName("display")[0].innerHTML.toLowerC ase();
249 if (aValue < bValue)
250 return -1;
251 if (aValue > bValue)
252 return 1;
253 return 0;
254 }
255 if (aPopular == "true")
256 return 1;
257 else
258 return -1;
259 }).indexOf(subscription);
260 };
261
262 var checkBoxClick = function()
263 {
264 toggleSubscription(subscription);
265 };
266
267 var appendToTable = function(table, elem)
268 {
269 var elements = table.getElementsByTagName("li");
270 if (elements.length == 0)
271 table.appendChild(elem);
272 else
273 {
274 var possition = getPossition(elements, elem);
275 table.insertBefore(elem, table.childNodes[possition]);
276 }
277 };
278
279 if (subscription.type && subscription.type == "ads")
280 {
281 if (!subscription.isAdded)
282 {
283 var listElem = generateListElement(subscription, subscription.display, " add");
284 listElem.dataset.url = subscription.url;
285 listElem._subscription = subscription;
286 listElem.getElementsByTagName("button")[0].addEventListener("click", fun ction()
287 {
288 addSubscription(this.dataset.url);
289 }.bind(listElem), false);
290 appendToTable(E("all-lang-table"), listElem);
291 }
292 else
293 {
294 var listElem = generateListElement(subscription, display, "checkbox");
295 listElem.dataset.url = subscription.url;
296 listElem._subscription = subscription;
297 listElem.getElementsByTagName("label")[0].addEventListener("click", chec kBoxClick, false);
298 appendToTable(E("blocking-languages-table"), listElem);
299 var listElem = generateListElement(subscription, display);
300 listElem.dataset.url = subscription.url;
301 listElem._subscription = subscription;
302 appendToTable(E("blocking-languages-modal-table"), listElem);
303 }
304 }
305 else
306 {
307 var listElem = generateListElement(subscription, display, "checkbox");
308 listElem.dataset.url = subscription.url;
309 listElem._subscription = subscription;
310 listElem.getElementsByTagName("label")[0].addEventListener("click", checkB oxClick, false);
311 appendToTable(E("further-list-table"), listElem);
312 }
313 }
314
315 function addLanguageSubscription(subscription)
316 {
317 var optionSubscription = getOptionSubscription(subscription.url);
318 var elems = getElementsByUrl(subscription.url);
319 for (var i = 0; i < elems.length; i++)
320 elems[i].parentNode.removeChild(elems[i]);
321 optionSubscription.isAdded = true;
322 optionSubscription.disabled = false;
323 addOptionItem(optionSubscription);
324 }
325
326 function createWhitelistElem(domain)
327 {
328 var listElem = generateListElement(null, domain, "delete");
329 listElem.dataset.domain = domain;
330 listElem.getElementsByTagName("button")[0].addEventListener("click", removeW hitelistBtnClick.bind(listElem), false);
331 return listElem;
332 }
333
334 function addFurtherList(subscription)
335 {
336 var optionSubscription = getOptionSubscription(subscription.url);
337 if (optionSubscription)
338 {
339 optionSubscription.disabled = false;
340 addOptionItem(optionSubscription);
341 }
342 else
343 {
344 optionSubscriptions[subscription.url] = subscription;
345 addOptionItem(subscription);
346 }
347 }
348
349 function updateSubscriptionState(subscription, state)
350 {
351 var elem = getElementsByUrl(subscription.url);
352 if (elem.length > 0)
353 {
354 for (var i = 0; i < elem.length; i++)
355 {
356 var checkbox = elem[i].getElementsByTagName("input")[0];
357 if (checkbox)
358 checkbox.checked = state;
359 }
360 }
361 else
362 {
363 if (subscription.url == acceptableAdsUrl)
364 E("allow-whitelist-cb").previousSibling.checked = state;
365 else
366 addFurtherList(subscription);
367 }
368 }
369
370 function getElementsByUrl(url)
371 {
372 return document.querySelectorAll("[data-url='"+url+"']");
373 }
374
375 function generateListElement(subscription, text, type)
376 {
377 var list = document.createElement("li");
378 if (type == "checkbox")
379 {
380 var input = document.createElement("input");
381 input.setAttribute("type", "checkbox");
382 if (subscription.disabled == false)
383 input.checked = true;
384 list.appendChild(input);
385 var label = document.createElement("label");
386 list.appendChild(label);
387 }
388 else if (type == "delete")
389 {
390 var button = document.createElement("button");
391 button.setAttribute("class", "delete");
392 list.appendChild(button);
393 }
394 else if (type == "add")
395 {
396 var button = document.createElement("button");
397 button.setAttribute("class", "addbtn");
398 var span = document.createElement("span");
399 span.innerHTML = "+" + ext.i18n.getMessage("options_btn_add");
400 button.appendChild(span);
401 list.appendChild(button);
402 }
403 var span = document.createElement("span");
404 span.setAttribute("class", "display");
405 span.innerHTML = text;
406 list.appendChild(span);
407
408 if (subscription && subscription.popular == "true")
409 {
410 var popular = document.createElement("span");
411 popular.setAttribute("class", "popular");
412 popular.innerHTML = "popular";
413 list.appendChild(popular);
414 }
415
416 return list;
417 }
418
419 function getOptionSubscription(url)
420 {
421 return optionSubscriptions[url];
422 }
423
424 function importListBtnCLick()
425 {
426 var url = E("blockingList-textbox").value;
427 addSubscription(url);
428 Modal.close();
429 }
430
431 function whitelistDomainBtnClick()
432 {
433 var domain = E("whitelisting-textbox").value;
434 if (domain)
435 addWhitelistedDomain(domain);
436 }
437
438 function removeWhitelistBtnClick()
439 {
440 removeWhitelistedDomain(this.dataset.domain);
441 }
442
443 function editOwnRulsBtnClick()
444 {
445
446 }
447
448 function showAddSubscriptionDialog(action, subscription)
449 {
450 E("blockingList-textbox").value = subscription.url;
451 Modal.open("further-blocking-modal");
452 }
453
454 function getAcceptableAdsURL(callback) 480 function getAcceptableAdsURL(callback)
455 { 481 {
456 ext.backgroundPage.sendMessage({ 482 ext.backgroundPage.sendMessage(
483 {
457 type: "prefs.get", 484 type: "prefs.get",
458 key: "subscriptions_exceptionsurl" 485 key: "subscriptions_exceptionsurl"
459 }, function(value) 486 },
487 function(value)
460 { 488 {
461 getAcceptableAdsURL = function(callback) 489 getAcceptableAdsURL = function(callback)
462 { 490 {
463 callback(value); 491 callback(value);
464 } 492 }
465 getAcceptableAdsURL(callback); 493 getAcceptableAdsURL(callback);
466 }); 494 });
467 } 495 }
468 496
469 function toggleSubscription(subscription) 497 function addEnableSubscription(url, title, homepage)
470 { 498 {
471 ext.backgroundPage.sendMessage({ 499 var messageType = null;
472 type: "subscriptions.toggle", 500 var knownSubscription = subscriptionsMap[url];
473 url: subscription.url, 501 if (knownSubscription && knownSubscription.disabled == true)
474 title: subscription.title, 502 messageType = "subscriptions.toggle"
475 homepage: subscription.homepage 503 else
476 }); 504 messageType = "subscriptions.add"
477 } 505
478
479 function toggleAcceptableAds()
480 {
481 var acceptableCheckbox = this.previousSibling;
482 getAcceptableAdsURL(function(url)
483 {
484 var isChecked = acceptableCheckbox.checked;
485 var title = "Allow non-intrusive advertising";
486 if (isChecked)
487 removeSubscription(url);
488 else
489 addSubscription(url, title);
490 });
491 }
492
493 function addSubscription(url, title, homepage)
494 {
495 var message = { 506 var message = {
496 type: "subscriptions.add", 507 type: messageType,
497 url: url 508 url: url
498 }; 509 };
499 if (title) 510 if (title)
500 message.title = title; 511 message.title = title;
501 if (homepage) 512 if (homepage)
502 message.homepage = homepage; 513 message.homepage = homepage;
503 514
504 ext.backgroundPage.sendMessage(message); 515 ext.backgroundPage.sendMessage(message);
505 } 516 }
506 517
507 function removeSubscription(url) 518 function removeSubscription(url)
508 { 519 {
509 ext.backgroundPage.sendMessage({ 520 ext.backgroundPage.sendMessage(
521 {
510 type: "subscriptions.remove", 522 type: "subscriptions.remove",
511 url: url 523 url: url
512 }); 524 });
513 } 525 }
514 526
515 function addWhitelistedDomain(domain) 527 function removeFilter(filter)
516 { 528 {
517 ext.backgroundPage.sendMessage({ 529 ext.backgroundPage.sendMessage(
518 type: "filters.add", 530 {
519 text: "@@||" + domain.toLowerCase() + "^$document"
520 });
521 }
522
523 function removeWhitelistedDomain(domain)
524 {
525 ext.backgroundPage.sendMessage({
526 type: "filters.remove", 531 type: "filters.remove",
527 text: "@@||" + domain.toLowerCase() + "^$document" 532 text: filter
528 }); 533 });
529 } 534 }
530 535
531 function onFilterMessage(action, filter) 536 function onFilterMessage(action, filter)
532 { 537 {
533 switch (action) 538 switch (action)
534 { 539 {
535 case "added": 540 case "added":
536 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 541 updateFilter(filter);
537 if (match[1]) 542 updateShareLink();
538 {
539 var whitelistTbl = E("whitelisting-table");
540 var items = whitelistTbl.getElementsByClassName("display");
541 var domains = [];
542 for (var i = 0; i < items.length; i++)
543 {
544 domains.push(items[i].innerHTML);
545 }
546 var domain = match[1];
547 domains.push(domain);
548 domains.sort();
549
550 whitelistTbl.insertBefore(createWhitelistElem(domain), whitelistTbl.ch ildNodes[domains.indexOf(domain)]);
551 E("whitelisting-textbox").value = "";
552 }
553 else
554 {
555 // TODO: add `filters[i].text` to list of custom filters
556 }
557 break; 543 break;
558 case "loaded": 544 case "loaded":
559 populateLists(); 545 populateLists();
560 break; 546 break;
561 case "removed": 547 case "removed":
562 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 548 var knownFilter = filtersMap[filter.text];
563 if (match[1]) 549 collections.whitelist.removeItem(knownFilter);
564 { 550 delete filtersMap[filter.text];
565 var elem = document.querySelector("[data-domain='"+match[1]+"']"); 551 updateShareLink();
566 elem.parentNode.removeChild(elem); 552 break;
567 } 553 }
568 break; 554 }
569 } 555
570 }
571
572 function onSubscriptionMessage(action, subscription) 556 function onSubscriptionMessage(action, subscription)
573 { 557 {
574 switch (action) 558 switch (action)
575 { 559 {
576 case "added": 560 case "added":
577 var optionSubscription = getOptionSubscription(subscription.url);
578 if (optionSubscription)
579 {
580 var isAdsType = optionSubscription.type && optionSubscription.type == "ads";
581 if (isAdsType && !optionSubscription.isAdded)
582 addLanguageSubscription(subscription);
583 else
584 updateSubscriptionState(subscription, true);
585 }
586 else if (subscription.url == acceptableAdsUrl)
587 updateSubscriptionState(subscription, true);
588 else
589 addFurtherList(subscription);
590 break;
591 case "disabled": 561 case "disabled":
592 updateSubscriptionState(subscription, false); 562 updateSubscription(subscription);
563 updateShareLink();
593 break; 564 break;
594 case "homepage": 565 case "homepage":
595 // TODO: NYI 566 // TODO: NYI
596 break; 567 break;
597 case "removed": 568 case "removed":
598 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 });
599 break; 589 break;
600 case "title": 590 case "title":
601 // TODO: NYI 591 // TODO: NYI
602 break; 592 break;
603 } 593 }
604 } 594 }
605 595
596 function showAddSubscriptionDialog(subscription)
597 {
598 E("blockingList-textbox").value = subscription.url;
599 openDialog("customlist");
600 }
601
606 function updateShareLink() 602 function updateShareLink()
607 { 603 {
608 ext.backgroundPage.sendMessage({ 604 ext.backgroundPage.sendMessage(
605 {
609 type: "filters.blocked", 606 type: "filters.blocked",
610 url: "https://platform.twitter.com/widgets/", 607 url: "https://platform.twitter.com/widgets/",
611 requestType: "SCRIPT", 608 requestType: "SCRIPT",
612 docDomain: "adblockplus.org", 609 docDomain: "adblockplus.org",
613 thirdParty: true 610 thirdParty: true
614 }, function(blocked) 611 },
612 function(blocked)
615 { 613 {
616 // TODO: modify "share" link accordingly 614 // TODO: modify "share" link accordingly
617 }); 615 });
618 } 616 }
619 617
620 function getDocLink(link, callback)
621 {
622 ext.backgroundPage.sendMessage({
623 type: "app.get",
624 what: "doclink",
625 link: link
626 }, callback);
627 }
628
629 function E(id) 618 function E(id)
630 { 619 {
631 return document.getElementById(id); 620 return document.getElementById(id);
632 } 621 }
633 622
634 ext.onMessage.addListener(function(message) 623 ext.onMessage.addListener(function(message)
635 { 624 {
636 switch (message.type) 625 switch (message.type)
637 { 626 {
638 case "app.listen": 627 case "app.listen":
639 if (message.action == "addSubscription") 628 if (message.action == "addSubscription")
640 { 629 showAddSubscriptionDialog(message.args[0]);
641 message.args.unshift(message.action);
642 showAddSubscriptionDialog.apply(null, message.args);
643 }
644 break; 630 break;
645 case "filters.listen": 631 case "filters.listen":
646 message.args.unshift(message.action); 632 onFilterMessage(message.action, message.args[0]);
647 onFilterMessage.apply(null, message.args);
648 break; 633 break;
649 case "subscriptions.listen": 634 case "subscriptions.listen":
650 message.args.unshift(message.action); 635 onSubscriptionMessage(message.action, message.args[0]);
651 onSubscriptionMessage.apply(null, message.args);
652 break; 636 break;
653 } 637 }
654 }); 638 });
655 639
656 ext.backgroundPage.sendMessage({ 640 ext.backgroundPage.sendMessage(
641 {
657 type: "app.listen", 642 type: "app.listen",
658 filter: ["addSubscription"] 643 filter: ["addSubscription"]
659 }); 644 });
660 ext.backgroundPage.sendMessage({ 645 ext.backgroundPage.sendMessage(
646 {
661 type: "filters.listen", 647 type: "filters.listen",
662 filter: ["added", "loaded", "removed"] 648 filter: ["added", "loaded", "removed"]
663 }); 649 });
664 ext.backgroundPage.sendMessage({ 650 ext.backgroundPage.sendMessage(
651 {
665 type: "subscriptions.listen", 652 type: "subscriptions.listen",
666 filter: ["added", "disabled", "homepage", "removed", "title"] 653 filter: ["added", "disabled", "homepage", "removed", "title"]
667 }); 654 });
668 655
669 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 656 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
670 })(); 657 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld