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

Delta Between Two Patch Sets: include.postload.js

Issue 5945877571043328: Issue 705 - Generate element hiding instead request blocking filters for non-HTTP URLs (Closed)
Left Patch Set: Rebased Created Oct. 23, 2014, 11:15 a.m.
Right Patch Set: Rebased Created Oct. 30, 2014, 4:07 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 | « no previous file | no next file » | 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 <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 clickHideFilters = null; 22 var clickHideFilters = null;
23 var highlightedElementsSelector = null; 23 var highlightedElementsSelector = null;
24 var clickHideFiltersDialog = null; 24 var clickHideFiltersDialog = null;
25 var lastRightClickEvent = null; 25 var lastRightClickEvent = null;
26
27 function quote(value)
28 {
29 return '"' + value.replace(/(["\\])/g, "\\$1") + '"';
30 }
26 31
27 function supportsShadowRoot(element) 32 function supportsShadowRoot(element)
28 { 33 {
29 if (!("createShadowRoot" in element)) 34 if (!("createShadowRoot" in element))
30 return false; 35 return false;
31 36
32 // There are some elements (e.g. <textarea>), which don't 37 // There are some elements (e.g. <textarea>), which don't
33 // support author created shadow roots and throw an exception. 38 // support author created shadow roots and throw an exception.
34 var clone = element.cloneNode(false); 39 var clone = element.cloneNode(false);
35 try 40 try
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, false); 338 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, false);
334 } 339 }
335 340
336 // Selects the currently hovered-over filter or cancels selection 341 // Selects the currently hovered-over filter or cancels selection
337 function clickHide_keyDown(e) 342 function clickHide_keyDown(e)
338 { 343 {
339 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /) 344 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /)
340 clickHide_mouseClick(e); 345 clickHide_mouseClick(e);
341 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/) 346 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/)
342 { 347 {
343 clickHide_deactivate(); 348 ext.backgroundPage.sendMessage(
349 {
350 type: "forward",
351 payload:
352 {
353 type: "clickhide-deactivate"
354 }
355 });
344 e.preventDefault(); 356 e.preventDefault();
345 e.stopPropagation(); 357 e.stopPropagation();
346 } 358 }
347 } 359 }
348 360
349 // When the user clicks, the currentElement is the one we want. 361 // When the user clicks, the currentElement is the one we want.
350 // We should have ABP rules ready for when the 362 // We should have ABP rules ready for when the
351 // popup asks for them. 363 // popup asks for them.
352 function clickHide_mouseClick(e) 364 function clickHide_mouseClick(e)
353 { 365 {
(...skipping 20 matching lines...) Expand all
374 386
375 clickHideFilters = new Array(); 387 clickHideFilters = new Array();
376 selectorList = new Array(); 388 selectorList = new Array();
377 389
378 var addSelector = function(selector) 390 var addSelector = function(selector)
379 { 391 {
380 clickHideFilters.push(document.domain + "##" + selector); 392 clickHideFilters.push(document.domain + "##" + selector);
381 selectorList.push(selector); 393 selectorList.push(selector);
382 }; 394 };
383 395
384 var addSelectorFromAttribute = function(element, attribute)
385 {
386 var value = element.getAttribute(attribute);
387 if (value)
388 addSelector(element.localName + "[" + attribute + '="' + value.replace(/"/ g, '\\"') + '"]');
Wladimir Palant 2014/10/24 17:56:55 This isn't a sufficient escaping approach. First o
Sebastian Noack 2014/10/30 16:36:24 The attribute name is hard-coded in the calling co
389 };
390
391 if (elementId) 396 if (elementId)
392 addSelector("#" + elementId); 397 addSelector("#" + elementId);
393 398
394 if (elementClasses && elementClasses.length > 0) 399 if (elementClasses && elementClasses.length > 0)
395 { 400 {
396 var selector = ""; 401 var selector = "";
397 402
398 for (var i = 0; i < elt.classList.length; i++) 403 for (var i = 0; i < elt.classList.length; i++)
399 selector += "." + elt.classList[i].replace(/([^\w-])/, "\\$1"); 404 selector += "." + elt.classList[i].replace(/([^\w-])/, "\\$1");
400 405
401 addSelector(selector); 406 addSelector(selector);
402 } 407 }
403 408
404 if (url) 409 if (url)
405 { 410 {
411 var src = elt.getAttribute("src");
412 var selector = src && elt.localName + '[src=' + quote(src) + ']';
Wladimir Palant 2014/10/30 22:36:28 elt.localName still needs escaping, DOM methods al
Sebastian Noack 2014/10/31 13:52:49 The syntax for valid tag names is very restrictive
Wladimir Palant 2014/11/03 18:14:28 document.createElement("foo:bar") might still be p
413
406 if (/^https?:/i.test(url)) 414 if (/^https?:/i.test(url))
407 { 415 {
408 clickHideFilters.push(url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||")); 416 clickHideFilters.push(url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"));
409 selectorList.push('[src="' + elt.getAttribute("src") + '"]'); 417
418 if (selector)
419 selectorList.push(selector);
410 } 420 }
411 else 421 else if (selector)
412 addSelectorFromAttribute(elt, "src"); 422 addSelector(selector);
413 } 423 }
414
415 // Show popup
416 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters);
417 424
418 // restore the original style, before generating the fallback filter that 425 // restore the original style, before generating the fallback filter that
419 // will include the style, and to prevent highlightElements from saving those 426 // will include the style, and to prevent highlightElements from saving those
420 unhighlightElement(currentElement); 427 unhighlightElement(currentElement);
421 428
422 // as last resort, create a filter based on inline styles 429 // as last resort, create a filter based on inline styles
423 if (clickHideFilters.length == 0) 430 if (clickHideFilters.length == 0)
424 addSelectorFromAttribute(elt, "style"); 431 {
432 var style = elt.getAttribute("style");
433 if (style)
434 addSelector(elt.localName + '[style=' + quote(style) + ']');
435 }
436
437 // Show popup
438 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters);
425 439
426 // Highlight the elements specified by selector in yellow 440 // Highlight the elements specified by selector in yellow
427 highlightElements(selectorList.join(",")); 441 highlightElements(selectorList.join(","));
428 // Now, actually highlight the element the user clicked on in red 442 // Now, actually highlight the element the user clicked on in red
429 highlightElement(currentElement, "#fd1708", "#f6a1b5"); 443 highlightElement(currentElement, "#fd1708", "#f6a1b5");
430 444
431 // Make sure the browser doesn't handle this click 445 // Make sure the browser doesn't handle this click
432 e.preventDefault(); 446 e.preventDefault();
433 e.stopPropagation(); 447 e.stopPropagation();
434 } 448 }
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 clickHide_activated = true; 622 clickHide_activated = true;
609 // FIXME: clickHideFilters is erased in clickHide_mouseClick anyway, s o why set it? 623 // FIXME: clickHideFilters is erased in clickHide_mouseClick anyway, s o why set it?
610 clickHideFilters = [msg.filter]; 624 clickHideFilters = [msg.filter];
611 // Coerce red highlighted overlay on top of element to remove. 625 // Coerce red highlighted overlay on top of element to remove.
612 // TODO: Wow, the design of the clickHide stuff is really dumb - gotta fix it sometime 626 // TODO: Wow, the design of the clickHide stuff is really dumb - gotta fix it sometime
613 currentElement = addElementOverlay(target); 627 currentElement = addElementOverlay(target);
614 // clickHide_mouseOver(lastRightClickEvent); 628 // clickHide_mouseOver(lastRightClickEvent);
615 clickHide_mouseClick(lastRightClickEvent); 629 clickHide_mouseClick(lastRightClickEvent);
616 } 630 }
617 else 631 else
618 console.log("clickhide-new-filter: URLs don't match. Couldn't find tha t element.", request.filter, url, lastRightClickEvent.target.src); 632 console.log("clickhide-new-filter: URLs don't match. Couldn't find tha t element.", msg.filter, url, lastRightClickEvent.target.src);
619 break; 633 break;
620 case "clickhide-init": 634 case "clickhide-init":
621 if (clickHideFiltersDialog) 635 if (clickHideFiltersDialog)
622 { 636 {
623 sendResponse({filters: clickHide_filters}); 637 sendResponse({filters: clickHide_filters});
624 638
625 clickHideFiltersDialog.style.width = msg.width + "px"; 639 clickHideFiltersDialog.style.width = msg.width + "px";
626 clickHideFiltersDialog.style.height = msg.height + "px"; 640 clickHideFiltersDialog.style.height = msg.height + "px";
627 clickHideFiltersDialog.style.visibility = "visible"; 641 clickHideFiltersDialog.style.visibility = "visible";
628 } 642 }
629 break; 643 break;
630 case "clickhide-move": 644 case "clickhide-move":
631 if (clickHideFiltersDialog) 645 if (clickHideFiltersDialog)
632 { 646 {
633 clickHideFiltersDialog.style.left = (parseInt(clickHideFiltersDialog.s tyle.left, 10) + request.x) + "px"; 647 clickHideFiltersDialog.style.left = (parseInt(clickHideFiltersDialog.s tyle.left, 10) + msg.x) + "px";
634 clickHideFiltersDialog.style.top = (parseInt(clickHideFiltersDialog.st yle.top, 10) + request.y) + "px"; 648 clickHideFiltersDialog.style.top = (parseInt(clickHideFiltersDialog.st yle.top, 10) + msg.y) + "px";
635 } 649 }
636 break; 650 break;
637 case "clickhide-close": 651 case "clickhide-close":
638 if (clickHideFiltersDialog) 652 if (clickHideFiltersDialog)
639 { 653 {
640 // Explicitly get rid of currentElement 654 // Explicitly get rid of currentElement
641 if (msg.remove && currentElement && currentElement.parentNode) 655 if (msg.remove && currentElement && currentElement.parentNode)
642 currentElement.parentNode.removeChild(currentElement); 656 currentElement.parentNode.removeChild(currentElement);
643
644 clickHide_deactivate();
645 } 657 }
658 clickHide_deactivate();
646 break; 659 break;
647 default: 660 default:
648 sendResponse({}); 661 sendResponse({});
649 break; 662 break;
650 } 663 }
651 }); 664 });
652 665
653 if (window == window.top) 666 if (window == window.top)
654 ext.backgroundPage.sendMessage({type: "report-html-page"}); 667 ext.backgroundPage.sendMessage({type: "report-html-page"});
655 } 668 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld