From 198ef63cbfdcab7f8fef72bf6144964ed5b76cf6 Mon Sep 17 00:00:00 2001 From: Phil Crosby Date: Thu, 31 Dec 2009 02:29:58 -0500 Subject: Fix a race condition where domready would be fired prior to 'initializeWhenEnabled', breaking our initial input-is-focused detection --- vimiumFrontend.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/vimiumFrontend.js b/vimiumFrontend.js index 58113e1a..a1ad7c9c 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -115,24 +115,32 @@ function initializeWhenEnabled() { document.addEventListener("keydown", onKeydown); document.addEventListener("focus", onFocusCapturePhase, true); document.addEventListener("blur", onBlurCapturePhase, true); + enterInsertModeIfElementIsFocused(); } /* * Initialization tasks that must wait for the document to be ready. */ function initializeOnDomReady() { - if (isEnabledForUrl) { - // Enter insert mode automatically if there's already a text box focused. - var focusNode = window.getSelection().focusNode; - var focusOffset = window.getSelection().focusOffset; - if (focusNode && focusOffset && focusNode.children.length > focusOffset && - isInputOrText(focusNode.children[focusOffset])) - enterInsertMode(); - } + if (isEnabledForUrl) + enterInsertModeIfElementIsFocused(); // Tell the background page we're in the dom ready state. chrome.extension.connect({ name: "domReady" }); }; +/* + * Checks the currently focused element of the document and will enter insert mode if that element is focusable. + */ +function enterInsertModeIfElementIsFocused() { + // Enter insert mode automatically if there's already a text box focused. + // TODO(philc): Consider using document.activeElement here instead. + var focusNode = window.getSelection().focusNode; + var focusOffset = window.getSelection().focusOffset; + if (focusNode && focusOffset && focusNode.children.length > focusOffset && + isInputOrText(focusNode.children[focusOffset])) + enterInsertMode(); +} + /* * Asks the background page to persist the zoom level for the given domain to localStorage. */ -- cgit v1.2.3