How to create a class object that is defined on a remote page?

For example, a remote web page has a snippet of code, for example:

<script>
function foo(){
this.bar = 0;
}

In my greasemonkey script, I want to create an object of this class:

var _foo= unsafeWindow['foo'];
new _foo();

Then I got the error "Invalid value".

+3
source share
1 answer

Here's how to do it:

var _foo = eval('(' + unsafeWindow.foo.toSource() + ')');
var x = new _foo();

This workaround may be required due to the various security zones or sandboxes that Greasemonkey does, although I'm not quite sure.

+1
source

All Articles