我为firefox开发了一个小插件,其中保存了一些http代理,因为我经常使用它们。加载到ff中的所有代理都会在弹出窗口中要求输入用户名和密码。我的插件保存了这些信息,所以我需要找出一种方法来设置通知框中输入字段的值,以便向代理服务器进行身份验证,并避免每次更改代理时都必须手动输入这些详细信息。
我必须承认,我对XUL和javascript也是一个完全的初学者。我在网上搜索并测试了所有类型的代码片段,但我无法获得通知内容,无法确定它是否是我要查找的内容,也无法输入正确的值。
我有一段代码,它可能接近我想要的东西,但似乎不起作用:
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);您在那里看到的字符串"is requesting a username and password“是通知窗口上显示的文本的一部分。如果我做错了什么,有人能发现吗?
任何帮助都是非常感谢的。谢谢!
发布于 2010-12-11 02:52:25
您收到的通知框是否正确?您可以将getChromeWindow函数简化为:
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)
您可能还需要chromeWin变量的.wrappedJSObject。
(http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js#1295)
发布于 2010-12-11 03:14:59
根据您的描述,您尝试拦截的对话框实际上是身份验证对话框,而不是通知栏(在输入密码后出现,以确认您是否希望保存/更改密码)。换句话说,听起来你想修复Mozilla bug 223636 (https://bugzilla.mozilla.org/show_bug.cgi?id=223636)。Firefox4Beta实际上已经有了针对这种情况的修复(https://bugzilla.mozilla.org/show_bug.cgi?id=521467),但默认情况下它是禁用的(需要使用about:config更改隐藏的首选项)。
不幸的是,您粘贴的代码对此并不是很有用。要从当前发布的版本中的扩展捕获身份验证对话框,您可能需要一个遵守“公共对话框加载”主题的代码组合和来自http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/test/prompt_common.js的对话框处理逻辑。如果你在irc.mozilla.org/#extdev上找到我,我可以提供更多细节。
https://stackoverflow.com/questions/4405988
复制相似问题