getting the URL from a sidebar or popup
To retrieve the URL from a sidebar or popup requires tab permissions
"permissions": [
"tabs"
]
then you need to find the tab you want. If you just want the active tab this would work fine, for anything more advanced I'd look here.
function getPage(){
browser.tabs.query({currentWindow: true, active: true})
.then((tabs) => {
console.log(tabs[0].url);
})
}
getting the URL from injected javascript
If you want the URL for a background task I suggest this method as you do not need permissions.
this will give you a background script and then inject a script onto almost any webpage on the internet.
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["https://www.*"],
"js": ["modify-page/URL.js"]
}
],
this will be injected into webpages through the URL js and will send a message to your background js to use.
var service= browser.runtime.connect({name:"port-from-cs"});
service.postMessage({location: document.URL});
This code is in your background js and will collect each new page's url as it changes.
var portFromCS;
function connected(p) {
portFromCS = p;
portFromCS.onMessage.addListener(function(m) {
if(m.location !== undefined){
console.log(m.location);
}
});
}
browser.runtime.onConnect.addListener(connected);
document.location
from it. – Homoio