按钮点击不工作在谷歌扩展

button click not working in google extension

本文关键字:谷歌 扩展 工作 按钮      更新时间:2024-02-19

hi我正在制作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");
 }

尝试以下片段:

在XML:中

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

一旦加载了弹出文档主体,这个脚本就应该将事件附加到元素上。

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

你也可以检查这个相关的问题:点击Chrome扩展不工作

希望这能有所帮助!

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

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