tamlevn
tamlevn

Reputation: 3

Chrome Extension callback function after create tabs not working

I create tab then I can't use callback function

   document.getElementById("login").addEventListener('click', function(){
    chrome.tabs.query({}, function(tabs){
      chrome.tabs.query({active: true,currentWindow: true}, function (tabs) {
        chrome.tabs.create({url: url_homepage, active: true}, function (tabs){
          alert('abc');
        });
      });
    });
  });

But it's not working, I try to use chrome.tabs.onUpdated.addListener like this

  document.getElementById("login").addEventListener('click', function(){
chrome.tabs.query({}, function(tabs){
  chrome.tabs.query({active: true,currentWindow: true}, function (tabs) {
    chrome.tabs.create({url: url_homepage, active: true}, function (tabs){
      chrome.tabs.onUpdated.addListener(function listenEvent(tabid,info,tab){
        if(info.status == "complete" && tab.url.indexOf(url_homepage) != -1 ){
          alert('abc');
        }
      });
    });
  });
});

But it's still not working Can anyone help me, please?

Upvotes: 0

Views: 380

Answers (1)

Aefits
Aefits

Reputation: 3467

While creating the new tab just make active: false you should be good to go. Use active: true when you want to focus to the newly created tab.

also, you don't actually need to query tabs when creating a new tab.

Change this code:

document.getElementById("login").addEventListener('click', function(){
    chrome.tabs.query({}, function(tabs){
      chrome.tabs.query({active: true,currentWindow: true}, function (tabs) {
        chrome.tabs.create({url: url_homepage, active: true}, function (tabs){
          alert('abc');
        });
      });
    });
  });

To this should also work.

document.getElementById("login").addEventListener('click', function(){
    chrome.tabs.create({url: url_homepage, active: false}, function (tabs){
      alert('abc');
    });
});

Upvotes: 1

Related Questions