Index: lib/composer.js |
=================================================================== |
rename from composer.postload.js |
rename to lib/composer.js |
--- a/composer.postload.js |
+++ b/lib/composer.js |
@@ -474,106 +474,112 @@ |
let overlays = document.getElementsByClassName("__adblockplus__overlay"); |
while (overlays.length > 0) |
overlays[0].parentNode.removeChild(overlays[0]); |
ext.onExtensionUnloaded.removeListener(deactivateBlockElement); |
} |
-if (document instanceof HTMLDocument) |
+function setupComposer() |
{ |
- // Use a contextmenu handler to save the last element the user right-clicked |
- // on. To make things easier, we actually save the DOM event. We have to do |
- // this because the contextMenu API only provides a URL, not the actual DOM |
- // element. |
- // We also need to make sure that the previous right click event, |
- // if there is one, is removed. We don't know which frame it is in so we must |
- // send a message to the other frames to clear their old right click events. |
- document.addEventListener("contextmenu", event => |
+ if (document instanceof HTMLDocument) |
{ |
- lastRightClickEvent = event; |
- lastRightClickEventIsMostRecent = true; |
+ // Use a contextmenu handler to save the last element the user right-clicked |
+ // on. To make things easier, we actually save the DOM event. We have to do |
+ // this because the contextMenu API only provides a URL, not the actual DOM |
+ // element. |
+ // We also need to make sure that the previous right click event, |
+ // if there is one, is removed. We don't know which frame it is in so we |
+ // must send a message to the other frames to clear their old right click |
+ // events. |
+ document.addEventListener("contextmenu", event => |
+ { |
+ lastRightClickEvent = event; |
+ lastRightClickEventIsMostRecent = true; |
+ |
+ ext.backgroundPage.sendMessage({ |
+ type: "forward", |
+ payload: |
+ { |
+ type: "composer.content.clearPreviousRightClickEvent" |
+ } |
+ }); |
+ }, true); |
- ext.backgroundPage.sendMessage({ |
- type: "forward", |
- payload: |
+ ext.onMessage.addListener((msg, sender, sendResponse) => |
+ { |
+ switch (msg.type) |
{ |
- type: "composer.content.clearPreviousRightClickEvent" |
+ case "composer.content.getState": |
+ if (window == window.top) |
+ { |
+ sendResponse({ |
+ active: currentlyPickingElement || blockelementPopupId != null |
+ }); |
+ } |
+ break; |
+ case "composer.content.startPickingElement": |
+ if (window == window.top) |
+ startPickingElement(); |
+ break; |
+ case "composer.content.contextMenuClicked": |
+ let event = lastRightClickEvent; |
+ deactivateBlockElement(); |
+ if (event) |
+ { |
+ getBlockableElementOrAncestor(event.target, element => |
+ { |
+ if (element) |
+ { |
+ currentElement = element; |
+ elementPicked(event); |
+ } |
+ }); |
+ } |
+ break; |
+ case "composer.content.finished": |
+ if (currentElement && msg.remove) |
+ { |
+ // Hide the selected element itself if an added blocking |
+ // filter is causing it to collapse. Note that this |
+ // behavior is incomplete, but the best we can do here, |
+ // e.g. if an added blocking filter matches other elements, |
+ // the effect won't be visible until the page is is reloaded. |
+ checkCollapse(currentElement.prisoner || currentElement); |
+ |
+ // Apply added element hiding filters. |
+ elemhide.apply(); |
+ } |
+ deactivateBlockElement(); |
+ break; |
+ case "composer.content.clearPreviousRightClickEvent": |
+ if (!lastRightClickEventIsMostRecent) |
+ lastRightClickEvent = null; |
+ lastRightClickEventIsMostRecent = false; |
+ break; |
+ case "composer.content.dialogOpened": |
+ if (window == window.top) |
+ blockelementPopupId = msg.popupId; |
+ break; |
+ case "composer.content.dialogClosed": |
+ // The onRemoved hook for the popup can create a race condition, so we |
+ // to be careful here. (This is not perfect, but best we can do.) |
+ if (window == window.top && blockelementPopupId == msg.popupId) |
+ { |
+ ext.backgroundPage.sendMessage({ |
+ type: "forward", |
+ payload: |
+ { |
+ type: "composer.content.finished" |
+ } |
+ }); |
+ } |
+ break; |
} |
}); |
- }, true); |
- ext.onMessage.addListener((msg, sender, sendResponse) => |
- { |
- switch (msg.type) |
- { |
- case "composer.content.getState": |
- if (window == window.top) |
- { |
- sendResponse({ |
- active: currentlyPickingElement || blockelementPopupId != null |
- }); |
- } |
- break; |
- case "composer.content.startPickingElement": |
- if (window == window.top) |
- startPickingElement(); |
- break; |
- case "composer.content.contextMenuClicked": |
- let event = lastRightClickEvent; |
- deactivateBlockElement(); |
- if (event) |
- { |
- getBlockableElementOrAncestor(event.target, element => |
- { |
- if (element) |
- { |
- currentElement = element; |
- elementPicked(event); |
- } |
- }); |
- } |
- break; |
- case "composer.content.finished": |
- if (currentElement && msg.remove) |
- { |
- // Hide the selected element itself if an added blocking |
- // filter is causing it to collapse. Note that this |
- // behavior is incomplete, but the best we can do here, |
- // e.g. if an added blocking filter matches other elements, |
- // the effect won't be visible until the page is is reloaded. |
- checkCollapse(currentElement.prisoner || currentElement); |
+ if (window == window.top) |
+ ext.backgroundPage.sendMessage({type: "composer.ready"}); |
+ } |
+} |
- // Apply added element hiding filters. |
- elemhide.apply(); |
- } |
- deactivateBlockElement(); |
- break; |
- case "composer.content.clearPreviousRightClickEvent": |
- if (!lastRightClickEventIsMostRecent) |
- lastRightClickEvent = null; |
- lastRightClickEventIsMostRecent = false; |
- break; |
- case "composer.content.dialogOpened": |
- if (window == window.top) |
- blockelementPopupId = msg.popupId; |
- break; |
- case "composer.content.dialogClosed": |
- // The onRemoved hook for the popup can create a race condition, so we |
- // to be careful here. (This is not perfect, but best we can do.) |
- if (window == window.top && blockelementPopupId == msg.popupId) |
- { |
- ext.backgroundPage.sendMessage({ |
- type: "forward", |
- payload: |
- { |
- type: "composer.content.finished" |
- } |
- }); |
- } |
- break; |
- } |
- }); |
- |
- if (window == window.top) |
- ext.backgroundPage.sendMessage({type: "composer.ready"}); |
-} |
+exports.setupComposer = setupComposer; |