diff options
Diffstat (limited to 'content_scripts/link_hints.coffee')
| -rw-r--r-- | content_scripts/link_hints.coffee | 95 |
1 files changed, 51 insertions, 44 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 72fde9e1..6bc37aaf 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -8,16 +8,15 @@ # In 'filter' mode, our link hints are numbers, and the user can narrow down the range of possibilities by # typing the text of the link itself. # -# The "name" property below is a short-form name to appear in the link-hints mode name. Debugging only. The -# key appears in the mode's badge. +# The "name" property below is a short-form name to appear in the link-hints mode's name. It's for debug only. # -OPEN_IN_CURRENT_TAB = { name: "curr-tab", key: "" } -OPEN_IN_NEW_BG_TAB = { name: "bg-tab", key: "B" } -OPEN_IN_NEW_FG_TAB = { name: "fg-tab", key: "F" } -OPEN_WITH_QUEUE = { name: "queue", key: "Q" } -COPY_LINK_URL = { name: "link", key: "C" } -OPEN_INCOGNITO = { name: "incognito", key: "I" } -DOWNLOAD_LINK_URL = { name: "download", key: "D" } +OPEN_IN_CURRENT_TAB = name: "curr-tab" +OPEN_IN_NEW_BG_TAB = name: "bg-tab" +OPEN_IN_NEW_FG_TAB = name: "fg-tab" +OPEN_WITH_QUEUE = name: "queue" +COPY_LINK_URL = name: "link" +OPEN_INCOGNITO = name: "incognito" +DOWNLOAD_LINK_URL = name: "download" LinkHints = hintMarkerContainingDiv: null @@ -33,6 +32,8 @@ LinkHints = if settings.get("filterLinkHints") then filterHints else alphabetHints # lock to ensure only one instance runs at a time isActive: false + # Call this function on exit (if defined). + onExit: null # # To be called after linkHints has been generated from linkHintsBase. @@ -55,61 +56,66 @@ LinkHints = return @isActive = true - @setOpenLinkMode(mode) - hintMarkers = (@createMarkerFor(el) for el in @getVisibleClickableElements()) + elements = @getVisibleClickableElements() + # For these modes, we filter out those elements which don't have an HREF (since there's nothing we can do + # with them). + elements = (el for el in elements when el.element.href?) if mode in [ COPY_LINK_URL, OPEN_INCOGNITO ] + hintMarkers = (@createMarkerFor(el) for el in elements) @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 - # that if you scroll the page and the link has position=fixed, the marker will not stay fixed. - @hintMarkerContainingDiv = DomUtils.addElementList(hintMarkers, - { id: "vimiumHintMarkerContainer", className: "vimiumReset" }) - @hintMode = new Mode name: "hint/#{mode.name}" - badge: "#{mode.key}?" + indicator: false passInitialKeyupEvents: true - keydown: @onKeyDownInMode.bind(this, hintMarkers), - # trap all key events + keydown: @onKeyDownInMode.bind this, hintMarkers + # Trap all other key events. keypress: -> false keyup: -> false + @setOpenLinkMode mode + + # 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 + # that if you scroll the page and the link has position=fixed, the marker will not stay fixed. + @hintMarkerContainingDiv = DomUtils.addElementList(hintMarkers, + { id: "vimiumHintMarkerContainer", className: "vimiumReset" }) + setOpenLinkMode: (@mode) -> 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") + @hintMode.setIndicator "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") + @hintMode.setIndicator "Open link in new tab and switch to it" else - HUD.show("Open multiple links in a new tab") + @hintMode.setIndicator "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", - altKey: false}) + DomUtils.simulateClick link, + shiftKey: @mode is OPEN_IN_NEW_FG_TAB + metaKey: KeyboardUtils.platform == "Mac" + ctrlKey: KeyboardUtils.platform != "Mac" + altKey: false else if @mode is COPY_LINK_URL - HUD.show("Copy link URL to Clipboard") - @linkActivator = (link) -> - chrome.runtime.sendMessage({handler: "copyToClipboard", data: link.href}) + @hintMode.setIndicator "Copy link URL to Clipboard" + @linkActivator = (link) => + if link.href? + chrome.runtime.sendMessage handler: "copyToClipboard", data: link.href + url = link.href + url = url[0..25] + "...." if 28 < url.length + @onExit = -> HUD.showForDuration "Yanked #{url}", 2000 + else + @onExit = -> HUD.showForDuration "No link to yank.", 2000 else if @mode is OPEN_INCOGNITO - HUD.show("Open link in incognito window") - + @hintMode.setIndicator "Open link in incognito window" @linkActivator = (link) -> - chrome.runtime.sendMessage( - handler: 'openUrlInIncognito' - url: link.href) + chrome.runtime.sendMessage handler: 'openUrlInIncognito', url: link.href else if @mode is DOWNLOAD_LINK_URL - HUD.show("Download link URL") + @hintMode.setIndicator "Download link URL" @linkActivator = (link) -> - DomUtils.simulateClick(link, { - altKey: true, - ctrlKey: false, - metaKey: false }) + DomUtils.simulateClick link, altKey: true, ctrlKey: false, metaKey: false else # OPEN_IN_CURRENT_TAB - HUD.show("Open link in current tab") + @hintMode.setIndicator "Open link in current tab" @linkActivator = (link) -> DomUtils.simulateClick.bind(DomUtils, link)() # @@ -276,8 +282,8 @@ LinkHints = handlerStack.push keyup: (event) => if event.keyCode == keyCode - @setOpenLinkMode previousMode if @isActive handlerStack.remove() + @setOpenLinkMode previousMode if @isActive true # TODO(philc): Ignore keys that have modifiers. @@ -347,7 +353,8 @@ LinkHints = DomUtils.removeElement LinkHints.hintMarkerContainingDiv LinkHints.hintMarkerContainingDiv = null @hintMode.exit() - HUD.hide() + @onExit?() + @onExit = null @isActive = false # we invoke the deactivate() function directly instead of using setTimeout(callback, 0) so that |
