From 9ce07ada32883ac75a1d7436c026a846628181c8 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 12 Jan 2015 11:47:56 +0000 Subject: Fix race condition on the options page. Fixes #1426. --- content_scripts/vimium_frontend.coffee | 2 +- lib/dom_utils.coffee | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 351a2690..0f42c65b 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -1142,7 +1142,7 @@ CursorHider = initializePreDomReady() window.addEventListener("DOMContentLoaded", registerFrame) window.addEventListener("unload", unregisterFrame) -window.addEventListener("DOMContentLoaded", initializeOnDomReady) +DomUtils.runWhenDOMLoaded initializeOnDomReady window.onbeforeunload = -> chrome.runtime.sendMessage( diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index ba5e279f..762c6418 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -178,5 +178,12 @@ DomUtils = event.preventDefault() @suppressPropagation(event) + # Calls func either now (if the DOM has already loaded), or when the DOM is loaded. + runWhenDOMLoaded: (func) -> + if document.readyState == "loading" + window.addEventListener "DOMContentLoaded", func + else + func() + root = exports ? window root.DomUtils = DomUtils -- cgit v1.2.3 From 4504b78613b4a68b838dd887c8b23ec5a71fe779 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 12 Jan 2015 11:55:20 +0000 Subject: Fix race condition on the options page (better). Uses document.readyState for all onDOMContentLoaded dependent functions. This should fix the same race condition as #1426 for all cases. --- content_scripts/vimium_frontend.coffee | 2 +- lib/dom_utils.coffee | 16 +++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 0f42c65b..281cc978 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -1142,7 +1142,7 @@ CursorHider = initializePreDomReady() window.addEventListener("DOMContentLoaded", registerFrame) window.addEventListener("unload", unregisterFrame) -DomUtils.runWhenDOMLoaded initializeOnDomReady +DomUtils.documentReady initializeOnDomReady window.onbeforeunload = -> chrome.runtime.sendMessage( diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 762c6418..7a75dd6a 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -2,10 +2,11 @@ DomUtils = # # Runs :callback if the DOM has loaded, otherwise runs it on load # - documentReady: do -> - loaded = false - window.addEventListener("DOMContentLoaded", -> loaded = true) - (callback) -> if loaded then callback() else window.addEventListener("DOMContentLoaded", callback) + documentReady: (func) -> + if document.readyState == "loading" + window.addEventListener "DOMContentLoaded", func + else + func() # # Adds a list of elements to a page. @@ -178,12 +179,5 @@ DomUtils = event.preventDefault() @suppressPropagation(event) - # Calls func either now (if the DOM has already loaded), or when the DOM is loaded. - runWhenDOMLoaded: (func) -> - if document.readyState == "loading" - window.addEventListener "DOMContentLoaded", func - else - func() - root = exports ? window root.DomUtils = DomUtils -- cgit v1.2.3 From 2e8acc1fcd8661fc3f14d86cbf4329b4fc7f843d Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 12 Jan 2015 11:59:30 +0000 Subject: Fix race condition on the options page (better still). a --- content_scripts/vimium_frontend.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 281cc978..a3ab051b 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -1140,9 +1140,9 @@ CursorHider = window.addEventListener "scroll", @onScroll initializePreDomReady() -window.addEventListener("DOMContentLoaded", registerFrame) -window.addEventListener("unload", unregisterFrame) DomUtils.documentReady initializeOnDomReady +DomUtils.documentReady registerFrame +window.addEventListener "unload", unregisterFrame window.onbeforeunload = -> chrome.runtime.sendMessage( -- cgit v1.2.3