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

Side by Side Diff: composer.postload.js

Issue 29370947: Issue 3138 - Improve how context menu "block element" handles iframes (Closed)
Patch Set: Addressed feedback Created Jan. 12, 2017, 7:14 a.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 | « chrome/ext/background.js ('k') | lib/filterComposer.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 baseURL: document.location.href 56 baseURL: document.location.href
57 }, 57 },
58 response => 58 response =>
59 { 59 {
60 callback(response.filters, response.selectors); 60 callback(response.filters, response.selectors);
61 }); 61 });
62 } 62 }
63 63
64 function getBlockableElementOrAncestor(element, callback) 64 function getBlockableElementOrAncestor(element, callback)
65 { 65 {
66 // If we're offering to block the iframe element given by window.frameElement
67 // we must use the context of the parent frame.
68 let document = element.ownerDocument;
69 let HTMLElement = document.defaultView.HTMLElement;
70
66 // We assume that the user doesn't want to block the whole page. 71 // We assume that the user doesn't want to block the whole page.
67 // So we never consider the <html> or <body> element. 72 // So we never consider the <html> or <body> element.
68 while (element && element != document.documentElement && 73 while (element && element != document.documentElement &&
69 element != document.body) 74 element != document.body)
70 { 75 {
71 // We can't handle non-HTML (like SVG) elements, as well as 76 // We can't handle non-HTML (like SVG) elements, as well as
72 // <area> elements (see below). So fall back to the parent element. 77 // <area> elements (see below). So fall back to the parent element.
73 if (!(element instanceof HTMLElement) || element.localName == "area") 78 if (!(element instanceof HTMLElement) || element.localName == "area")
74 element = element.parentElement; 79 element = element.parentElement;
75 80
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // We reached the document root without finding a blockable element. 119 // We reached the document root without finding a blockable element.
115 callback(null); 120 callback(null);
116 } 121 }
117 122
118 123
119 /* Element highlighting */ 124 /* Element highlighting */
120 125
121 // Adds an overlay to an element in order to highlight it. 126 // Adds an overlay to an element in order to highlight it.
122 function addElementOverlay(element) 127 function addElementOverlay(element)
123 { 128 {
129 let document = element.ownerDocument;
130
124 let position = "absolute"; 131 let position = "absolute";
125 let offsetX = window.scrollX; 132 let offsetX = window.scrollX;
126 let offsetY = window.scrollY; 133 let offsetY = window.scrollY;
127 134
128 for (let e = element; e; e = e.parentElement) 135 for (let e = element; e; e = e.parentElement)
129 { 136 {
130 let style = getComputedStyle(e); 137 let style = getComputedStyle(e);
131 138
132 // If the element isn't rendered (since its or one of its ancestor's 139 // If the element isn't rendered (since its or one of its ancestor's
133 // "display" property is "none"), the overlay wouldn't match the element. 140 // "display" property is "none"), the overlay wouldn't match the element.
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 }); 431 });
425 } 432 }
426 }); 433 });
427 434
428 if (selectors.length > 0) 435 if (selectors.length > 0)
429 highlightElements(selectors.join(",")); 436 highlightElements(selectors.join(","));
430 437
431 highlightElement(currentElement, "#fd1708", "#f6a1b5"); 438 highlightElement(currentElement, "#fd1708", "#f6a1b5");
432 }); 439 });
433 440
434 event.preventDefault(); 441 if (event)
435 event.stopPropagation(); 442 {
443 event.preventDefault();
444 event.stopPropagation();
445 }
436 } 446 }
437 447
438 function stopPickingElement() 448 function stopPickingElement()
439 { 449 {
440 currentlyPickingElement = false; 450 currentlyPickingElement = false;
441 451
442 document.removeEventListener("mousedown", stopEventPropagation, true); 452 document.removeEventListener("mousedown", stopEventPropagation, true);
443 document.removeEventListener("mouseup", stopEventPropagation, true); 453 document.removeEventListener("mouseup", stopEventPropagation, true);
444 document.removeEventListener("mouseenter", stopEventPropagation, true); 454 document.removeEventListener("mouseenter", stopEventPropagation, true);
445 document.removeEventListener("mouseleave", stopEventPropagation, true); 455 document.removeEventListener("mouseleave", stopEventPropagation, true);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 sendResponse({ 533 sendResponse({
524 active: currentlyPickingElement || blockelementPopupId != null 534 active: currentlyPickingElement || blockelementPopupId != null
525 }); 535 });
526 break; 536 break;
527 case "composer.content.startPickingElement": 537 case "composer.content.startPickingElement":
528 if (window == window.top) 538 if (window == window.top)
529 startPickingElement(); 539 startPickingElement();
530 break; 540 break;
531 case "composer.content.contextMenuClicked": 541 case "composer.content.contextMenuClicked":
532 let event = lastRightClickEvent; 542 let event = lastRightClickEvent;
543 let target = event && event.target;
544
545 // When the user attempts to block an element inside an iframe for which
546 // our right click event listener was trashed the best we can do is to
547 // offer to block the entire iframe. This of course only works if the
548 // parent frame is considered to be of the same origin.
549 // Note: Since Edge doesn't yet support per-frame messaging[1] we
550 // can't use this workaround there yet. (The contextMenuClicked message
551 // will be sent to all of the page's frames.)
552 // [1] - https://developer.microsoft.com/en-us/microsoft-edge/platform/d ocumentation/extensions/api-support/supported-apis/
553 if (!target && window.frameElement && typeof chrome != "undefined")
554 target = addElementOverlay(window.frameElement);
555
533 deactivateBlockElement(); 556 deactivateBlockElement();
534 if (event) 557 if (target)
535 { 558 {
536 getBlockableElementOrAncestor(event.target, element => 559 getBlockableElementOrAncestor(target, element =>
537 { 560 {
538 if (element) 561 if (element)
539 { 562 {
540 currentElement = element; 563 currentElement = element;
541 elementPicked(event); 564 elementPicked(event);
542 } 565 }
543 }); 566 });
544 } 567 }
545 break; 568 break;
546 case "composer.content.finished": 569 case "composer.content.finished":
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 604 }
582 }); 605 });
583 } 606 }
584 break; 607 break;
585 } 608 }
586 }); 609 });
587 610
588 if (window == window.top) 611 if (window == window.top)
589 ext.backgroundPage.sendMessage({type: "composer.ready"}); 612 ext.backgroundPage.sendMessage({type: "composer.ready"});
590 } 613 }
OLDNEW
« no previous file with comments | « chrome/ext/background.js ('k') | lib/filterComposer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld