diff options
Diffstat (limited to 'content_scripts/vimium_frontend.coffee')
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 123 |
1 files changed, 47 insertions, 76 deletions
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 931b8edf..b5a5f51e 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -141,13 +141,13 @@ window.initializeModes = -> constructor: -> super name: "normal" + indicator: false # There is no mode indicator in normal mode. keydown: (event) => onKeydown.call @, event keypress: (event) => onKeypress.call @, event keyup: (event) => onKeyup.call @, event # Install the permanent modes. The permanently-installed insert mode tracks focus/blur events, and # activates/deactivates itself accordingly. - new BadgeMode new NormalMode new PassKeysMode new InsertMode permanent: true @@ -174,10 +174,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 @@ -185,8 +184,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 } @@ -197,7 +194,7 @@ initializePreDomReady = -> # from the former. return if sender.tab and not sender.tab.url.startsWith 'chrome-extension://' # We handle the message if we're enabled, or if it's one of these listed message types. - return unless isEnabledForUrl or request.name in [ "getActiveState", "setState", "executePageCommand" ] + return unless isEnabledForUrl or request.name in [ "executePageCommand" ] # These requests are delivered to the options page, but there are no handlers there. return if request.handler in [ "registerFrame", "unregisterFrame" ] # We don't handle these here. They're handled elsewhere (e.g. in the vomnibar/UI component). @@ -226,34 +223,25 @@ window.initializeWhenEnabled = -> for type in [ "keydown", "keypress", "keyup", "click", "focus", "blur", "mousedown" ] do (type) -> installListener window, type, (event) -> handlerStack.bubbleEvent type, event installListener document, "DOMActivate", (event) -> handlerStack.bubbleEvent 'DOMActivate', event - installListener window, "focus", registerFocus 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. # -registerFocus = (event) -> +onFocus = (event) -> if event.target == window - # Settings may have changed since the frame last had focus. settings.load() + chrome.runtime.sendMessage handler: "frameFocused", frameId: frameId, url: window.location.toString() checkIfEnabledForUrl() - # Don't register frameset containers; focusing them is no use. - unless document.body?.tagName.toLowerCase() == "frameset" - chrome.runtime.sendMessage handler: "frameFocused", frameId: frameId + +# 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. @@ -434,7 +422,6 @@ extend window, constructor: -> super name: "focus-selector" - badge: "?" exitOnClick: true keydown: (event) => if event.keyCode == KeyboardUtils.keyCodes.tab @@ -600,12 +587,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) -> @@ -727,7 +723,6 @@ handleKeyCharForFindMode = (keyChar) -> updateQueryForFindMode findModeQuery.rawQuery + keyChar handleEscapeForFindMode = -> - exitFindMode() document.body.classList.remove("vimiumFindMode") # removing the class does not re-color existing selections. we recreate the current selection so it reverts # back to the default color. @@ -741,8 +736,7 @@ handleEscapeForFindMode = -> # Return true if character deleted, false otherwise. handleDeleteForFindMode = -> if findModeQuery.rawQuery.length == 0 - exitFindMode() - performFindInPlace() + HUD.hide() false else updateQueryForFindMode findModeQuery.rawQuery.substring(0, findModeQuery.rawQuery.length - 1) @@ -752,22 +746,25 @@ handleDeleteForFindMode = -> # <esc> corresponds approximately to 'nevermind, I have found it already' while <cr> means 'I want to save # this query and do more searches with it' handleEnterForFindMode = -> - exitFindMode() focusFoundLink() document.body.classList.add("vimiumFindMode") FindModeHistory.saveQuery findModeQuery.rawQuery class FindMode extends Mode - constructor: -> + constructor: (options = {}) -> @historyIndex = -1 @partialQuery = "" + if options.returnToViewport + @scrollX = window.scrollX + @scrollY = window.scrollY super name: "find" - badge: "/" + indicator: false exitOnEscape: true exitOnClick: true keydown: (event) => + window.scrollTo @scrollX, @scrollY if options.returnToViewport if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey @exit() unless handleDeleteForFindMode() @suppressEvent @@ -790,8 +787,8 @@ class FindMode extends Mode DomUtils.suppressPropagation(event) handlerStack.stopBubblingAndFalse - keypress: (event) -> - handlerStack.neverContinueBubbling -> + keypress: (event) => + handlerStack.neverContinueBubbling => if event.keyCode > 31 keyChar = String.fromCharCode event.charCode handleKeyCharForFindMode keyChar if keyChar @@ -1024,15 +1021,13 @@ findModeRestoreSelection = (range = findModeInitialRange) -> selection.addRange range # Enters find mode. Returns the new find-mode instance. -window.enterFindMode = -> +window.enterFindMode = (options = {}) -> # Save the selection, so performFindInPlace can restore it. findModeSaveSelection() - findModeQuery = { rawQuery: "" } - HUD.show("/") - new FindMode() - -exitFindMode = -> - HUD.hide() + findModeQuery = rawQuery: "" + findMode = new FindMode options + HUD.show "/" + findMode window.showHelpDialog = (html, fid) -> return if (isShowingHelpDialog || !document.body || fid != frameId) @@ -1102,7 +1097,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 @@ -1120,26 +1114,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. # @@ -1150,26 +1124,23 @@ 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" document.body.appendChild(element) element - hide: (immediate) -> + # Hide the HUD. + # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately). + # If :updateIndicator is truthy, then we also refresh the mode indicator. The only time we don't update the + # mode indicator, is when hide() is called for the mode indicator itself. + hide: (immediate = false, updateIndicator = true) -> clearInterval(HUD._tweenId) - if (immediate) - HUD.displayElement().style.display = "none" + if immediate + HUD.displayElement().style.display = "none" unless updateIndicator + Mode.setIndicator() if updateIndicator else - HUD._tweenId = Tween.fade(HUD.displayElement(), 0, 150, - -> HUD.displayElement().style.display = "none") + HUD._tweenId = Tween.fade HUD.displayElement(), 0, 150, -> HUD.hide true, updateIndicator isReady: -> document.body != null |
