diff options
Diffstat (limited to 'content_scripts/link_hints.coffee')
-rw-r--r-- | content_scripts/link_hints.coffee | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index f2479d0f..24314b26 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -9,7 +9,8 @@ # typing the text of the link itself. # OPEN_IN_CURRENT_TAB = {} -OPEN_IN_NEW_TAB = {} +OPEN_IN_NEW_BG_TAB = {} +OPEN_IN_NEW_FG_TAB = {} OPEN_WITH_QUEUE = {} COPY_LINK_URL = {} OPEN_INCOGNITO = {} @@ -24,7 +25,8 @@ LinkHints = delayMode: false # Handle the link hinting marker generation and matching. Must be initialized after settings have been # loaded, so that we can retrieve the option setting. - markerMatcher: undefined + getMarkerMatcher: -> + if settings.get("filterLinkHints") then filterHints else alphabetHints # lock to ensure only one instance runs at a time isActive: false @@ -32,7 +34,6 @@ LinkHints = # To be called after linkHints has been generated from linkHintsBase. # init: -> - @markerMatcher = if settings.get("filterLinkHints") then filterHints else alphabetHints # # Generate an XPath describing what a clickable element is. @@ -46,7 +47,8 @@ LinkHints = "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true']"]) # We need this as a top-level function because our command system doesn't yet support arguments. - activateModeToOpenInNewTab: -> @activateMode(OPEN_IN_NEW_TAB) + activateModeToOpenInNewTab: -> @activateMode(OPEN_IN_NEW_BG_TAB) + activateModeToOpenInNewForegroundTab: -> @activateMode(OPEN_IN_NEW_FG_TAB) activateModeToCopyLinkUrl: -> @activateMode(COPY_LINK_URL) activateModeWithQueue: -> @activateMode(OPEN_WITH_QUEUE) activateModeToOpenIncognito: -> @activateMode(OPEN_INCOGNITO) @@ -60,7 +62,8 @@ LinkHints = @isActive = true @setOpenLinkMode(mode) - hintMarkers = @markerMatcher.fillInMarkers(@createMarkerFor(el) for el in @getVisibleClickableElements()) + hintMarkers = (@createMarkerFor(el) for el in @getVisibleClickableElements()) + @getMarkerMatcher().fillInMarkers(hintMarkers) # Note(philc): Append these markers as top level children instead of as child nodes to the link itself, # because some clickable elements cannot contain children, e.g. submit buttons. This has the caveat @@ -77,15 +80,18 @@ LinkHints = }) setOpenLinkMode: (@mode) -> - if @mode is OPEN_IN_NEW_TAB or @mode is OPEN_WITH_QUEUE - if @mode is OPEN_IN_NEW_TAB + if @mode is OPEN_IN_NEW_BG_TAB or @mode is OPEN_IN_NEW_FG_TAB or @mode is OPEN_WITH_QUEUE + if @mode is OPEN_IN_NEW_BG_TAB HUD.show("Open link in new tab") + else if @mode is OPEN_IN_NEW_FG_TAB + HUD.show("Open link in new tab and switch to it") else HUD.show("Open multiple links in a new tab") @linkActivator = (link) -> # When "clicking" on a link, dispatch the event with the appropriate meta key (CMD on Mac, CTRL on # windows) to open it in a new tab if necessary. DomUtils.simulateClick(link, { + shiftKey: @mode is OPEN_IN_NEW_FG_TAB, metaKey: KeyboardUtils.platform == "Mac", ctrlKey: KeyboardUtils.platform != "Mac" }) else if @mode is COPY_LINK_URL @@ -101,9 +107,7 @@ LinkHints = url: link.href) else # OPEN_IN_CURRENT_TAB HUD.show("Open link in current tab") - # When we're opening the link in the current tab, don't navigate to the selected link immediately - # we want to give the user some time to notice which link has received focus. - @linkActivator = (link) -> setTimeout(DomUtils.simulateClick.bind(DomUtils, link), 400) + @linkActivator = (link) -> DomUtils.simulateClick.bind(DomUtils, link)() # # Creates a link marker for the given link. @@ -161,29 +165,29 @@ LinkHints = visibleElements # - # Handles shift and esc keys. The other keys are passed to markerMatcher.matchHintsByKey. + # Handles shift and esc keys. The other keys are passed to getMarkerMatcher().matchHintsByKey. # onKeyDownInMode: (hintMarkers, event) -> return if @delayMode - if (event.keyCode == keyCodes.shiftKey && @mode != COPY_LINK_URL) + if ((event.keyCode == keyCodes.shiftKey or event.keyCode == keyCodes.ctrlKey) and + (@mode == OPEN_IN_CURRENT_TAB or + @mode == OPEN_IN_NEW_BG_TAB or + @mode == OPEN_IN_NEW_FG_TAB)) # Toggle whether to open link in a new or current tab. prev_mode = @mode - @setOpenLinkMode(if @mode is OPEN_IN_CURRENT_TAB then OPEN_IN_NEW_TAB else OPEN_IN_CURRENT_TAB) + if event.keyCode == keyCodes.shiftKey + @setOpenLinkMode(if @mode is OPEN_IN_CURRENT_TAB then OPEN_IN_NEW_BG_TAB else OPEN_IN_CURRENT_TAB) - handlerStack.push({ - keyup: (event) => - return if (event.keyCode != keyCodes.shiftKey) - @setOpenLinkMode(prev_mode) if @isActive - @remove() - }) + else # event.keyCode == keyCodes.ctrlKey + @setOpenLinkMode(if @mode is OPEN_IN_NEW_FG_TAB then OPEN_IN_NEW_BG_TAB else OPEN_IN_NEW_FG_TAB) # TODO(philc): Ignore keys that have modifiers. if (KeyboardUtils.isEscape(event)) @deactivateMode() - else if (event.keyCode != keyCodes.shiftKey) - keyResult = @markerMatcher.matchHintsByKey(hintMarkers, event) + else + keyResult = @getMarkerMatcher().matchHintsByKey(hintMarkers, event) linksMatched = keyResult.linksMatched delay = keyResult.delay ? 0 if (linksMatched.length == 0) @@ -194,7 +198,7 @@ LinkHints = for marker in hintMarkers @hideMarker(marker) for matched in linksMatched - @showMarker(matched, @markerMatcher.hintKeystrokeQueue.length) + @showMarker(matched, @getMarkerMatcher().hintKeystrokeQueue.length) false # We've handled this key, so prevent propagation. # @@ -239,8 +243,8 @@ LinkHints = # deactivateMode: (delay, callback) -> deactivate = => - if (LinkHints.markerMatcher.deactivate) - LinkHints.markerMatcher.deactivate() + if (LinkHints.getMarkerMatcher().deactivate) + LinkHints.getMarkerMatcher().deactivate() if (LinkHints.hintMarkerContainingDiv) DomUtils.removeElement LinkHints.hintMarkerContainingDiv LinkHints.hintMarkerContainingDiv = null |