Working at migrating my old Firefox extension to the up-to-date Webextension format. Earlier, I was able to get the URL of the active tab in the following way:
var URL = tabs.activeTab.url;
Now, it doesn't work. I saw some references for tabs.getCurrent() and tabs.Tab -> url, but didn't find a single example on how to use it. So how I can get the URL of the active Firefox tab and place it into a variable for further usage?
Thanks, Racoon
Assuming you have the "tabs" permission listed in your manifest.json, you can get the url of the active tab in a background script by using the following:
// verbose variant
function logTabs(tabs) {
let tab = tabs[0]; // Safe to assume there will only be one result
console.log(tab.url);
}
browser.tabs.query({currentWindow: true, active: true}).then(logTabs, console.error);
// or the short variant
browser.tabs.query({currentWindow: true, active: true}).then((tabs) => {
let tab = tabs[0]; // Safe to assume there will only be one result
console.log(tab.url);
}, console.error),
In a content script, you can get the url by using:
let url = window.location.href;
Don't use browser.tabs.getCurrent because it doesn't return the active tab, but instead the tab where the calling code runs, which may be an inactive tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With