Chrome扩展如何在任何选定的文本字段中输入文本
当您按Ctrl+Shift+U时,我正在制作一个镀铬扩展名,该扩展名将生成昵称,如果选择一个,则应在选定的文本字段中自动输入生成的名称。我的问题是文本字段有多种类型,最基本的是输入。目前,这是生成昵称的功能,该功能适用于输入字段和TextaRea字段,但没有其他作用。
//Nickname gets generated here
//...
var inp = document.querySelectorAll("input");
var txt = document.querySelectorAll("textarea");
for (var i=0;i<inp.length;i++) {
if (inp[i] == document.activeElement) {
inp[i].value = nick;
}
}
for (var i=0;i<txt.length;i++) {
console.log(txt[i]);
if (txt[i] == document.activeElement) {
txt[i].innerHTML = nick;
}
}
基本上,我认为我需要的是某种功能,可以像用户键入名称一样,而无需直接更改字段的值。
I am making a Chrome extension that generates nicknames when you press Ctrl+Shift+U, and it should automatically enter the generated name in the selected text field, if one is selected. My problem is that there are many types of text fields, the most basic being input. This is currently my function that generates the nickname, which works for input fields and textarea fields, but for nothing else.
//Nickname gets generated here
//...
var inp = document.querySelectorAll("input");
var txt = document.querySelectorAll("textarea");
for (var i=0;i<inp.length;i++) {
if (inp[i] == document.activeElement) {
inp[i].value = nick;
}
}
for (var i=0;i<txt.length;i++) {
console.log(txt[i]);
if (txt[i] == document.activeElement) {
txt[i].innerHTML = nick;
}
}
Basically, what I think I would need, is some kind of function that can act like the user is typing the name, without directly changing the value of a field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 wOxxOm 的回答,经过一些修改,我让它工作了。这是我的最终代码:
Thanks to the answer of wOxxOm, with a bit of modification, I got it to work. This is my final code: