diff options
| author | Stephen Blott | 2016-04-14 10:35:23 +0100 |
|---|---|---|
| committer | Stephen Blott | 2016-04-14 10:35:23 +0100 |
| commit | f1cb5a70fac210eaaa33609c1a4553b04f00b910 (patch) | |
| tree | d71f83349ddb190639f5a2af7accfa2fca35f9d1 /pages | |
| parent | f9c00d9d56373566c034685d0e4bb99ff3b0367b (diff) | |
| parent | 39115e921af964a73d49da3085319fc1160009d2 (diff) | |
| download | vimium-f1cb5a70fac210eaaa33609c1a4553b04f00b910.tar.bz2 | |
Merge pull request #2097 from smblott-github/ui-component-init-sequence
Rework UI component init sequence.
Diffstat (limited to 'pages')
| -rw-r--r-- | pages/ui_component_server.coffee | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/pages/ui_component_server.coffee b/pages/ui_component_server.coffee index 8b43095b..4210a60e 100644 --- a/pages/ui_component_server.coffee +++ b/pages/ui_component_server.coffee @@ -1,6 +1,6 @@ -# Fetch the Vimium secret, register the port recieved from the parent window, and stop listening for messages -# on the window object. vimiumSecret is accessible only within the current instantion of Vimium. So a +# Fetch the Vimium secret, register the port received from the parent window, and stop listening for messages +# on the window object. vimiumSecret is accessible only within the current instance of Vimium. So a # malicious host page trying to register its own port can do no better than guessing. registerPort = (event) -> chrome.storage.local.get "vimiumSecret", ({vimiumSecret: secret}) -> @@ -15,13 +15,26 @@ UIComponentServer = handleMessage: null portOpen: (@ownerPagePort) -> - @ownerPagePort.onmessage = (event) => - @handleMessage event if @handleMessage + @ownerPagePort.onmessage = (event) => @handleMessage? event + @registerIsReady() registerHandler: (@handleMessage) -> postMessage: (message) -> - @ownerPagePort.postMessage message if @ownerPagePort + @ownerPagePort?.postMessage message + + # We require both that the DOM is ready and that the port has been opened before the UI component is ready. + # These events can happen in either order. We count them, and notify the content script when we've seen + # both. + registerIsReady: do -> + uiComponentIsReadyCount = + if document.readyState == "loading" + window.addEventListener "DOMContentLoaded", -> UIComponentServer.registerIsReady() + 0 + else + 1 + + -> @postMessage "uiComponentIsReady" if ++uiComponentIsReadyCount == 2 root = exports ? window root.UIComponentServer = UIComponentServer |
