John Smith
John Smith

Reputation: 625

chrome extension close or update tabs

I want to code an extension that opens the same page in 10 tabs and then updates those pages in a certain interval. If updating is not possible it should close those tabs and open them again. Right now I have a javascript running as a background-script

chrome.browserAction.onClicked.addListener(function(activeTab){

var openTabs = 0;
var tabArray = [];

promise = window.setInterval(function() {
    chrome.tabs.create({ url: "http://some.com/url/" });
    }, 2000);
});

Is there a way to save the IDs of the tabs I already opened?

Upvotes: 0

Views: 170

Answers (1)

peterdotjs
peterdotjs

Reputation: 1556

Yes there is a way to save the ids of the tabs that you have created/opened.

When you call chrome.tabs.create in the callback parameter you have access the tab object that is created. You then have access to the tabId and can store it using localStorage to keep track of them. Something like this:

localStorage.setItem('tabIds',JSON.stringify([])) //init the store

chrome.tabs.create({ url: "http://some.com/url/" }, function (tab){
  var tabs = JSON.parse(localStorage.getItem('tabIds')); 
  tabs.push(tab.id);
  localStorage.setItem('tabIds',JSON.stringify(tabs)); //store array of tabsIds
});

Upvotes: 1

Related Questions