title: How to write Greasemonkey scripts for Chrome so that you can unit-test it?
body: <p>I am writing a Greasemonkey user script that should use jQuery and work on Google Chrome and Firefox. </p>
  
  <p>I have seen several examples of how to do this, including very good answers here on SO. And all of them boil down to calling an "injection script" function passing another callback function as parameter. </p>
  
  <p>The code inside that callback function is where the "magic" happens, including access to the <code>jQuery</code> (<code>$</code>) object.</p>
  
  <p>This solution works fine. But, one of the consequences of using it is that functions defined <em>outside</em> the callback function cannot be invoked from within it:</p>
  
  <pre><code>function doSomethingImportantThatIWantToUnitTest(){ ... }
  
  function with_jquery(callback) {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.textContent = "(" + callback.toString() + ")(jQuery)";
      document.body.appendChild(script);
  };
  
  with_jquery(function ($) {
      doSomethingImportantThatIWantToUnitTest(); // &lt;---- meh. Not defined!
  });
  </code></pre>
  
  <p><br>
  So, I can only use functions defined <em>inside</em> the callback function. But these functions, in turn, cannot be called from outside. In particular, they cannot be called from a unit test, for example, which is very annoying to me. </p>
  
  <p>Is there a way to write Greasemonkey scripts for Chrome and yet unit-test it?</p>