简体   繁体   English

Chrome扩展程序可在Chrome浏览器弹出窗口中单击按钮后打开新的弹出网址

[英]Chrome extension to open a new popup url on button click in chrome browser popup window

I am tring to open a new popup url in chrome extension pop up on the onclick event of a button. 我正在尝试在按钮的onclick事件上弹出的chrome扩展名中打开一个新的弹出网址。 In my case I have a page which will display two buttons on startup. 就我而言,我有一个页面,该页面在启动时将显示两个按钮。 This has been set in the useropt.html page 这已在useropt.html页面中设置

useropt.html useropt.html

<script src="scripts/usmanager.js"></script>
<body>
    <div id="btn_wrap_inner">   
        <button class="btn" id="signin">Sign In</button><br /><br />
        <button class="btn" id="signup">Sign Up</button>
    </div>
</body>

The JS file usnmanager.js has js code which has code to open a new popurl url in the popup window on the onlick event of the buttons above. JS文件usnmanager.js具有js代码,该代码具有在上述按钮的onlick事件上的弹出窗口中打开新popurl url的代码。

usnmanager.js usnmanager.js

function btn_clicked(tgtUrl) {
    if(tgtUrl == "signin") {
        var manager_url = "signin.html";
        chrome.tabs.query(
        {
            currentWindow: true,
            active : true
        }, function(openselection) {
              chrome.browserAction.setPopup({
                  popup: manager_url
           });
        });
    } else if(tgtUrl = "signup") {
        var manager_url = "signup.html";
        chrome.tabs.query(
        {
            currentWindow: true,
            active : true
        }, function(openselection) {
              chrome.browserAction.setPopup({
                  popup: manager_url
           });
        });
    }
}

document.addEventListener("click", function() {
  document.querySelector('#signin').addEventListener('click', btn_clicked('signin'));
  document.querySelector('#signup').addEventListener('click', btn_clicked('signup'));
});

But when I am clicking either of the button the popup window is not loading the new popup page as set. 但是,当我单击任一按钮时,弹出窗口不会按设置加载新的弹出页面。 Please let me know what have I done wrong 请让我知道我做错了什么

I would not try to pass the string in the event handler but use an attribute of the clicked button 我不会尝试在事件处理程序中传递字符串,而是使用clicked按钮的属性

function btn_clicked() {
  var id=this.id; 
  if(id == "signup") {

Also in you code you test signup twice and you only add the event handler when you click something. 同样在您的代码中,您两次测试signup ,并且仅在单击某些内容时才添加事件处理程序。 Instead you likely want 相反,您可能想要

document.addEventListener("DOMContentLoaded", function() {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM