为Chrome扩展程序调用Javascript函数失败

时间:2017-06-30 21:36:55

标签: javascript html google-chrome google-chrome-extension

我想在Chrome扩展程序中创建一个下拉菜单,这需要我调用Javascript函数(myFunction())来触发下拉列表。

但是,函数myFunction未被调用。我知道javascript文件正在运行,因为我记录的console.log(" in interface")消息出现在控制台中。

注意:似乎没有调用line console.log(" in toggle")。

HTML:

<div class="dropdown">
            <button onclick="myFunction()" class="dropbtn">Dropdown</button>
              <div id="myDropdown" class="dropdown-content">
                <a href="#home">Home</a>
                <a href="#about">About</a>
                <a href="#contact">Contact</a>
              </div>
            </div>

使用Javascript:

    console.log("in interface");
     /*When the user clicks on the button, 
    toggle between hiding and showing the dropdown content */
    function myFunction()  {
        console.log("in toggle");


    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

的manifest.json:

   {
  "manifest_version": 2,

  "name": "Hello World",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0"/*,
  "background": {
    "scripts": ["background.bundle.js"]
  }*/,
  "browser_action": {
    "default_icon": "./icons/icon.png",
    "default_popup":  "account.html"
  },
  "manifest_version": 2,
  "content_scripts": [
    {
        "css": ["style.css"/],

      "js": [ "content.bundle.js", "interface.js"],
        "matches": ["<all_urls>"]
    }
  ], //if you want to inject it from a background page'

  "permissions": [
    "background", //if you want to inject it from a background page
    "http://www.google.com/" // host permission to google
  ]
}

0 个答案:

没有答案