2010-06-30 47 views
0

我正在創建Google Chrome擴展程序並希望在頁面上填充表單字段。創建Chrome擴展程序 - 如何填充表單

我想這樣的事情有沒有影響:

chrome.tabs.executeScript(null, 
     {code:"document.body.form[0].email_field='" + email + "'"}); 
} 

回答

1
  1. 你應該確保你在你的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. 我相信你應該訪問帶有文檔的表單.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)。 祝你好運!