按钮单击无法在谷歌扩展中工作

时间:2016-04-06 12:35:58

标签: javascript json node.js google-chrome google-chrome-extension

您好我正在制作Chrome扩展程序,它会重定向到我的网站并自动填写密码。     我无法提交按钮点击事件。

manifest.json  // manifest json code 

{
  "name": "z",
  "version": "0.1",
  "description": "z",
  "background": { 

     "scripts":["redirect.js"]
     },
   "manifest_version": 2,
  "permissions": [
    "http://Abcd:90/Login.aspx"
  ],
  "browser_action": {
      "default_icon": {                    
        "19": "icon.png"  //you have to put an image icon.png in to the folder.                 
      },
      "default_title": "Redirect" //optional 
   }

}   


----------------------------redirect.js------------------

//试过

 chrome.browserAction.onClicked.addListener(function(tab) {

        debugger
        chrome.tabs.create({ url: "http://Abcd:90/Login.aspx" });

            hello();


});


    function hello() {

        debugger
        var myUsername = 'username';
        var myPassword = 'password';
        // find the fiends in your lo
        document.getElementsByName('ctlLogin$UserName').value = myUsername;
        document.getElementsByName('ctlLogin$Password').value = myPassword;
        var link = document.getElementById('ctlLogin_LoginButton');      
        link.addEventListener('DOMContentLoaded', function () {   // unable to perform click.
            var link = document.getElementByName('ctlLogin$LoginButton');
            // onClick's logic below:
            alert(link);
            link.addEventListener('click', function () {
                hellYeah('xxx');
            });
        });

//也尝试了这个

   var link = document.getElementById('ctlLogin_LoginButton');
        link.addEventListener('click', function () {
            alert('gg');
        });

    }

//也是这个

document.addEventListener('DOMContentLoaded', function () {
         document.querySelector('ctlLogin_LoginButton').addEventListener('click', showalert, false);
 }, false);

 function showalert() {
     alert("you just pressed the button");
 }

2 个答案:

答案 0 :(得分:0)

请尝试以下代码段:

在XML中:

<input id="ctlLogin_LoginButton" type="submit" value="Submit">

一旦加载了弹出文档正文,此脚本应将事件附加到元素。

document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("ctlLogin_LoginButton").addEventListener("click", hello);
});

您还可以查看以下相关问题:onClick within Chrome Extension not working

希望这有帮助!

答案 1 :(得分:0)

将处理程序追加到任何现有元素

$(document).on("click","#ctlLogin_LoginButton",function(e) {
    console.log("button clicked");
})