0

I'm running a couple of self-written Greasemonkey userscripts that almost always needed the unsafeWindow function in some way. Unfortunately, that function has been discontinued for the most part, but now I'm having trouble fixing my scripts. I've read that there are three new functions (cloneInto(), exportFunction(), and createObjectIn()), but I openly admit that I don't get what this page wants me to do.

Can somebody give me a quick explanation of which of these three functions I should use and of course also how to convert them to work again? I'm sorry, but I'm not very good at this stuff.

As a simple example:

$("#someid").append('<a href="#" onclick="somefunction();return false;">Click</a>');

unsafeWindow.somefunction = function() {
    alert("Hello!");
}
Selbi
  • 813
  • 7
  • 23
  • That duplicate has pretty much nothing to do with my problem. Either that or I can't seem to find any connection. – Selbi Sep 19 '14 at 22:30
  • 1
    I rater think that it does have to do with your problem ... – SamB Jan 03 '15 at 19:28

1 Answers1

0

Greasemonkey 2.0 radically changed unsafeWindow handling. Now you should use

unsafeWindow.somefunction = exportFunction(function() {
    alert("Hello!");
}, unsafeWindow);

or

exportFunction(function() {
    alert("Hello!");
}, unsafeWindow, {defineAs: "somefunction"});

See exportFunction in MDN. You may also be interested on createObjectIn and cloneInto.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • I said I know of these three functions. What I don't know is how to use them properly. Also, in that code you provided I can't see the name anymore ("somefunction"). – Selbi Sep 19 '14 at 21:45
  • @user3216060 I forgot that part, fixed. What exactly don't you understand? – Oriol Sep 19 '14 at 21:49
  • Hmm, I still seem to get "ReferenceError: somefunction is not defined". I tried both methods. It's like Firefox/Greasemonkey outright ignore the presence of this function. Any idea what's wrong? – Selbi Sep 19 '14 at 21:56
  • 1
    @user3216060 Do you get that error in your GM script or in target page? How do you attempt to use `somefunction`? Are you sure you attempt to use it after exporting it? – Oriol Sep 20 '14 at 21:24