Javascript Firefox插件:关闭面板后将焦点返回到输入元素
我正在开发一个扩展来生成密码。它有一个按钮和一个面板。按钮打开面板,面板用JS加载html,一切正常。 在面板中键入时,我生成密码并将其传播到带有“.port.emit”方案的页面。在那里我制作了“document.activeElement.value=pass”,没关系,它可以工作。 然后,面板关闭,用户希望提交表单,但该字段(或表单)中没有焦点!用户必须用鼠标单击提交按钮。 我试过了。focus(),但似乎焦点在其他地方,根本不在页面上 面板隐藏后,如何将焦点返回到页面本身 main.js:Javascript Firefox插件:关闭面板后将焦点返回到输入元素,javascript,firefox,firefox-addon,firefox-addon-sdk,Javascript,Firefox,Firefox Addon,Firefox Addon Sdk,我正在开发一个扩展来生成密码。它有一个按钮和一个面板。按钮打开面板,面板用JS加载html,一切正常。 在面板中键入时,我生成密码并将其传播到带有“.port.emit”方案的页面。在那里我制作了“document.activeElement.value=pass”,没关系,它可以工作。 然后,面板关闭,用户希望提交表单,但该字段(或表单)中没有焦点!用户必须用鼠标单击提交按钮。 我试过了。focus(),但似乎焦点在其他地方,根本不在页面上 面板隐藏后,如何将焦点返回到页面本身 main.js
var data = require("sdk/self").data;
var tabs = require("sdk/tabs");
var { Hotkey } = require("sdk/hotkeys");
// Create a button
var button = require("sdk/ui/button/action").ActionButton({
id: "show-panel",
label: "myPass",
icon: {
"16": "./padlock-16.png",
"32": "./padlock-32.png",
"64": "./padlock-64.png"
},
onClick: handleClick
});
var currentUrl = '';
// Show the panel when the user clicks the button.
function handleClick(state) {
currentUrl = tabs.activeTab.url;
pass_panel.show();
//if (tabs.activeTab.url)
// console.log('Current url is: ' + tabs.activeTab.url);
}
var hotKey = Hotkey({
combo: "f9",
onPress: function() {
handleClick();
}
});
// Construct a panel, loading its content from the "text-entry.html"
// file in the "data" directory, and loading the "get-text.js" script
// into it.
var pass_panel = require("sdk/panel").Panel({
width: 300,
height: 300,
position: button,
contentURL: data.url("addon.htm"),
contentScriptFile: data.url("addon.js")
});
// When the panel is displayed it generated an event called
// "show": we will listen for that event and when it happens,
// send our own "show" event to the panel's script, so the
// script can prepare the panel for display.
pass_panel.on("show", function() {
pass_panel.port.emit("show", currentUrl);
});
pass_panel.on("hide", function() {
pass_panel.port.emit("hide");
var worker = tabs.activeTab.attach({
contentScriptFile: data.url("page.js")
});
worker.port.emit("focusPass");
});
// Listen for messages called "text-entered" coming from
// the content script. The message payload is the text the user
// entered.
// In this implementation we'll just log the text to the console.
pass_panel.port.on("text-entered", function (text) {
//console.log(text);
var worker = tabs.activeTab.attach({
contentScriptFile: data.url("page.js")
});
worker.port.emit("enterPass", text);
require('sdk/timers').setTimeout(closePanel, 5000);
});
function closePanel() {
pass_panel.hide();
}
addon.js(从面板的addon.html中的标记加载):
page.js:
var passWindow = document.activeElement;
// "self" is a global object in content scripts
// Listen for a "enterPass"
self.port.on("enterPass", function(pass) {
passWindow.value = pass;
});
self.port.on("focusPass", function() {
console.log("focusing!");
passWindow.focus();
});
后一个passWindow.focus();不起作用,因为页面根本没有焦点:(如果没有代码,这个问题可能与主题无关:寻求调试帮助的问题(“为什么代码没有按我希望的方式工作?”)必须包括:•期望的行为,•特定的问题或错误,以及在问题本身中•**复制它所需的最短代码**。没有明确问题陈述的问题对其他读者没有用处。请参阅:,和。我们通常会尝试提供经过测试和有效的答案。如果我们要验证我们的解决方案对您有效如果我们有您的代码,那么这样做会容易得多,而不必全部编造。要做到这一点,我们每个人都必须首先复制您遇到的问题,然后找到解决方案。没有a形式的源代码会帮助您做更多的工作。没有这样的代码,就没有动力为您找到答案。您可以使用Services.focus,它使用nsIFocusManager获取最后一个焦点元素。添加了代码。如果您有一些问题,请随时提问。您是否取消了nsIFocusManager?
var passWindow = document.activeElement;
// "self" is a global object in content scripts
// Listen for a "enterPass"
self.port.on("enterPass", function(pass) {
passWindow.value = pass;
});
self.port.on("focusPass", function() {
console.log("focusing!");
passWindow.focus();
});