1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const Services = require("Services");
11 * A simple way to be notified (once) when a window becomes
12 * interactive (DOMContentLoaded).
14 * It is based on the chromeEventHandler. This is useful when
15 * chrome iframes are loaded in content docshells (in Firefox
18 * @param nsIDOMWindow win
19 * The content window, owning the document to traverse.
20 * @param Function callback
21 * The method to call when the frame is loaded.
22 * @param String targetURL
23 * (optional) Check that the frame URL corresponds to the provided URL
24 * before calling the callback.
26 onceDOMReady: function(win, callback, targetURL) {
28 throw new Error("window can't be null or undefined");
30 const docShell = win.docShell;
31 const onReady = function(event) {
32 if (event.target == win.document) {
33 docShell.chromeEventHandler.removeEventListener(
37 // If in `callback` the URL of the window is changed and a listener to DOMContentLoaded
38 // is attached, the event we just received will be also be caught by the new listener.
39 // We want to avoid that so we execute the callback in the next queue.
40 Services.tm.dispatchToMainThread(callback);
44 (win.document.readyState == "complete" ||
45 win.document.readyState == "interactive") &&
46 win.location.href == targetURL
48 Services.tm.dispatchToMainThread(callback);
50 docShell.chromeEventHandler.addEventListener("DOMContentLoaded", onReady);