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

Powered by Google App Engine
This is Rietveld