From e6a529ea00033d44226581c48df4e84f4cb80237 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 15 Aug 2017 19:53:35 +0100 Subject: FF: Simulate default action for clicking links with link hints --- background_scripts/main.coffee | 1 + content_scripts/link_hints.coffee | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 379239ae..eeda263a 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -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) -> diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index eeadfc0c..f06ef786 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -11,6 +11,7 @@ # The "name" property below is a short-form name to appear in the link-hints mode's name. It's for debug only. # isMac = KeyboardUtils.platform == "Mac" +simulateClickDefaultAction = true OPEN_IN_CURRENT_TAB = name: "curr-tab" indicator: "Open link in current tab" @@ -384,7 +385,16 @@ class LinkHintsMode window.focus() DomUtils.simulateSelect clickEl else - clickActivator = (modifiers) -> (link) -> DomUtils.simulateClick link, modifiers + clickActivator = (modifiers) -> (link) -> + defaultActionsTriggered = DomUtils.simulateClick link, modifiers + if simulateClickDefaultAction and + defaultActionsTriggered[3] and link.tagName?.toLowerCase() == "a" and + modifiers? and modifiers.metaKey == isMac and modifiers.ctrlKey == not isMac + # We've clicked a link that *should* open in a new tab. If simulateClickDefaultAction is true, + # we assume the popup-blocker is active, and simulate opening the new tab ourselves. + chrome.runtime.sendMessage {handler: "openUrlInNewTab", url: link.href, active: + modifiers.shiftKey == true} + linkActivator = @mode.linkActivator ? clickActivator @mode.clickModifiers # TODO: Are there any other input elements which should not receive focus? if clickEl.nodeName.toLowerCase() in ["input", "select"] and clickEl.type not in ["button", "submit"] -- cgit v1.2.3 From a8337e93b33cfbaba17c4a237fe7330f27145c0c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 17 Aug 2017 19:49:07 +0100 Subject: Use Utils.isFirefox for link hints fix --- content_scripts/link_hints.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index f06ef786..0fbb66bf 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -11,7 +11,6 @@ # The "name" property below is a short-form name to appear in the link-hints mode's name. It's for debug only. # isMac = KeyboardUtils.platform == "Mac" -simulateClickDefaultAction = true OPEN_IN_CURRENT_TAB = name: "curr-tab" indicator: "Open link in current tab" @@ -387,6 +386,7 @@ class LinkHintsMode else clickActivator = (modifiers) -> (link) -> defaultActionsTriggered = DomUtils.simulateClick link, modifiers + simulateClickDefaultAction = Utils.isFirefox() if simulateClickDefaultAction and defaultActionsTriggered[3] and link.tagName?.toLowerCase() == "a" and modifiers? and modifiers.metaKey == isMac and modifiers.ctrlKey == not isMac -- cgit v1.2.3 From 7fa22da6954d28dbd578b0cd490ada638d98b373 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 21 Sep 2017 21:46:39 +0100 Subject: Add openUrlInNewWindow command --- background_scripts/main.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index eeda263a..8afecd4f 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -117,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} @@ -416,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) -> -- cgit v1.2.3 From 92d2b4058a378c276d52ecc53409603679c3865c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 21 Sep 2017 21:47:46 +0100 Subject: Move simulating click default action to DomUtils, add shift handling --- content_scripts/link_hints.coffee | 12 +----------- lib/dom_utils.coffee | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 0fbb66bf..eeadfc0c 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -384,17 +384,7 @@ class LinkHintsMode window.focus() DomUtils.simulateSelect clickEl else - clickActivator = (modifiers) -> (link) -> - defaultActionsTriggered = DomUtils.simulateClick link, modifiers - simulateClickDefaultAction = Utils.isFirefox() - if simulateClickDefaultAction and - defaultActionsTriggered[3] and link.tagName?.toLowerCase() == "a" and - modifiers? and modifiers.metaKey == isMac and modifiers.ctrlKey == not isMac - # We've clicked a link that *should* open in a new tab. If simulateClickDefaultAction is true, - # we assume the popup-blocker is active, and simulate opening the new tab ourselves. - chrome.runtime.sendMessage {handler: "openUrlInNewTab", url: link.href, active: - modifiers.shiftKey == true} - + clickActivator = (modifiers) -> (link) -> DomUtils.simulateClick link, modifiers linkActivator = @mode.linkActivator ? clickActivator @mode.clickModifiers # TODO: Are there any other input elements which should not receive focus? if clickEl.nodeName.toLowerCase() in ["input", "select"] and clickEl.type not in ["button", "submit"] diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index b3fc981b..65a14c34 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -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 @@ -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" -- cgit v1.2.3