我正在分发一个firefox插件https://github.com/jixiuf/firefox-passwordmaker
用热键创建一个面板 我希望隐藏面板后,密码字段可以获得焦点。 在创建面板后,当前的显示窗口(选项卡)似乎失去焦点。 在pannel hide之后它无法恢复。
var windows = require("sdk/windows");
browserWindows=windows.browserWindows;
var lastFocusedWindow; // do not use this ,use getLastFocusedWindow()
function getLastFocusedWindow() {
if (lastFocusedWindow==undefined) {
if (browserWindows.length==1) {
lastFocusedWindow=browserWindows[0];
}
}
return lastFocusedWindow;
}
var panel = panels.Panel({
contentURL: self.data.url("panel.html"),
contentScriptFile: scripts,
height: 170,
onHide: function handleHide() {
getLastFocusedWindow().activate(); //this doesnot work
getLastFocusedWindow().tabs.activeTab.activate(); //this doesnot work
tabWorkers[tabs.activeTab.id].port.emit("set-focus");//
}
});
self.port.on("set-focus", function() {
if (lastFocusedInputEl!=undefined) {
// if last focused password field is not undefined,then auto fill it.
lastFocusedInputEl.focus();// this doesnot work
}
})
答案 0 :(得分:0)
有焦点管理器,您可以使用它来获取整个窗口或子帧等最近关注的元素。这是一个片段:
var exampleTabSelected = function(event) {
window.setTimeout(function(){
var fm = Components.classes["@mozilla.org/focus-manager;1"].getService(Components.interfaces.nsIFocusManager);
var aXULWindow = window;
//if fm.focusedWindow == [object ChromeWindow] then it == aXULWindow
//if fm.focusedWindow == [object XrayWrapper [object Window]] then it is aXULWindow.gBrowser.contentWindow
//Cu.reportError(fm.focusedWindow)
//Cu.reportError(fm.focusedWindow == aXULWindow.gBrowser.contentWindow)
if (fm.focusedWindow == window) {
Cu.reportError(fm.focusedElement.nodeName);
var browserInTab = event.target.linkedBrowser;
var aFocusedWindow = {};
var subFocusedEl = fm.getFocusedElementForWindow(browserInTab.contentWindow, true, aFocusedWindow);
Cu.reportError('focused of tab contents window: ' + subFocusedEl)
Cu.reportError('subFocusedEl has caret = ' + subFocusedEl.
}
}, 10); //can be 1ms
}
gBrowser.tabContainer.addEventListener("TabSelect", exampleTabSelected, false);