chyoo CHENG
chyoo CHENG

Reputation: 720

chrome extension refresh two tab simultaneously

I should write a chrome extension than can refresh two special tabs simultaneously when I click the extension icon. but I just find the code:

chrome.browserAction.onClicked.addListener(function(tab))

the function(tab) can not push two tabs but only one. How can I do it?

Upvotes: 0

Views: 146

Answers (1)

Siddharth
Siddharth

Reputation: 7156

Well to refresh a tab, you only need tab id.So, you can use chrome.tabs.reload() on any number of tabs you like.

Example:

     chrome.browserAction.onClicked.addListener(function(){
           chrome.tabs.query({currentWindow: true},function(tabs){// This will return all tabs in current window
           //If you want to reload first 2 tabs 
            chrome.tabs.reload(tabs[0].id);
            chrome.tabs.reload(tabs[1].id);
    })
 })

Lets say if you want to reload current active tab and the tab left to it. Then pass active:true along with currentWindow:true to get active tab. Then use index property to reload left or right tab.

Example:

  chrome.browserAction.onClicked.addListener(function(){
           chrome.tabs.query({active:true,currentWindow: true},function(tabs){
            var currentIndex = tabs[0].index;
            var leftIndex = tabs[0].index - 1; 
             chrome.tabs.query({currentWindow: true},function(tabs){// This will return all tabs in current window
             chrome.tabs.reload(tabs[currentIndex].id);
             chrome.tabs.reload(tabs[leftIndex].id);
       });
    })
 })

Upvotes: 1

Related Questions