0

I have a chrome extension that takes the URL from the active tab, finds another tab in the current window through tabs.query, and will log something to console when found.

My question is, how to a get the tabId from the variable that stores the tabs.query?

      //click button to trigger event
      $('#geturlbutton').on('click', function(){
        console.log('geturlbutton clicked')

        //get current URL for for active tab
        chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
          currentURL = tabs[0].url;
          console.log(currentURL);

          //find the tab with mywebsite
          villageTab = chrome.tabs.query({url: "*://*.mywebsite.com/*"}, vilTab);

          //once found run function to execute script in mywebsite tab
          function vilTab(tabs){
            chrome.tabs.executeScript(villageTab.tabId,{  <-- this is where error occurs
              code: "console.log('executeScript worked');"
            });  
          };

        });

The chrome.tabs.executeScript(villageTab.tabId, is clearly not the way to get the tabId of that object. Please tell me how to get the tabId so I can execute a script in that tab?

IWI
  • 1,528
  • 4
  • 27
  • 47
  • 1
    The first `.query` is correct, but then you somehow forget it's async and try to use its direct return value. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) – wOxxOm Sep 16 '16 at 19:27
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Makyen Sep 16 '16 at 20:27

0 Answers0