0

I found a function (via this person's github) that I might use in my script that mimics the functionality of an API object.

Here's the relevant code from the link:

unsafeWindow = (function() {
    var e1 = document.createElement('p')
    e1.setAttribute('onclick', 'return window;');
    return e1.onclick();
})();

Where the poster says you can use the function in the format unsafeWindow.jQuery

Now, I want to be able to use $ instead of the jQuery keyword elsewhere in my code. I tried learning from this stack overflow question to simplify it and re-wrote the code like so:

(function($){
    var e1 = document.createElement('p')
    e1.setAttribute('onclick', 'return window;');
    return e1.onclick();
})(jQuery);

But it didn't work. I guess I could just try something like $ = unsafeWindow.jQuery in order to map to the $, but I wanted to try to do it in the format seen above.

Community
  • 1
  • 1
tempcode
  • 175
  • 1
  • 2
  • 11
  • Are you really making an extension? That is, do you have a `manifest.json`? If so, then this is a `content-script` question, not a `userscripts` question. PS: [making a userscript into a full-featured content script](http://stackoverflow.com/a/5259212/331508) is easy. – Brock Adams Apr 18 '13 at 02:34
  • 1
    I just changed it for you. What you are doing, currently, *is* a userscript and not what most would consider an extension (But there is a lot of overlap in Chrome). – Brock Adams Apr 18 '13 at 02:50
  • 1
    Since you are starting out, I suggest you install the Tampermonkey extension. Then you can write one script that will almost always work the same in both GM+Firefox and Tampermonkey+Chrome. Tampermonkey has a lot of other advantages over ordinary Chrome userscripts. – Brock Adams Apr 18 '13 at 02:53

1 Answers1

1

You would map $ to unsafeWindow.jQuery like so:

unsafeWindow    = ( function () {
    var dummyElem   = document.createElement('p');
    dummyElem.setAttribute ('onclick', 'return window;');
    return dummyElem.onclick ();
} ) ();

var $ = unsafeWindow.jQuery;

// Now you can use the page's jQuery. EG:
$("body").append ('<p>Content added by unsafeWindow.jQuery</p>');


But keep in mind:

  1. This is a Hack, and it will probably stop working around Chrome version 28.

  2. It may still fail due to a race condition about when userscripts fire. To fix that, add // @run-at document-end to the userscript's metadata block.

  3. Don't do things this way! It will only cause grief, side effects and maintenance headaches.

    For userscripts: use this technique (best cross-browser)  or  this technique (relies on page's jQuery, but the example shows how to use GM_ functions too).

    For full extensions or content scripts:, use this technique (use the manifest.json and keep everything properly sandboxed).

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • In all 3 methods I linked, you don't use `unsafeWindow` at all. You should never use `unsafeWindow` for a library like jQuery. And you should avoid using `unsafeWindow` at all, if you can help it. `unsafeWindow` is for those pretty-rare scenarios when you can't figure any other solution but to use a page's *unique* JavaScript object, and when the use seems too trivial to use script injection. The `manifest.json` approach gives your script its own copy of jQuery, removing the need for `unsafeWindow` and providing a host of other benefits (beyond scope of this question). – Brock Adams Apr 18 '13 at 03:15
  • But there's my problem now, for the purposes of my script I need to access and use a certain website's unique JavaScript object(s). How could I do so if I'm not using `unsafeWindow` or a function that mimics it? (I'm sorry if this seems like a redundant question or silly even (my own research only yielded `unsafeWindow` as a means of accessing these hidden unique objects), you're being very helpful and I appreciate your time). – tempcode Apr 18 '13 at 03:55
  • Open a new question and give ***specifics*** of the both the code you are trying to use, your real goal, and what you have tried. *This* question is answered and comments are not designed for endless follow-up or tangential questions. – Brock Adams Apr 18 '13 at 04:08