I'm new to javascript and I want to code a firefox web extension. I have a browser action button with a popup. but I didn't define the popup in the manifest, i set it in javascript code, because the click event won't be fired when the popup is defined. So here is the important part of my background script:
browser.browserAction.onClicked.addListener((tab) => {
var tabUrl = tab.url;
browser.browserAction.setPopup({ popup: "/popup/popup.html" });
browser.browserAction.openPopup();
browser.browserAction.setPopup({ popup: "" });
});
In this event the tab object is passed, so I can use the url.
This file is in the /background_scripts folder. The popup is in the /popup folder. It's a html file with 2 menuitems. In the popup.js I have an event to get the click:
document.addEventListener("click", (e) => {
if(e.target.id == menuItem1)
{
...
//here i want to use the url of the current tab
});
How I can get the tab object or the url in my popup code?
The tabs.tab.getCurrent() method doesn't work as I understand according to this:
tabs.getCurrent() result is undefined?
and this
How do I include a JavaScript file in another JavaScript file?
doesn't work too.