0

<browser>我正在尝试在元素中加载指定的 url 之前执行一些操作。<browser>可以是侧边栏或选项卡中。

示例:(上下文是浏览器主窗口):

// it can be also browser of tab
var browser = document.getElementById("sidebar"); // <xul:browser>

// execute a function when window of browser will be prepared for loading new content but before any script will be executed
execOnWindowPrepared(browser, function(window) {
    // e.g. inject something
    window.foo = "bar";
});

// load an uri 
browser.loadURI("chrome://foo/content/bar.xul"); // (can be also http:)

注意我不能在loadURI语句之后立即执行我的操作,因为browser.contentWindow它仍然指向旧窗口。

也许可以为此使用进度侦听器,但我对需要等待哪些标志感到困惑。

4

2 回答 2

0

我以使用这种方法结束。它是通过反复试验编码的,因此在某些情况下可能是错误的,但我没有注意到许多测试有任何问题。

// opens aUrl in aBrowser and call an action defined in aCallback immediately after window is prepared
function loadInBrowserWithCallback (aBrowser, aUrl, /* function(window) */ aCallback) {
    var loadedUrl = nsIIOService.newURI(aUrl, null, null).spec // canonify chrome url (what mean 'canonifing': https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIChromeRegistry#canonify%28%29 )
    var lastWindow = aBrowser.contentWindow;
    lastWindow.__thisIsPrevWin = true;
    var progressListener = { 
        onLocationChange : function(aWebProgress, aRequest, aLocation, aFlags) { 
            var window = aWebProgress.DOMWindow;
            if(!window.__thisIsPrevWin && window.location.href === loadedUrl) {
                // the property assigned to previus window was removed and location.href isn't any transitional url
                aBrowser.removeProgressListener(this);
                aCallback(window);
            }
        },
        onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
            var window = aWebProgress.DOMWindow;
            if(!window.__thisIsPrevWin && window.location.href === loadedUrl) {
                aBrowser.removeProgressListener(this);
                aCallback(window);
            }
        },
        onSecurityChange: function(aWebProgress, aRequest, aState) {
            var window = aWebProgress.DOMWindow;
            if(!window.__thisIsPrevWin && window.location.href === loadedUrl) {
                aBrowser.removeProgressListener(this);
                aCallback(window);
            }
        },
        onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
            var window = aWebProgress.DOMWindow;
            if(!window.__thisIsPrevWin && window.location.href === loadedUrl) {
                aBrowser.removeProgressListener(this);
                aCallback(window);
            }
        },
        onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {
            var window = aWebProgress.DOMWindow;
            if(!window.__thisIsPrevWin && window.location.href === loadedUrl) {
                aCallback(window);
                aBrowser.removeProgressListener(this);
            }
        },
        QueryInterface: function(aIID) {
            if (aIID.equals(Ci.nsIWebProgressListener) ||
                aIID.equals(Ci.nsISupportsWeakReference) ||
                aIID.equals(Ci.nsISupports))
                return this;
           throw Cr.NS_NOINTERFACE;
        }
    };
    aBrowser.addProgressListener(progressListener, Ci.nsIWebProgress.NOTIFY_ALL);
    aBrowser.loadURI(aUrl);
};
于 2013-11-16T10:14:19.013 回答
0

使用nsIObserverService 的 content-document-global-created 或 document-element-inserted 通知。您使用哪一个取决于您的脚本在做什么。如果它只是像您的示例中那样设置窗口级变量,那么 content-document-global-created 将是最好的。如果您需要插入元素或以其他方式使<html>元素可用,则 document-element-inserted 会更好。

document-element-inserted 是Greasemonkey在包含 @run-at document-start 时使用的方法。

于 2013-11-15T22:12:24.997 回答