带标签的Chrome扩展程序

时间:2012-06-17 19:33:56

标签: javascript google-chrome tabs google-chrome-extension autofill

我过去三天都在努力尝试这项工作,所以请不要回复Chrome API的链接或类似内容。

我正在开发一个与mySql数据库通信的Chrome扩展程序,以获取用户最喜欢的网站,以及每个网站的登录名/密码(比如f​​acebook fbuser fbpass,google gguser ggpass ...)

因此,扩展的目标是为每个网站打开一个新标签,然后进行登录。

我已经建立了与数据库的通信,我可以成功地打开网站的新标签,但我已经堆积在每个网站上插入用户名和密码。我的问题是我不能使用document.getElementsById('input')...因为它返回弹出窗口中的元素,而不是网页上的元素。

所以我读过chrome.tabs.executeScript(),我正在尝试使用它(这似乎是我需要用来访问网页的DOM)。

所以我使用的是:

chrome.tabs.create({"url": web[i],"selected":false},function(tab) {
        chrome.tabs.executeScript(tab.id, {file:"write.js"}, function(){
        });
        });

“write.js”必须自动填充。

所以这里开始我的问题:

我有一个“警告(”我在“);” write.js中的消息,它永远不会显示(所以函数的其余部分永远不会被调用)。 (我在互联网上找到了这个函数“write.js”。)

在这里,我将发布我的弹出式HTML,清单和js的部分内容,也许我必须在那里更改一些东西,因为我已经尝试了我在大多数示例中看到的并且不适合我。

的manifest.json:

{
  "name": "MyExtension",
  "version": "1.0",
  "description": "My chrome extension",
  "icons":{
      "128":"icon_128.png"
  },
  "browser_action":   {
      "default_icon": "icon.png",
      "popup": "consulta_empleados.html"
  },
  "permissions": [
    "http://*/*",
    "https://*/*"
  ]
}

consulta_empleados.html(弹出窗口):

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Consulta Registro con AJAX</title>
<script src="ajax.js"></script>
</head>
<body>
<form name="form1" method="post" >
  <label>
  <div align="center">Email<br>
    <input type="text" name="email" id="email">
  </div>
  </label>
  <p align="center">
    <label>
    <input type="submit" name="open" id="open" value="Log In" onClick="runScript()">
    </label>
  </p>
</form>
<div id="resultado"></div>
<div id="prova"></div>
</body>
</html>

档案ajax.js:

function runScript(){
      ...
      //here I fill a vector with the usernames, the websites and the passwords from the data coming from my database. var num is the number of websites
      ...
  var i=0;
  while(i<num){
    chrome.tabs.create({"url": web[i],"selected":false},function(tab) {
        chrome.tabs.executeScript(tab.id, {file: "write.js"}, function(){

        });
        });
    i++;
  }

}

write.js:
function write() {
     ...
      alert("I'm in");
     ...      
} 
write();

提前致谢。

2 个答案:

答案 0 :(得分:0)

恕我直言,而不是“选择”:假你需要“有效”:假

你也可以通过在chrome.tabs.executeScript之前添加另一个警告来查看这个,看看是否正在调用chrome.tabs.create的回调

答案 1 :(得分:0)

看看你的devtools'控制台,必须有一些错误信息。 要使用内容脚本,您需要tabs权限

{
  "name": "My extension",
  ...
  "permissions": [
    "tabs", "http://www.google.com/*"
  ],
  ...
}