Search code examples
javascriptgoogle-chrome-extensionmessage-passing

How do I send tab ID at creating?


So I want to create new tab and execute script on new tab and use variables from message sended earlier

chrome.tabs.create({url: myUrlblabla}, function() {
  chrome.tabs.executeScript(null, {file: 'myFileScript.js'}, function(Tab tab) {
    chrome.tabs.sendMessage(tab.id, message.myVariable)
  })
});

And Chrome is saying that there is "unexpected identifier" in this line.


Solution

  • You had a syntax error (Tab tab). Try this code:

    chrome.tabs.create({
      url: myUrlblabla
    }, function(tab) {
      chrome.tabs.executeScript(tab.id, {
        file: 'myFileScript.js'
      }, function(results) {
        chrome.tabs.sendMessage(tab.id, message.myVariable);
      });
    });