True Believer
True Believer

Reputation: 139

How can Chrome extension functionality be run independently for each tab?

I've been developing chrome extension for dealing with different tab DOM but with development so far, it seems that an extension can only handle one tab at a time.

background.js has method which calls recursively with 10 sec interval :

function sendMessageScheduler()
{
    console.log(tabId); // tab is created for each new tab

     timer = setTimeout(function(){

     chrome.tabs.sendMessage(tabId, {text: 'click_question', 
                                      currentTabId: tabId}, doStuffWithDom);
            sendMessageScheduler();

     }, 10000); 
}

pop.js :

startButtonButton.addEventListener('click', function() {

      chrome.extension.getBackgroundPage().sendMessageScheduler();

  }, false);

contentscript.js :

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {

    if (msg.text === 'click_question') {
   }
});

manifest.json :

{
  "manifest_version": 2,
  "name": "Click Question",
  "version": "1.0",
  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },
  "content_scripts": [{
    "matches": ["*://*.stackoverflow.com/*"],
    "js": ["contentscript.js"]
  }],
  "browser_action": {
     "default_icon": "question.png",
    "default_title": "Click Questions Plugin",
      "default_popup": "popup.html"
  },
  "permissions": ["activeTab"]
}

What I've assumed before implementing above logic is that the extension will handle each tab independently but with development, it seems that if I execute setTimeout function for one tab, then other tab running setTimeout function stops and vice-versa.

How can I should handle this case so that different timeout function works independently?

Upvotes: 0

Views: 642

Answers (1)

Haibara Ai
Haibara Ai

Reputation: 10897

You can get all tabs with the following code

chrome.tabs.query({}, function(tabs) {
    for (var i = 0; i < tabs.length; i++) {
        var tabId = tabs[i].id;
        // do what you want
    }
});

Upvotes: 1

Related Questions