diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 10fa323b2..67dec75c5 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -118,6 +118,7 @@ Commands = "LinkHints.activateModeWithQueue", "LinkHints.activateModeToDownloadLink", "LinkHints.activateModeToOpenIncognito", + "LinkHints.activateModeToTriggerRightClickAction", "Vomnibar.activate", "Vomnibar.activateInNewTab", "Vomnibar.activateTabSelection", @@ -163,6 +164,7 @@ Commands = "focusInput", "LinkHints.activateModeWithQueue", "LinkHints.activateModeToDownloadLink", + "LinkHints.activateModeToTriggerRightClickAction", "Vomnibar.activateEditUrl", "Vomnibar.activateEditUrlInNewTab", "LinkHints.activateModeToOpenIncognito", @@ -295,6 +297,7 @@ commandDescriptions = "LinkHints.activateModeWithQueue": ["Open multiple links in a new tab", { noRepeat: true }] "LinkHints.activateModeToOpenIncognito": ["Open a link in incognito window", { noRepeat: true }] "LinkHints.activateModeToDownloadLink": ["Download link url", { noRepeat: true }] + "LinkHints.activateModeToTriggerRightClickAction": ["Right click a link", { noRepeat: true }] enterFindMode: ["Enter find mode", { noRepeat: true }] performFind: ["Cycle forward to the next find match"] diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 24bd7126a..03ceb3bcd 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -15,6 +15,7 @@ OPEN_WITH_QUEUE = {} COPY_LINK_URL = {} OPEN_INCOGNITO = {} DOWNLOAD_LINK_URL = {} +RIGHT_CLICK = {} LinkHints = hintMarkerContainingDiv: null @@ -54,6 +55,7 @@ LinkHints = activateModeWithQueue: -> @activateMode(OPEN_WITH_QUEUE) activateModeToOpenIncognito: -> @activateMode(OPEN_INCOGNITO) activateModeToDownloadLink: -> @activateMode(DOWNLOAD_LINK_URL) + activateModeToTriggerRightClickAction: -> @activateMode(RIGHT_CLICK) activateMode: (mode = OPEN_IN_CURRENT_TAB) -> # we need documentElement to be ready in order to append links @@ -112,9 +114,17 @@ LinkHints = HUD.show("Download link URL") @linkActivator = (link) -> DomUtils.simulateClick(link, { - altKey: true, + altKey: false, ctrlKey: false, metaKey: false }) + else if @mode is RIGHT_CLICK + HUD.show("Right click link") + @linkActivator = (link) -> + DomUtils.simulateClick(link, { + altKey: true, + ctrlKey: false, + metaKey: false }, 2) + DomUtils.simulateContextMenuEvent(link) else # OPEN_IN_CURRENT_TAB HUD.show("Open link in current tab") @linkActivator = (link) -> DomUtils.simulateClick.bind(DomUtils, link)() diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 62e655e70..06c33b60d 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -103,14 +103,19 @@ DomUtils = # When focusing a textbox, put the selection caret at the end of the textbox's contents. element.setSelectionRange(element.value.length, element.value.length) - simulateClick: (element, modifiers) -> + simulateContextMenuEvent: (element) -> + htmlEvent = document.createEvent("HTMLEvents") + htmlEvent.initEvent("contextmenu", true, false) + element.dispatchEvent(htmlEvent) + + simulateClick: (element, modifiers, button = 0) -> modifiers ||= {} eventSequence = ["mouseover", "mousedown", "mouseup", "click"] for event in eventSequence mouseEvent = document.createEvent("MouseEvents") mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, modifiers.altKey, - modifiers.shiftKey, modifiers.metaKey, 0, null) + modifiers.shiftKey, modifiers.metaKey, button, null) # Debugging note: Firefox will not execute the element's default action if we dispatch this click event, # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately element.dispatchEvent(mouseEvent)