How to write Greasemonkey scripts for Chrome so that you can unit-test it? How to write Greasemonkey scripts for Chrome so that you can unit-test it? google-chrome google-chrome

How to write Greasemonkey scripts for Chrome so that you can unit-test it?


You should be able to pass anything you want into the callback function, including function variables.

var f = function doSomethingImportantThatIWantToUnitTest(){ ... }function with_jquery(callback) {    var script = document.createElement("script");    script.type = "text/javascript";    script.textContent = "(" + callback.toString() + ")(jQuery,f)";    document.body.appendChild(script);};with_jquery(function ($, f) {    f(); // <---- DEFINED!});

If you have more than a few functions you want to do, and don't want to update the code in several different places, you can just pass in an object or array that has all the functions as object properties or elements in an array.

Although if it were me, I would just define the functions within the scope you are using them.