简体   繁体   中英

XUL getNotificationBox

I have developed a small addon for firefox in which I save some http proxies since I work a lot with them. All of the proxies, once loaded into ff, ask for a username and password in a popup window. My addon has this information saved so I need to figure out a way of setting the value of the input fields in that notification box which is displayed in order to authenticate to the proxy server and avoid having to manually enter those details every time I change my proxy.

I must admit that I'm an absolute beginner with XUL and javascript as well. I searched the web and tested all kinds of snippets but I can't get the notification content, decide if it's what I'm looking for and enter the right values.

I have a piece of code which probably gets near to what I want but it doesn't seem to work:

function getNotificationBox() {
        const Ci = Components.interfaces;

        function getChromeWindow(aWindow) {
                var chromeWin = aWindow
                .QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIWebNavigation)
                .QueryInterface(Ci.nsIDocShellTreeItem)
                .rootTreeItem
                .QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIDOMWindow)
                .QueryInterface(Ci.nsIDOMChromeWindow);
                return chromeWin;
        }

        var notifyWindow = window.top;

        var chromeWin = getChromeWindow(notifyWindow);

        var notifyBox = chromeWin.getNotificationBox(notifyWindow);

        return notifyBox;
}

function clickNotificationButton() {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var box = getNotificationBox();
        var bar = box.getNotificationWithValue("is requesting a username and password");
        var button = bar.getElementsByTagName("button").item("OK");
        button.doCommand();
}

window.addEventListener("DOMNodeInserted", function(e) { clickNotificationButton;
}, false);

The piece of string you see there "is requesting a username and password" is part of text that is shown on the notification window. Can someone spot if I'm doing something wrong here?

Any help is much appreciated. Thanks!

Are you getting the notification box correctly? You can simplify your getChromeWindow function to:

function getChromeWindow(aWindow) {
    var chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                           .getInterface(Ci.nsIWebNavigation)
                           .QueryInterface(Ci.nsIDocShell)
                           .chromeEventHandler.ownerDocument.defaultView;
    return chromeWin;
}

(http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js#1208)

You may also need an .wrappedJSObject to your chromeWin variable.

(http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js#1295)

From your description, it sounds like the dialog you're trying to intercept is actually the authentication dialog, rather than the notification bar (which appears after the password has been entered to confirm whether you want it saved/changed). In other words, it sounds like you want to fix Mozilla bug 223636 (https://bugzilla.mozilla.org/show_bug.cgi?id=223636). Firefox 4 betas actually already have a fix for exactly this case (https://bugzilla.mozilla.org/show_bug.cgi?id=521467), but it's disabled by default (requires changing a hidden pref using about:config).

The code you pasted isn't going to be very useful for that, unfortunately. To catch authentication dialogs from an extension in currently shipping versions, you probably want a combination of code that observes the "common-dialog-loaded" topic and the dialog handling logic from http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/test/prompt_common.js . I can provide more detail if you find me on irc.mozilla.org/#extdev.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM