Using jQuery in Tampermonkey

jquery, tampermonkey

Solution

EDIT: Since Greasemonkey 2.0 you do not (and cannot) use `unsafeWindow.jQuery` for getting the host page's instance of jQuery. You just use `window.jQuery`, and `// @grant none` (or `@grant` whatever other special APIs you need for non-jQuery things).

Sources: https://github.com/greasemonkey/greasemonkey/issues/1952 http://www.greasespot.net/2014/06/greasemonkey-20-release.html

Tampermonkey currently (Feb 2015) seems to work similarly by default settings. (archived on 2015-03-12 from http://tampermonkey.net/faq.php#Q404)

old outdated answer below:

Add a directive `// @grant unsafeWindow`. Also, if you are getting your jQuery reference from the host window object, you won't need the `@require` line.

Problem

I'm using Chrome 27.0.1453.116 m and have enabled "Experimental Javascript", however I'm unable to get jQuery to run on Tampermonkey. I have tried: ``` // ==UserScript== // @name My Fancy New Userscript // @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js // ==/UserScript== var $ = unsafeWindow.jQuery; var jQuery = unsafeWindow.jQuery; ``` However, I get an error on the line `var $ = unsafeWindow.jQuery;` highlighting `unsafeWindow` saying unsafeWindow was used before it was defined. How to fix this?

Original source