title: Callback of chrome.tabs.executeScript is called before I want it to, need workaround
body: <p>I have written an extension in Google Chrome that opens a tab to a webpage and injects code into that page. I have also registered a callback in <code>chrome.tabs.executeScript</code> to be called after the code's completion. This callback then opens another tab to another page and performs other actions, but I don't want that to happen until the first tab's page has been manipulated.</p>
  
  <p>My issue is that this callback is called when it should be called, but not when I want it to be called:</p>
  
  <p>My injected code contains a <code>setInterval</code> that checks to see if certain DOM elements are present before acting on the page, and I don't want the callback to activate until those DOM elements have been acted on.</p>
  
  <p>I don't know of any way to have the injected code interact with the code in my extension page.</p>
  
  <p>Is there any way to tell the code in my extension page that it's okay to keep going and call the callback at the right time?</p>
  
  <p>Here's some code that explains what I'm doing now:</p>
  
  <pre><code>function doStuffWithTabs() {
      chrome.tabs.create({url: 'some/url/here'}, function(tab) {
          chrome.tabs.executeScript(tab.id, code: "var interval = setInterval(function(){if(jQuery(domElement).length){clearInterval(interval);workMagic();}},1);"}, function(){
              goForthAndConquer();
          });
      }
  }
  </code></pre>
  
  <p><code>workMagic()</code> represents the code that manipulates the DOM of the opened tab and <code>goForthAndConquer()</code> is what I want to happen when the manipulation of the opened tab is complete.</p>
  
  <p>Right now, as you can plainly see, the injected code just registers the setInterval and immediately calls the callback. Again, I want the callback to happen after <code>workMagic()</code>.</p>