OLD | NEW |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 } | 472 } |
473 unhighlightElements(); | 473 unhighlightElements(); |
474 | 474 |
475 let overlays = document.getElementsByClassName("__adblockplus__overlay"); | 475 let overlays = document.getElementsByClassName("__adblockplus__overlay"); |
476 while (overlays.length > 0) | 476 while (overlays.length > 0) |
477 overlays[0].parentNode.removeChild(overlays[0]); | 477 overlays[0].parentNode.removeChild(overlays[0]); |
478 | 478 |
479 ext.onExtensionUnloaded.removeListener(deactivateBlockElement); | 479 ext.onExtensionUnloaded.removeListener(deactivateBlockElement); |
480 } | 480 } |
481 | 481 |
482 if (document instanceof HTMLDocument) | 482 function setupComposer() |
483 { | 483 { |
484 // Use a contextmenu handler to save the last element the user right-clicked | 484 if (document instanceof HTMLDocument) |
485 // on. To make things easier, we actually save the DOM event. We have to do | |
486 // this because the contextMenu API only provides a URL, not the actual DOM | |
487 // element. | |
488 // We also need to make sure that the previous right click event, | |
489 // if there is one, is removed. We don't know which frame it is in so we must | |
490 // send a message to the other frames to clear their old right click events. | |
491 document.addEventListener("contextmenu", event => | |
492 { | 485 { |
493 lastRightClickEvent = event; | 486 // Use a contextmenu handler to save the last element the user right-clicked |
494 lastRightClickEventIsMostRecent = true; | 487 // on. To make things easier, we actually save the DOM event. We have to do |
| 488 // this because the contextMenu API only provides a URL, not the actual DOM |
| 489 // element. |
| 490 // We also need to make sure that the previous right click event, |
| 491 // if there is one, is removed. We don't know which frame it is in so we |
| 492 // must send a message to the other frames to clear their old right click |
| 493 // events. |
| 494 document.addEventListener("contextmenu", event => |
| 495 { |
| 496 lastRightClickEvent = event; |
| 497 lastRightClickEventIsMostRecent = true; |
495 | 498 |
496 ext.backgroundPage.sendMessage({ | 499 ext.backgroundPage.sendMessage({ |
497 type: "forward", | 500 type: "forward", |
498 payload: | 501 payload: |
| 502 { |
| 503 type: "composer.content.clearPreviousRightClickEvent" |
| 504 } |
| 505 }); |
| 506 }, true); |
| 507 |
| 508 ext.onMessage.addListener((msg, sender, sendResponse) => |
| 509 { |
| 510 switch (msg.type) |
499 { | 511 { |
500 type: "composer.content.clearPreviousRightClickEvent" | 512 case "composer.content.getState": |
| 513 if (window == window.top) |
| 514 { |
| 515 sendResponse({ |
| 516 active: currentlyPickingElement || blockelementPopupId != null |
| 517 }); |
| 518 } |
| 519 break; |
| 520 case "composer.content.startPickingElement": |
| 521 if (window == window.top) |
| 522 startPickingElement(); |
| 523 break; |
| 524 case "composer.content.contextMenuClicked": |
| 525 let event = lastRightClickEvent; |
| 526 deactivateBlockElement(); |
| 527 if (event) |
| 528 { |
| 529 getBlockableElementOrAncestor(event.target, element => |
| 530 { |
| 531 if (element) |
| 532 { |
| 533 currentElement = element; |
| 534 elementPicked(event); |
| 535 } |
| 536 }); |
| 537 } |
| 538 break; |
| 539 case "composer.content.finished": |
| 540 if (currentElement && msg.remove) |
| 541 { |
| 542 // Hide the selected element itself if an added blocking |
| 543 // filter is causing it to collapse. Note that this |
| 544 // behavior is incomplete, but the best we can do here, |
| 545 // e.g. if an added blocking filter matches other elements, |
| 546 // the effect won't be visible until the page is is reloaded. |
| 547 checkCollapse(currentElement.prisoner || currentElement); |
| 548 |
| 549 // Apply added element hiding filters. |
| 550 elemhide.apply(); |
| 551 } |
| 552 deactivateBlockElement(); |
| 553 break; |
| 554 case "composer.content.clearPreviousRightClickEvent": |
| 555 if (!lastRightClickEventIsMostRecent) |
| 556 lastRightClickEvent = null; |
| 557 lastRightClickEventIsMostRecent = false; |
| 558 break; |
| 559 case "composer.content.dialogOpened": |
| 560 if (window == window.top) |
| 561 blockelementPopupId = msg.popupId; |
| 562 break; |
| 563 case "composer.content.dialogClosed": |
| 564 // The onRemoved hook for the popup can create a race condition, so we |
| 565 // to be careful here. (This is not perfect, but best we can do.) |
| 566 if (window == window.top && blockelementPopupId == msg.popupId) |
| 567 { |
| 568 ext.backgroundPage.sendMessage({ |
| 569 type: "forward", |
| 570 payload: |
| 571 { |
| 572 type: "composer.content.finished" |
| 573 } |
| 574 }); |
| 575 } |
| 576 break; |
501 } | 577 } |
502 }); | 578 }); |
503 }, true); | |
504 | 579 |
505 ext.onMessage.addListener((msg, sender, sendResponse) => | 580 if (window == window.top) |
506 { | 581 ext.backgroundPage.sendMessage({type: "composer.ready"}); |
507 switch (msg.type) | 582 } |
508 { | 583 } |
509 case "composer.content.getState": | |
510 if (window == window.top) | |
511 { | |
512 sendResponse({ | |
513 active: currentlyPickingElement || blockelementPopupId != null | |
514 }); | |
515 } | |
516 break; | |
517 case "composer.content.startPickingElement": | |
518 if (window == window.top) | |
519 startPickingElement(); | |
520 break; | |
521 case "composer.content.contextMenuClicked": | |
522 let event = lastRightClickEvent; | |
523 deactivateBlockElement(); | |
524 if (event) | |
525 { | |
526 getBlockableElementOrAncestor(event.target, element => | |
527 { | |
528 if (element) | |
529 { | |
530 currentElement = element; | |
531 elementPicked(event); | |
532 } | |
533 }); | |
534 } | |
535 break; | |
536 case "composer.content.finished": | |
537 if (currentElement && msg.remove) | |
538 { | |
539 // Hide the selected element itself if an added blocking | |
540 // filter is causing it to collapse. Note that this | |
541 // behavior is incomplete, but the best we can do here, | |
542 // e.g. if an added blocking filter matches other elements, | |
543 // the effect won't be visible until the page is is reloaded. | |
544 checkCollapse(currentElement.prisoner || currentElement); | |
545 | 584 |
546 // Apply added element hiding filters. | 585 exports.setupComposer = setupComposer; |
547 elemhide.apply(); | |
548 } | |
549 deactivateBlockElement(); | |
550 break; | |
551 case "composer.content.clearPreviousRightClickEvent": | |
552 if (!lastRightClickEventIsMostRecent) | |
553 lastRightClickEvent = null; | |
554 lastRightClickEventIsMostRecent = false; | |
555 break; | |
556 case "composer.content.dialogOpened": | |
557 if (window == window.top) | |
558 blockelementPopupId = msg.popupId; | |
559 break; | |
560 case "composer.content.dialogClosed": | |
561 // The onRemoved hook for the popup can create a race condition, so we | |
562 // to be careful here. (This is not perfect, but best we can do.) | |
563 if (window == window.top && blockelementPopupId == msg.popupId) | |
564 { | |
565 ext.backgroundPage.sendMessage({ | |
566 type: "forward", | |
567 payload: | |
568 { | |
569 type: "composer.content.finished" | |
570 } | |
571 }); | |
572 } | |
573 break; | |
574 } | |
575 }); | |
576 | |
577 if (window == window.top) | |
578 ext.backgroundPage.sendMessage({type: "composer.ready"}); | |
579 } | |
OLD | NEW |