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

Side by Side Diff: composer.postload.js

Issue 29581892: Issue 5599 - Delay composer initialization if ext object is unavailable (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Oct. 17, 2017, 4:41 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 | « no previous file | no next file » | 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-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
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 initializeComposer()
483 { 483 {
484 if (typeof ext == "undefined")
485 return false;
486
484 // Use a contextmenu handler to save the last element the user right-clicked 487 // Use a contextmenu handler to save the last element the user right-clicked
485 // on. To make things easier, we actually save the DOM event. We have to do 488 // 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 489 // this because the contextMenu API only provides a URL, not the actual DOM
487 // element. 490 // element.
488 // We also need to make sure that the previous right click event, 491 // 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 492 // 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. 493 // send a message to the other frames to clear their old right click events.
491 document.addEventListener("contextmenu", event => 494 document.addEventListener("contextmenu", event =>
492 { 495 {
493 lastRightClickEvent = event; 496 lastRightClickEvent = event;
494 lastRightClickEventIsMostRecent = true; 497 lastRightClickEventIsMostRecent = true;
495 498
496 browser.runtime.sendMessage({ 499 browser.runtime.sendMessage({
497 type: "forward", 500 type: "forward",
498 payload: 501 payload:
499 { 502 {
500 type: "composer.content.clearPreviousRightClickEvent" 503 type: "composer.content.clearPreviousRightClickEvent"
501 } 504 }
502 }); 505 });
503 }, true); 506 }, true);
504 507
505 ext.onMessage.addListener((msg, sender, sendResponse) => 508 ext.onMessage.addListener((message, sender, sendResponse) =>
Manish Jethani 2017/10/17 16:43:16 Unrelated renaming.
506 { 509 {
507 switch (msg.type) 510 switch (message.type)
508 { 511 {
509 case "composer.content.getState": 512 case "composer.content.getState":
510 if (window == window.top) 513 if (window == window.top)
511 { 514 {
512 sendResponse({ 515 sendResponse({
513 active: currentlyPickingElement || blockelementPopupId != null 516 active: currentlyPickingElement || blockelementPopupId != null
514 }); 517 });
515 } 518 }
516 break; 519 break;
517 case "composer.content.startPickingElement": 520 case "composer.content.startPickingElement":
518 if (window == window.top) 521 if (window == window.top)
519 startPickingElement(); 522 startPickingElement();
520 break; 523 break;
521 case "composer.content.contextMenuClicked": 524 case "composer.content.contextMenuClicked":
522 let event = lastRightClickEvent; 525 let event = lastRightClickEvent;
523 deactivateBlockElement(); 526 deactivateBlockElement();
524 if (event) 527 if (event)
525 { 528 {
526 getBlockableElementOrAncestor(event.target, element => 529 getBlockableElementOrAncestor(event.target, element =>
527 { 530 {
528 if (element) 531 if (element)
529 { 532 {
530 currentElement = element; 533 currentElement = element;
531 elementPicked(event); 534 elementPicked(event);
532 } 535 }
533 }); 536 });
534 } 537 }
535 break; 538 break;
536 case "composer.content.finished": 539 case "composer.content.finished":
537 if (currentElement && msg.remove) 540 if (currentElement && message.remove)
538 { 541 {
539 // Hide the selected element itself if an added blocking 542 // Hide the selected element itself if an added blocking
540 // filter is causing it to collapse. Note that this 543 // filter is causing it to collapse. Note that this
541 // behavior is incomplete, but the best we can do here, 544 // behavior is incomplete, but the best we can do here,
542 // e.g. if an added blocking filter matches other elements, 545 // e.g. if an added blocking filter matches other elements,
543 // the effect won't be visible until the page is is reloaded. 546 // the effect won't be visible until the page is is reloaded.
544 checkCollapse(currentElement.prisoner || currentElement); 547 checkCollapse(currentElement.prisoner || currentElement);
545 548
546 // Apply added element hiding filters. 549 // Apply added element hiding filters.
547 elemhide.apply(); 550 elemhide.apply();
548 } 551 }
549 deactivateBlockElement(); 552 deactivateBlockElement();
550 break; 553 break;
551 case "composer.content.clearPreviousRightClickEvent": 554 case "composer.content.clearPreviousRightClickEvent":
552 if (!lastRightClickEventIsMostRecent) 555 if (!lastRightClickEventIsMostRecent)
553 lastRightClickEvent = null; 556 lastRightClickEvent = null;
554 lastRightClickEventIsMostRecent = false; 557 lastRightClickEventIsMostRecent = false;
555 break; 558 break;
556 case "composer.content.dialogOpened": 559 case "composer.content.dialogOpened":
557 if (window == window.top) 560 if (window == window.top)
558 blockelementPopupId = msg.popupId; 561 blockelementPopupId = message.popupId;
559 break; 562 break;
560 case "composer.content.dialogClosed": 563 case "composer.content.dialogClosed":
561 // The onRemoved hook for the popup can create a race condition, so we 564 // 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.) 565 // to be careful here. (This is not perfect, but best we can do.)
563 if (window == window.top && blockelementPopupId == msg.popupId) 566 if (window == window.top && blockelementPopupId == message.popupId)
564 { 567 {
565 browser.runtime.sendMessage({ 568 browser.runtime.sendMessage({
566 type: "forward", 569 type: "forward",
567 payload: 570 payload:
568 { 571 {
569 type: "composer.content.finished" 572 type: "composer.content.finished"
570 } 573 }
571 }); 574 });
572 } 575 }
573 break; 576 break;
574 } 577 }
575 }); 578 });
576 579
577 if (window == window.top) 580 if (window == window.top)
578 browser.runtime.sendMessage({type: "composer.ready"}); 581 browser.runtime.sendMessage({type: "composer.ready"});
582
583 return true;
579 } 584 }
585
586 if (document instanceof HTMLDocument)
587 {
588 // There's a bug in Firefox that causes document_end content scripts to run
589 // before document_start content scripts on extension startup. In this case
590 // the ext object is undefined, we fail to initialize, and initializeComposer
591 // returns false. As a workaround, try again after a timeout.
592 // https://bugzilla.mozilla.org/show_bug.cgi?id=1395287
593 if (!initializeComposer())
594 setTimeout(initializeComposer, 2000);
Manish Jethani 2017/10/17 16:43:16 Two seconds is probably fine.
595 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld