0

I want to save the url of the current tab but when I execute the code it doesn't save the url in the variable.

/* global var */
var currentURL;

function getCurrentURL() {
  chrome.tabs.query({ active: true, currentWindow: true}, function(tabs) {
    var tab = tabs[0];
    currentURL = tab.url;
  });
}

function addChannel() {

}

document.addEventListener('DOMContentLoaded', function () {
  getCurrentURL();
  console.log(currentURL);
  if (currentURL !== "https://www.youtube.com/channel/*") {
    document.getElementById('addChannel').remove();
  }
  else {
    document.getElementById('addChannel').addEventListener('click', 
  function() {addChannel();});
  }
})
Landerrrm
  • 13
  • 1
  • 3
  • Does the answer in the linked question make sense, @Landerrrm? The problem is that getCurrentURL() doesn't actually wait for the chrome.tabs.query asynchronous call to complete; that's what *asynchronous* means. To fix this, you would add a callback argument to getCurrentURL which you call after setting currentURL. Then put everything after the call to getCurrentURL() in an anonymous function that you pass like so: `getCurrentURL(function () { ... });`. The [accepted answer](http://stackoverflow.com/a/14220323/237285) goes into more details. – Andres Riofrio Apr 18 '17 at 20:35
  • I do not understand it. After al lot of googling I don't understand how I need to use a callback. – Landerrrm Apr 19 '17 at 09:44

0 Answers0