Skip to content

Firefox: Simulate default action for clicking links with link hints #2602

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

Merged
merged 4 commits into from
Sep 25, 2017
Merged
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
12 changes: 12 additions & 0 deletions background_scripts/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ TabOperations =
index: request.tab.index + 1
active: true
windowId: request.tab.windowId
tabConfig.active = request.active if request.active?
# Firefox does not support "about:newtab" in chrome.tabs.create.
delete tabConfig["url"] if tabConfig["url"] == Settings.defaults.newTabUrl
chrome.tabs.create tabConfig, (tab) ->
Expand All @@ -116,6 +117,16 @@ TabOperations =
catch
callback.apply this, arguments

# Opens request.url in new window and switches to it.
openUrlInNewWindow: (request, callback = (->)) ->
winConfig =
url: Utils.convertToUrl request.url
active: true
winConfig.active = request.active if request.active?
# Firefox does not support "about:newtab" in chrome.tabs.create.
delete tabConfig["url"] if tabConfig["url"] == Settings.defaults.newTabUrl
chrome.windows.create winConfig, callback

toggleMuteTab = do ->
muteTab = (tab) -> chrome.tabs.update tab.id, {muted: !tab.mutedInfo.muted}

Expand Down Expand Up @@ -415,6 +426,7 @@ sendRequestHandlers =
# with Chrome-specific URLs like "view-source:http:..".
getCurrentTabUrl: ({tab}) -> tab.url
openUrlInNewTab: (request) -> TabOperations.openUrlInNewTab request
openUrlInNewWindow: (request) -> TabOperations.openUrlInNewWindow request
openUrlInIncognito: (request) -> chrome.windows.create incognito: true, url: Utils.convertToUrl request.url
openUrlInCurrentTab: TabOperations.openUrlInCurrentTab
openOptionsPageInNewTab: (request) ->
Expand Down
28 changes: 27 additions & 1 deletion lib/dom_utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ DomUtils =
simulateClick: (element, modifiers) ->
eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
for event in eventSequence
@simulateMouseEvent event, element, modifiers
defaultActionShouldTrigger = @simulateMouseEvent event, element, modifiers
if event == "click" and defaultActionShouldTrigger and Utils.isFirefox()
# Firefox doesn't (currently) trigger the default action for modified keys.
DomUtils.simulateClickDefaultAction element, modifiers
defaultActionShouldTrigger # return the values returned by each @simulateMouseEvent call.

simulateMouseEvent: do ->
lastHoveredElement = undefined
Expand All @@ -272,6 +276,28 @@ DomUtils =
# but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
element.dispatchEvent(mouseEvent)

simulateClickDefaultAction: (element, modifiers) ->
return unless modifiers?
return unless element.tagName?.toLowerCase() == "a" and element.href?

{ctrlKey, shiftKey, metaKey, altKey} = modifiers

# Mac uses a different new tab modifier (meta vs. ctrl).
if KeyboardUtils.platform == "Mac"
newTabModifier = metaKey == true and ctrlKey == false
else
newTabModifier = metaKey == false and ctrlKey == true

if newTabModifier
# Open in new tab. Shift determines whether the tab is focused when created. Alt is ignored.
chrome.runtime.sendMessage {handler: "openUrlInNewTab", url: element.href, active:
shiftKey == true}
else if shiftKey == true and metaKey == false and ctrlKey == false and altKey == false
# Open in new window.
chrome.runtime.sendMessage {handler: "openUrlInNewWindow", url: element.href}

return

addFlashRect: (rect) ->
flashEl = @createElement "div"
flashEl.classList.add "vimiumReset"
Expand Down