0

I have tabs permission, from my sidebar I'm running the following code:

var currentUrl ='';
getPage();
function getPage(){
  browser.tabs.query({currentWindow: true, active: true})
    .then((tabs) => {
      currentUrl = tabs[0].url;
  })
}

The return is: about:debugging#/runtime/this-firefox after reading some answers on this sub, I supposed it would return the current tab URL, what is the problem?

update: after reloading the add-on multiple times, it is now correctly reading the first current url, however it is not updating it as I navigate, here is the other half of sidebar code:

let btn_main = document.getElementById("btn_main");
btn_main.addEventListener("click", () => {

  LiveConsole(currentUrl);

});  

//show debug messages on the sidebar since console.log is not working
function LiveConsole(message){
  var tag = document.createElement("p"); // <p></p>
  var text = document.createTextNode(message); 
  tag.appendChild(text); // <p>TEST TEXT</p>
  var element = document.getElementById("debug");
  element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>
}

update: added the window.onload = getPage; but still it is not being run after every page load / news url visited etc...

S. W. G.
  • 433
  • 1
  • 6
  • 19
  • 1
    The `then` shows you that `browser.tabs.query` returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) (explained [in the documentation](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/query)), and you can't expect [your other code to expect that to return something immediately](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) and act on it. – Andy Mar 26 '22 at 18:48
  • @Andy I don't understand, even if I click after minutes the current url is not returned, see my second update as I tried to bind it to `window.onload` event. How else can I get current tab url when I press a button on the sidebar? – S. W. G. Mar 26 '22 at 19:02

0 Answers0