0

I want to create a dropdown menu in my Chrome extension, which requires that I call a Javascript function (myFunction()) to trigger the dropdown.

However, the function myFunction is not being called. I know that the javascript file is running, because the console.log("in interface") message I logged appears in console.

Note: the line console.log("in toggle") does not appear to be called.

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
  ]
}
Zoe Sheill
  • 11
  • 1
  • 4
  • Where is the html and where is the JS file? In a browser action, background page, content script..? – Karl Reid Jun 30 '17 at 21:40
  • I added the manifest file - the JS file is in a content script – Zoe Sheill Jun 30 '17 at 23:00
  • 1
    If the html snippet is inside an extension page such as the browser action popup, inline code like onclick doesn't work there, you need a separate js file and addEventListener. The same applies if html is injected into the web page. Also make sure you've read the extension overview, the architecture section in particular. – wOxxOm Jul 01 '17 at 06:46
  • Ok. Thank you very much! – Zoe Sheill Jul 01 '17 at 20:27

0 Answers0