开发者

Creating Chrome Extension - How do I populate a form

I am creating a Google Chrome extension and want to populate a form field on a page.

I am trying something like this with no effect:

 chrome.tabs.executeScript(null,
      {code:"doc开发者_Python百科ument.body.form[0].email_field='" + email + "'"});
}


  1. You should make sure you have the "tabs" permission in your manifest.json:
{
  "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://*/"
  ]
}
  1. I believe you should access forms with document.forms and not document.body.form. See my background.html file, and test it with 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>

(I would have usually used document.getElementById). Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜