From ffc49d3057daee2354fb77d939fffc0cf77ff2e1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 12 Feb 2015 10:52:56 +0000 Subject: Grab back focus... - add new option "GrabBackFocus" - use chrome.storage.sync.get() to get option value - avoid race conditions on load - fix tests --- background_scripts/settings.coffee | 1 + content_scripts/vimium_frontend.coffee | 33 ++++++++++++++++----------------- pages/options.coffee | 1 + pages/options.html | 14 ++++++++++++++ tests/dom_tests/chrome.coffee | 21 +++++++++------------ tests/dom_tests/dom_tests.coffee | 22 +++++++++++++--------- 6 files changed, 54 insertions(+), 38 deletions(-) diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index f43bd4bc..4342aa26 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -73,6 +73,7 @@ root.Settings = Settings = linkHintNumbers: "0123456789" filterLinkHints: false hideHud: false + grabBackfocus: false userDefinedLinkHintCss: """ div > .vimiumHintMarker { diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index ddc19d3a..3cbf2d53 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -98,28 +98,27 @@ frameId = Math.floor(Math.random()*999999999) # If an input grabs the focus before the user has interacted with the page, then grab it back. class GrabBackFocus extends Mode - constructor: (@insertMode) -> - return if @shouldBeDeactivated() - super name: "grab-focus", keydown: => @alwaysContinueBubbling => @exit() + constructor: -> + super + name: "grab-back-focus" + keydown: => @alwaysContinueBubbling => @exit() @push - _name: "grab-focus-handlers" + _name: "grab-back-focus-mousedown" mousedown: => @alwaysContinueBubbling => @exit() - focus: (event) => @grabBackFocus event.target - # An input may already be focused. If so, grab back the focus. - @grabBackFocus document.activeElement if document.activeElement + chrome.storage.sync.get "grabBackfocus", (items) => + return @exit() unless items.grabBackfocus and not chrome.runtime.lastError + @push + _name: "grab-back-focus-focus" + focus: (event) => @grabBackFocus event.target + # An input may already be focused. If so, grab back the focus. + @grabBackFocus document.activeElement if document.activeElement grabBackFocus: (element) -> - if DomUtils.isEditable(element) and not @shouldBeDeactivated() - element.blur() - @insertMode.exit null, element - return @suppressEvent - @exit() if @shouldBeDeactivated() - @continueBubbling - - shouldBeDeactivated: -> - false and settings.isLoaded and not settings.get "grabBackFocus" + return @continueBubbling unless DomUtils.isEditable element + element.blur() + @suppressEvent # Only exported for tests. window.initializeModes = -> @@ -139,7 +138,7 @@ window.initializeModes = -> new NormalMode new PassKeysMode new InsertMode permanent: true - new GrabBackFocus InsertMode.permanentInstance + new GrabBackFocus # # Complete initialization work that sould be done prior to DOMReady. diff --git a/pages/options.coffee b/pages/options.coffee index 93c9b503..525508fd 100644 --- a/pages/options.coffee +++ b/pages/options.coffee @@ -257,6 +257,7 @@ initOptionsPage = -> regexFindMode: CheckBoxOption scrollStepSize: NumberOption smoothScroll: CheckBoxOption + grabBackfocus: CheckBoxOption searchEngines: TextOption searchUrl: NonEmptyTextOption userDefinedLinkHintCss: TextOption diff --git a/pages/options.html b/pages/options.html index d37646c4..6df2c92b 100644 --- a/pages/options.html +++ b/pages/options.html @@ -128,6 +128,20 @@ b: http://b.com/?q=%s description +