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: Rebased and fixed documentation mistakenly indicating arguments as optional Created Feb. 28, 2015, 5:59 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 // If the element isn't displayed no overlay will be added. 35 // If the element isn't displayed no overlay will be added.
60 // Moreover, we don't need to highlight anything then. 36 // Moreover, we don't need to highlight anything then.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 ); 113 );
138 114
139 highlightedElementsSelector = null; 115 highlightedElementsSelector = null;
140 } 116 }
141 } 117 }
142 118
143 function getURLsFromObjectElement(element) 119 function getURLsFromObjectElement(element)
144 { 120 {
145 var url = element.getAttribute("data"); 121 var url = element.getAttribute("data");
146 if (url) 122 if (url)
147 return [resolveURL(url)]; 123 return [url];
148 124
149 for (var i = 0; i < element.children.length; i++) 125 for (var i = 0; i < element.children.length; i++)
150 { 126 {
151 var child = element.children[i]; 127 var child = element.children[i];
152 if (child.localName != "param") 128 if (child.localName != "param")
153 continue; 129 continue;
154 130
155 var name = child.getAttribute("name"); 131 var name = child.getAttribute("name");
156 if (name != "movie" && // Adobe Flash 132 if (name != "movie" && // Adobe Flash
157 name != "source" && // Silverlight 133 name != "source" && // Silverlight
158 name != "src" && // Real Media + Quicktime 134 name != "src" && // Real Media + Quicktime
159 name != "FileName") // Windows Media 135 name != "FileName") // Windows Media
160 continue; 136 continue;
161 137
162 var value = child.getAttribute("value"); 138 var value = child.getAttribute("value");
163 if (!value) 139 if (!value)
164 continue; 140 continue;
165 141
166 return [resolveURL(value)]; 142 return [value];
167 } 143 }
168 144
169 return []; 145 return [];
170 } 146 }
171 147
172 function getURLsFromAttributes(element) 148 function getURLsFromAttributes(element)
173 { 149 {
174 var urls = []; 150 var urls = [];
175 151
176 if (element.src) 152 if (element.src)
177 urls.push(element.src); 153 urls.push(element.src);
178 154
179 if (element.srcset) 155 if (element.srcset)
180 { 156 {
181 var candidates = element.srcset.split(","); 157 var candidates = element.srcset.split(",");
182 for (var i = 0; i < candidates.length; i++) 158 for (var i = 0; i < candidates.length; i++)
183 { 159 {
184 var url = candidates[i].trim().replace(/\s+\S+$/, ""); 160 var url = candidates[i].trim().replace(/\s+\S+$/, "");
185 if (url) 161 if (url)
186 urls.push(resolveURL(url)); 162 urls.push(url);
187 } 163 }
188 } 164 }
189 165
190 return urls; 166 return urls;
191 } 167 }
192 168
193 function getURLsFromMediaElement(element) 169 function getURLsFromMediaElement(element)
194 { 170 {
195 var urls = getURLsFromAttributes(element); 171 var urls = getURLsFromAttributes(element);
196 172
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 payload: 478 payload:
503 { 479 {
504 type: "clickhide-deactivate" 480 type: "clickhide-deactivate"
505 } 481 }
506 }); 482 });
507 e.preventDefault(); 483 e.preventDefault();
508 e.stopPropagation(); 484 e.stopPropagation();
509 } 485 }
510 } 486 }
511 487
488 function getFiltersForElement(element, callback)
489 {
490 ext.backgroundPage.sendMessage(
491 {
492 type: "compose-filters",
493 tagName: element.localName,
494 id: element.id,
495 src: element.getAttribute("src"),
496 style: element.getAttribute("style"),
497 classes: [].slice.call(element.classList),
498 urls: getURLsFromElement(element),
499 baseURL: document.location.href
500 },
501 function(response)
502 {
503 callback(response.filters, response.selectors);
504 }
505 );
506 }
507
512 // When the user clicks, the currentElement is the one we want. 508 // When the user clicks, the currentElement is the one we want.
513 // We should have ABP rules ready for when the 509 // We should have ABP rules ready for when the
514 // popup asks for them. 510 // popup asks for them.
515 function clickHide_mouseClick(e) 511 function clickHide_mouseClick(e)
516 { 512 {
517 if (!currentElement || !clickHide_activated) 513 if (!currentElement || !clickHide_activated)
518 return; 514 return;
519 515
520 var elt = currentElement; 516 var elt = currentElement;
521 if (currentElement.classList.contains("__adblockplus__overlay")) 517 if (currentElement.classList.contains("__adblockplus__overlay"))
522 elt = currentElement.prisoner; 518 elt = currentElement.prisoner;
523 519
524 var clickHideFilters = []; 520 getFiltersForElement(elt, function(filters, selectors)
525 var selectorList = [];
526
527 var addSelector = function(selector)
528 { 521 {
529 if (selectorList.indexOf(selector) != -1)
530 return;
531
532 clickHideFilters.push(document.domain + "##" + selector);
533 selectorList.push(selector);
534 };
535
536 if (elt.id)
537 addSelector("#" + escapeCSS(elt.id));
538
539 if (elt.classList.length > 0)
540 {
541 var selector = "";
542
543 for (var i = 0; i < elt.classList.length; i++)
544 selector += "." + escapeCSS(elt.classList[i]);
545
546 addSelector(selector);
547 }
548
549 var urls = getURLsFromElement(elt);
550 for (var i = 0; i < urls.length; i++)
551 {
552 var url = urls[i];
553
554 if (/^https?:/i.test(url))
555 {
556 var filter = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
557
558 if (clickHideFilters.indexOf(filter) == -1)
559 clickHideFilters.push(filter);
560
561 continue;
562 }
563
564 if (url == elt.src)
565 addSelector(escapeCSS(elt.localName) + '[src=' + quote(elt.getAttribute("s rc")) + ']');
566 }
567
568 // as last resort, create a filter based on inline styles
569 if (clickHideFilters.length == 0)
570 {
571 var style = elt.getAttribute("style");
572 if (style)
573 addSelector(escapeCSS(elt.localName) + '[style=' + quote(style) + ']');
574 }
575
576 // Show popup, or if inside frame tell the parent to do it
577 if (window.self == window.top)
578 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters);
579 else
580 ext.backgroundPage.sendMessage( 522 ext.backgroundPage.sendMessage(
581 { 523 {
582 type: "forward", 524 type: "forward",
583 payload: 525 payload:
584 { 526 {
585 type: "clickhide-show-dialog", 527 type: "clickhide-show-dialog",
586 screenX: e.screenX, 528 screenX: e.screenX,
587 screenY: e.screenY, 529 screenY: e.screenY,
588 clickHideFilters: clickHideFilters 530 clickHideFilters: filters
589 } 531 }
590 }); 532 });
591 533
592 // Highlight the elements specified by selector in yellow 534 if (selectors.length > 0)
593 if (selectorList.length > 0) 535 highlightElements(selectors.join(","));
594 highlightElements(selectorList.join(",")); 536
595 // Now, actually highlight the element the user clicked on in red 537 highlightElement(currentElement, "#fd1708", "#f6a1b5");
596 highlightElement(currentElement, "#fd1708", "#f6a1b5"); 538 });
597 539
598 // Make sure the browser doesn't handle this click 540 // Make sure the browser doesn't handle this click
599 e.preventDefault(); 541 e.preventDefault();
600 e.stopPropagation(); 542 e.stopPropagation();
601 } 543 }
602 544
603 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js 545 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js
604 // and licensed under the MIT license. See jquery-*.min.js for details. 546 // and licensed under the MIT license. See jquery-*.min.js for details.
605 function removeDotSegments(u) { 547 function removeDotSegments(u) {
606 var r = '', m = []; 548 var r = '', m = [];
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 lastRightClickEventValid = false; 717 lastRightClickEventValid = false;
776 else 718 else
777 lastRightClickEvent = null; 719 lastRightClickEvent = null;
778 break; 720 break;
779 } 721 }
780 }); 722 });
781 723
782 if (window == window.top) 724 if (window == window.top)
783 ext.backgroundPage.sendMessage({type: "report-html-page"}); 725 ext.backgroundPage.sendMessage({type: "report-html-page"});
784 } 726 }
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