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

Side by Side Diff: include.postload.js

Issue 6174977720057856: Issue 1853 - Moved filter generation into background page (Closed)
Patch Set: Created Jan. 25, 2015, 1:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « background.js ('k') | include.preload.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-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 // Click-to-hide stuff 18 // Click-to-hide stuff
19 var clickHide_activated = false; 19 var clickHide_activated = false;
20 var clickHide_filters = null; 20 var clickHide_filters = null;
21 var currentElement = null; 21 var currentElement = null;
22 var highlightedElementsSelector = null; 22 var highlightedElementsSelector = null;
23 var clickHideFiltersDialog = null; 23 var clickHideFiltersDialog = null;
24 var lastRightClickEvent = null; 24 var lastRightClickEvent = null;
25 var lastRightClickEventValid = false; 25 var lastRightClickEventValid = false;
26 26
27 function escapeChar(chr)
28 {
29 var code = chr.charCodeAt(0);
30
31 // Control characters and leading digits must be escaped based on
32 // their char code in CSS. Moreover, curly brackets aren't allowed
33 // in elemhide filters, and therefore must be escaped based on their
34 // char code as well.
35 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr))
36 return "\\" + code.toString(16) + " ";
37
38 return "\\" + chr;
39 }
40
41 function quote(value)
42 {
43 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"';
44 }
45
46 function escapeCSS(s)
47 {
48 return s.replace(/^[\d\-]|[^\w\-\u0080-\uFFFF]/g, escapeChar);
49 }
50
51 function highlightElement(element, shadowColor, backgroundColor) 27 function highlightElement(element, shadowColor, backgroundColor)
52 { 28 {
53 unhighlightElement(element); 29 unhighlightElement(element);
54 30
55 var highlightWithOverlay = function() 31 var highlightWithOverlay = function()
56 { 32 {
57 var overlay = addElementOverlay(element); 33 var overlay = addElementOverlay(element);
58 34
59 highlightElement(overlay, shadowColor, backgroundColor); 35 highlightElement(overlay, shadowColor, backgroundColor);
60 overlay.style.pointerEvents = "none"; 36 overlay.style.pointerEvents = "none";
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 ); 108 );
133 109
134 highlightedElementsSelector = null; 110 highlightedElementsSelector = null;
135 } 111 }
136 } 112 }
137 113
138 function getURLsFromObjectElement(element) 114 function getURLsFromObjectElement(element)
139 { 115 {
140 var url = element.getAttribute("data"); 116 var url = element.getAttribute("data");
141 if (url) 117 if (url)
142 return [resolveURL(url)]; 118 return [url];
143 119
144 for (var i = 0; i < element.children.length; i++) 120 for (var i = 0; i < element.children.length; i++)
145 { 121 {
146 var child = element.children[i]; 122 var child = element.children[i];
147 if (child.localName != "param") 123 if (child.localName != "param")
148 continue; 124 continue;
149 125
150 var name = child.getAttribute("name"); 126 var name = child.getAttribute("name");
151 if (name != "movie" && // Adobe Flash 127 if (name != "movie" && // Adobe Flash
152 name != "source" && // Silverlight 128 name != "source" && // Silverlight
153 name != "src" && // Real Media + Quicktime 129 name != "src" && // Real Media + Quicktime
154 name != "FileName") // Windows Media 130 name != "FileName") // Windows Media
155 continue; 131 continue;
156 132
157 var value = child.getAttribute("value"); 133 var value = child.getAttribute("value");
158 if (!value) 134 if (!value)
159 continue; 135 continue;
160 136
161 return [resolveURL(value)]; 137 return [value];
162 } 138 }
163 139
164 return []; 140 return [];
165 } 141 }
166 142
167 function getURLsFromAttributes(element) 143 function getURLsFromAttributes(element)
168 { 144 {
169 var urls = []; 145 var urls = [];
170 146
171 if (element.src) 147 if (element.src)
172 urls.push(element.src); 148 urls.push(element.src);
173 149
174 if (element.srcset) 150 if (element.srcset)
175 { 151 {
176 var candidates = element.srcset.split(","); 152 var candidates = element.srcset.split(",");
177 for (var i = 0; i < candidates.length; i++) 153 for (var i = 0; i < candidates.length; i++)
178 { 154 {
179 var url = candidates[i].trim().replace(/\s+\S+$/, ""); 155 var url = candidates[i].trim().replace(/\s+\S+$/, "");
180 if (url) 156 if (url)
181 urls.push(resolveURL(url)); 157 urls.push(url);
182 } 158 }
183 } 159 }
184 160
185 return urls; 161 return urls;
186 } 162 }
187 163
188 function getURLsFromMediaElement(element) 164 function getURLsFromMediaElement(element)
189 { 165 {
190 var urls = getURLsFromAttributes(element); 166 var urls = getURLsFromAttributes(element);
191 167
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 payload: 432 payload:
457 { 433 {
458 type: "clickhide-deactivate" 434 type: "clickhide-deactivate"
459 } 435 }
460 }); 436 });
461 e.preventDefault(); 437 e.preventDefault();
462 e.stopPropagation(); 438 e.stopPropagation();
463 } 439 }
464 } 440 }
465 441
442 function getFiltersForElement(element, callback)
443 {
444 ext.backgroundPage.sendMessage(
445 {
446 type: "compose-filters",
447 tagName: element.localName,
448 id: element.id,
449 src: element.getAttribute("src"),
450 style: element.getAttribute("style"),
451 classes: [].slice.call(element.classList),
452 urls: getURLsFromElement(element),
453 baseURL: document.location.href
454 },
455 function(response)
456 {
457 callback(response.filters, response.selectors);
458 }
459 );
460 }
461
466 // When the user clicks, the currentElement is the one we want. 462 // When the user clicks, the currentElement is the one we want.
467 // We should have ABP rules ready for when the 463 // We should have ABP rules ready for when the
468 // popup asks for them. 464 // popup asks for them.
469 function clickHide_mouseClick(e) 465 function clickHide_mouseClick(e)
470 { 466 {
471 if (!currentElement || !clickHide_activated) 467 if (!currentElement || !clickHide_activated)
472 return; 468 return;
473 469
474 var elt = currentElement; 470 var elt = currentElement;
475 if (currentElement.classList.contains("__adblockplus__overlay")) 471 if (currentElement.classList.contains("__adblockplus__overlay"))
476 elt = currentElement.prisoner; 472 elt = currentElement.prisoner;
477 473
478 var clickHideFilters = []; 474 getFiltersForElement(elt, function(filters, selectors)
479 var selectorList = [];
480
481 var addSelector = function(selector)
482 { 475 {
483 if (selectorList.indexOf(selector) != -1)
484 return;
485
486 clickHideFilters.push(document.domain + "##" + selector);
487 selectorList.push(selector);
488 };
489
490 if (elt.id)
491 addSelector("#" + escapeCSS(elt.id));
492
493 if (elt.classList.length > 0)
494 {
495 var selector = "";
496
497 for (var i = 0; i < elt.classList.length; i++)
498 selector += "." + escapeCSS(elt.classList[i]);
499
500 addSelector(selector);
501 }
502
503 var urls = getURLsFromElement(elt);
504 for (var i = 0; i < urls.length; i++)
505 {
506 var url = urls[i];
507
508 if (/^https?:/i.test(url))
509 {
510 var filter = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
511
512 if (clickHideFilters.indexOf(filter) == -1)
513 clickHideFilters.push(filter);
514
515 continue;
516 }
517
518 if (url == elt.src)
519 addSelector(escapeCSS(elt.localName) + '[src=' + quote(elt.getAttribute("s rc")) + ']');
520 }
521
522 // as last resort, create a filter based on inline styles
523 if (clickHideFilters.length == 0)
524 {
525 var style = elt.getAttribute("style");
526 if (style)
527 addSelector(escapeCSS(elt.localName) + '[style=' + quote(style) + ']');
528 }
529
530 // Show popup, or if inside frame tell the parent to do it
531 if (window.self == window.top)
532 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters);
533 else
534 ext.backgroundPage.sendMessage( 476 ext.backgroundPage.sendMessage(
535 { 477 {
536 type: "forward", 478 type: "forward",
537 payload: 479 payload:
538 { 480 {
539 type: "clickhide-show-dialog", 481 type: "clickhide-show-dialog",
540 screenX: e.screenX, 482 screenX: e.screenX,
541 screenY: e.screenY, 483 screenY: e.screenY,
542 clickHideFilters: clickHideFilters 484 clickHideFilters: filters
543 } 485 }
544 }); 486 });
545 487
kzar 2015/01/26 10:35:16 Why remove the comments explaining that we're high
Sebastian Noack 2015/01/26 10:44:07 A while ago we agreed to don't spam code with comm
kzar 2015/01/26 10:49:55 These comments were useful and directly helped me
Sebastian Noack 2015/01/26 11:08:14 Those comments doesn't add any information that ar
kzar 2015/01/26 14:21:28 Yes they do. These two lines do not explain that a
Sebastian Noack 2015/01/26 15:11:34 Actually nothing is highlighted yellow here. highl
546 // Highlight the elements specified by selector in yellow 488 if (selectors.length > 0)
547 if (selectorList.length > 0) 489 highlightElements(selectors.join(","));
548 highlightElements(selectorList.join(",")); 490
549 // Now, actually highlight the element the user clicked on in red 491 highlightElement(currentElement, "#fd1708", "#f6a1b5");
550 highlightElement(currentElement, "#fd1708", "#f6a1b5"); 492 });
551 493
552 // Make sure the browser doesn't handle this click 494 // Make sure the browser doesn't handle this click
553 e.preventDefault(); 495 e.preventDefault();
554 e.stopPropagation(); 496 e.stopPropagation();
555 } 497 }
556 498
557 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js 499 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js
558 // and licensed under the MIT license. See jquery-*.min.js for details. 500 // and licensed under the MIT license. See jquery-*.min.js for details.
559 function removeDotSegments(u) { 501 function removeDotSegments(u) {
560 var r = '', m = []; 502 var r = '', m = [];
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 lastRightClickEventValid = false; 671 lastRightClickEventValid = false;
730 else 672 else
731 lastRightClickEvent = null; 673 lastRightClickEvent = null;
732 break; 674 break;
733 } 675 }
734 }); 676 });
735 677
736 if (window == window.top) 678 if (window == window.top)
737 ext.backgroundPage.sendMessage({type: "report-html-page"}); 679 ext.backgroundPage.sendMessage({type: "report-html-page"});
738 } 680 }
OLDNEW
« no previous file with comments | « background.js ('k') | include.preload.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld