Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Needs rebase] Add a command to trigger javascript right click menus #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions background_scripts/commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Commands =
"LinkHints.activateModeWithQueue",
"LinkHints.activateModeToDownloadLink",
"LinkHints.activateModeToOpenIncognito",
"LinkHints.activateModeToTriggerRightClickAction",
"Vomnibar.activate",
"Vomnibar.activateInNewTab",
"Vomnibar.activateTabSelection",
Expand Down Expand Up @@ -163,6 +164,7 @@ Commands =
"focusInput",
"LinkHints.activateModeWithQueue",
"LinkHints.activateModeToDownloadLink",
"LinkHints.activateModeToTriggerRightClickAction",
"Vomnibar.activateEditUrl",
"Vomnibar.activateEditUrlInNewTab",
"LinkHints.activateModeToOpenIncognito",
Expand Down Expand Up @@ -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"]
Expand Down
12 changes: 11 additions & 1 deletion content_scripts/link_hints.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ OPEN_WITH_QUEUE = {}
COPY_LINK_URL = {}
OPEN_INCOGNITO = {}
DOWNLOAD_LINK_URL = {}
RIGHT_CLICK = {}

LinkHints =
hintMarkerContainingDiv: null
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)()
Expand Down
9 changes: 7 additions & 2 deletions lib/dom_utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down