[英]Chrome Extension Fill in Selected Field
I'm trying to build a very simple chrome extension that fills the selected form field on a page.我正在尝试构建一个非常简单的 chrome 扩展来填充页面上的选定表单字段。 I've created a git repo with the code:
我用代码创建了一个 git repo:
https://github.com/mastermindg/chrome-extension-fillform https://github.com/mastermindg/chrome-extension-fillform
My problem is that nothing happens when I click the button.我的问题是当我点击按钮时没有任何反应。
Here's my background js:这是我的背景js:
chrome.tabs.getSelected(null, function(tab) {
d = document;
var formfield = d.activeElement;
d.getElementById(formfield).value = Math.floor(89+Math.random()*11);
});
I suspect it's around the js element selection process but I'm not sure.我怀疑它与 js 元素选择过程有关,但我不确定。
It's not that perfect solution but the best I've seen so far...这不是那么完美的解决方案,而是迄今为止我见过的最好的解决方案......
I ended up using browser_action instead of background and using chrome.tabs.executeScript to affect the active tab.我最终使用 browser_action 而不是 background 并使用 chrome.tabs.executeScript 来影响活动选项卡。 I updated my github repo as well with the current code.
我也使用当前代码更新了我的 github 存储库。
chrome.tabs.executeScript(null,
{code:"var valued = Math.floor(89+Math.random()*11);var formfield = document.activeElement;formfield.value = valued"}
);
You should replace d.getElementById(formfield).value
with just formfield.value
.此时应更换
d.getElementById(formfield).value
只有formfield.value
。
var formfield = d.activeElement;
gets the element for you, so d.getElementById(formfield)
is not needed.为您获取元素,因此
d.getElementById(formfield)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.