diff options
Diffstat (limited to 'content_scripts/vimium_frontend.coffee')
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 88 | 
1 files changed, 30 insertions, 58 deletions
| diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index f549e06d..409a9373 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -26,7 +26,7 @@ validFirstKeys = ""  # The corresponding XPath for such elements.  textInputXPath = (-> -  textInputTypes = ["text", "search", "email", "url", "number", "password"] +  textInputTypes = [ "text", "search", "email", "url", "number", "password", "date", "tel" ]    inputElements = ["input[" +      "(" + textInputTypes.map((type) -> '@type="' + type + '"').join(" or ") + "or not(@type))" +      " and not(@disabled or @readonly)]", @@ -133,8 +133,6 @@ window.initializeModes = ->          keypress: (event) => onKeypress.call @, event          keyup: (event) => onKeyup.call @, event -      Scroller.init settings -    # Install the permanent modes.  The permanently-installed insert mode tracks focus/blur events, and    # activates/deactivates itself accordingly.    new BadgeMode @@ -142,6 +140,7 @@ window.initializeModes = ->    new PassKeysMode    new InsertMode permanent: true    new GrabBackFocus +  Scroller.init settings  #  # Complete initialization work that sould be done prior to DOMReady. @@ -163,10 +162,9 @@ initializePreDomReady = ->      isEnabledForUrl = false      chrome.runtime.sendMessage = ->      chrome.runtime.connect = -> +    window.removeEventListener "focus", onFocus    requestHandlers = -    hideUpgradeNotification: -> HUD.hideUpgradeNotification() -    showUpgradeNotification: (request) -> HUD.showUpgradeNotification(request.version)      showHUDforDuration: (request) -> HUD.showForDuration request.text, request.duration      toggleHelpDialog: (request) -> toggleHelpDialog(request.dialogHtml, request.frameId)      focusFrame: (request) -> if (frameId == request.frameId) then focusThisFrame(request.highlight) @@ -174,8 +172,6 @@ initializePreDomReady = ->      getScrollPosition: -> scrollX: window.scrollX, scrollY: window.scrollY      setScrollPosition: (request) -> setScrollPosition request.scrollX, request.scrollY      executePageCommand: executePageCommand -    getActiveState: getActiveState -    setState: setState      currentKeyQueue: (request) ->        keyQueue = request.keyQueue        handlerStack.bubbleEvent "registerKeyQueue", { keyQueue: keyQueue } @@ -184,9 +180,9 @@ initializePreDomReady = ->      # In the options page, we will receive requests from both content and background scripts. ignore those      # from the former.      return if sender.tab and not sender.tab.url.startsWith 'chrome-extension://' -    return unless isEnabledForUrl or request.name == 'getActiveState' or request.name == 'setState' +    return unless isEnabledForUrl      # These requests are delivered to the options page, but there are no handlers there. -    return if request.handler == "registerFrame" or request.handler == "frameFocused" +    return if request.handler in [ "registerFrame", "frameFocused", "unregisterFrame" ]      sendResponse requestHandlers[request.name](request, sender)      # Ensure the sendResponse callback is freed.      false @@ -213,26 +209,22 @@ window.initializeWhenEnabled = ->      installedListeners = true      FindModeHistory.init() -setState = (request) -> -  isEnabledForUrl = request.enabled -  passKeys = request.passKeys -  isIncognitoMode = request.incognito -  initializeWhenEnabled() if isEnabledForUrl -  handlerStack.bubbleEvent "registerStateChange", -    enabled: isEnabledForUrl -    passKeys: passKeys - -getActiveState = -> -  Mode.updateBadge() -  return { enabled: isEnabledForUrl, passKeys: passKeys } -  # -# The backend needs to know which frame has focus. +# Whenever we get the focus: +# - Reload settings (they may have changed). +# - Tell the background page this frame's URL. +# - Check if we should be enabled.  # -window.addEventListener "focus", -> -  # settings may have changed since the frame last had focus -  settings.load() -  chrome.runtime.sendMessage({ handler: "frameFocused", frameId: frameId }) +onFocus = (event) -> +  if event.target == window +    settings.load() +    chrome.runtime.sendMessage handler: "frameFocused", frameId: frameId, url: window.location.toString() +    checkIfEnabledForUrl() + +# We install these listeners directly (that is, we don't use installListener) because we still need to receive +# events when Vimium is not enabled. +window.addEventListener "focus", onFocus +window.addEventListener "hashchange", onFocus  #  # Initialization tasks that must wait for the document to be ready. @@ -565,12 +557,21 @@ checkIfEnabledForUrl = ->      isIncognitoMode = response.incognito      if isEnabledForUrl        initializeWhenEnabled() -    else if (HUD.isReady()) +    else if HUD.isReady()        # Quickly hide any HUD we might already be showing, e.g. if we entered insert mode on page load.        HUD.hide()      handlerStack.bubbleEvent "registerStateChange",        enabled: isEnabledForUrl        passKeys: passKeys +    # Update the page icon, if necessary. +    if document.hasFocus() +      chrome.runtime.sendMessage +        handler: "setIcon" +        icon: +          if isEnabledForUrl and not passKeys then "enabled" +          else if isEnabledForUrl then "partial" +          else "disabled" +  # Exported to window, but only for DOM tests.  window.refreshCompletionKeys = (response) -> @@ -801,8 +802,7 @@ executeFind = (query, options) ->    # previous find landed in an editable element, then that element may still be activated.  In this case, we    # don't want to leave it behind (see #1412).    if document.activeElement and DomUtils.isEditable document.activeElement -    if not DomUtils.isSelected document.activeElement -      document.activeElement.blur() +    document.activeElement.blur() unless DomUtils.isSelected document.activeElement    # we need to save the anchor node here because <esc> seems to nullify it, regardless of whether we do    # preventDefault() @@ -1072,7 +1072,6 @@ toggleHelpDialog = (html, fid) ->  HUD =    _tweenId: -1    _displayElement: null -  _upgradeNotificationElement: null    # This HUD is styled to precisely mimick the chrome HUD on Mac. Use the "has_popup_and_link_hud.html"    # test harness to tweak these styles to match Chrome's. One limitation of our HUD display is that @@ -1090,26 +1089,6 @@ HUD =      HUD._tweenId = Tween.fade(HUD.displayElement(), 1.0, 150)      HUD.displayElement().style.display = "" -  showUpgradeNotification: (version) -> -    HUD.upgradeNotificationElement().innerHTML = "Vimium has been upgraded to #{version}. See -      <a class='vimiumReset' target='_blank' -      href='https://github.com/philc/vimium#release-notes'> -      what's new</a>.<a class='vimiumReset close-button' href='#'>×</a>" -    links = HUD.upgradeNotificationElement().getElementsByTagName("a") -    links[0].addEventListener("click", HUD.onUpdateLinkClicked, false) -    links[1].addEventListener "click", (event) -> -      event.preventDefault() -      HUD.onUpdateLinkClicked() -    Tween.fade(HUD.upgradeNotificationElement(), 1.0, 150) - -  onUpdateLinkClicked: (event) -> -    HUD.hideUpgradeNotification() -    chrome.runtime.sendMessage({ handler: "upgradeNotificationClosed" }) - -  hideUpgradeNotification: (clickEvent) -> -    Tween.fade(HUD.upgradeNotificationElement(), 0, 150, -      -> HUD.upgradeNotificationElement().style.display = "none") -    #    # Retrieves the HUD HTML element.    # @@ -1120,13 +1099,6 @@ HUD =        HUD._displayElement.style.right = "150px"      HUD._displayElement -  upgradeNotificationElement: -> -    if (!HUD._upgradeNotificationElement) -      HUD._upgradeNotificationElement = HUD.createHudElement() -      # Position this just to the left of our normal HUD. -      HUD._upgradeNotificationElement.style.right = "315px" -    HUD._upgradeNotificationElement -    createHudElement: ->      element = document.createElement("div")      element.className = "vimiumReset vimiumHUD" | 
