lundi 8 mai 2017

If/else loop executes both conditions

OK, so I have the following code. The idea is to check if a page exists, if not, open a new tab. If exists, navigate to it.

chrome.browserAction.onClicked.addListener(function(tab) {
  function check() {
        chrome.tabs.query(
            {currentWindow: true, url: 'http://ift.tt/UHDl5t'},
            function(tabs) {
            tabs.forEach(function(tab) {
              console.log('Tab ID, URL: ', tab.id, ' ', tab.url);
                  if(tab.url !== '')
                  {
                    var updateProperties = {"active": true};
                    chrome.tabs.update(tab.id, updateProperties, function(tab){ });
                    console.log('Assign true to functon');
                    return true;
                  }
            });
        });
      };

      if(check() === true) {
        console.log('Function is true, do nothing')
      }
      else {
        console.log('Function is false, open page')
        chrome.tabs.create({ url: "https://www.google.com"});
      }
});

The first part works correctly, but the second one executes both true and false sections. It's like it executes the if/else statement before the function?

Aucun commentaire:

Enregistrer un commentaire