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
]
}