You can do the following, but you have to parse an external script within your function:
function a(window, document){
Or, if you have a server proxy, you can rewrite your code using the wrapped anon function, which is then called in your proxy document and window object.
;(function(window, document){
However, there are ways around this, as they may use the following depending on the JS environment:
var win = (function(){return this;})();
You may also need to include other collections to make sure they are not available:
;(function(window, document, all, images, ...){ ... }
But they will also be able to access the original document through any dom elements that you also allowed them to ...
Regarding UPD6
Just in case, this is useful, you can also connect the following holes:
Both of these values โโcan be used to evaluate the code.
setTimeout('(function(){alert('+'th'+'is'+');})()');
Plus, when exposing document.write this is also doable:
document.write( '<img src="data:image/gif;base64,ERROR" '+ 'onerror="alert(th'+'is.ownerDocument);" />' );
And you must block access to SafeThis and rewrite any mention of it in the target code, otherwise it can be canceled:
SafeThis = function(that){return that;}
Also, although it seems pretty safe. I am sure that there will be other ways around you: if you are zealous enough, but it really depends on how confident you are that your potential attackers might be;)