【问题标题】:Creating Chrome Extension - How do I populate a form创建 Chrome 扩展程序 - 如何填充表单
【发布时间】:2010-06-30 18:29:42
【问题描述】:
我正在创建一个 Google Chrome 扩展程序并希望在页面上填充表单字段。
我正在尝试这样的事情但没有效果:
chrome.tabs.executeScript(null,
{code:"document.body.form[0].email_field='" + email + "'"});
}
【问题讨论】:
标签:
google-chrome
google-chrome-extension
【解决方案1】:
- 您应该确保您的 manifest.json 中有“tabs”权限:
{
"name": "123 Testing",
"version": "0.1",
"description": ":-)",
"browser_action": {
//"default_icon": "my_icon.png",
"default_title": "Click to fill the form"
},
"background_page": "background.html",
"permissions": [
"tabs",
"http://*/"
]
}
- 我认为您应该使用 document.forms 而不是 document.body.form 访问表单。
查看我的 background.html 文件,并使用 google.com 对其进行测试:
<html>
<head>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {
code: "document.forms[0]['q'].value='Hello World!'"
})
});
</script>
</head>
<body></body>
</html>
(我通常会使用 document.getElementById)。
祝你好运!