1

It keeps saying $ is not defined in tamper-monkey, even though I already @require the necessary link. What am I doing wrong?

// ==UserScript==
// @name         New Userscript
// @author       You
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @include https://www.google.com/
// ==/UserScript==

$(document).ready(function(){
  console.log('ready');
});
frosty
  • 2,559
  • 8
  • 37
  • 73
  • Works for me, when I put in a `@include` and something into the function – CertainPerformance Aug 31 '19 at 06:17
  • @CertainPerformance You mean change the require into include? – frosty Aug 31 '19 at 06:17
  • No, I mean when I specify a site to run the userscript on. `// @include https://example.com/` (you did do that, right?) – CertainPerformance Aug 31 '19 at 06:19
  • @CertainPerformance Yeah. But I mean inside the tampermonkey editing part, to the left of the $.document it says $ is not defined. – frosty Aug 31 '19 at 06:21
  • Also I got this in console: Failed to load resource: net::ERR_BLOCKED_BY_CLIENT – frosty Aug 31 '19 at 06:22
  • You may have an adblocker or something interfering https://stackoverflow.com/questions/23341765/getting-neterr-blocked-by-client-error-on-some-ajax-calls – CertainPerformance Aug 31 '19 at 06:25
  • Make sure you don't have `@grant none`, lots of people use it needlessly. – wOxxOm Aug 31 '19 at 06:35
  • @wOxxOm I may be one of those people, I always include it because Greasemonkey [highly recommends](https://github.com/greasemonkey/greasemonkey/issues/1645) specifying @ grant - users of a userscript without a @ grant in GM will see warnings. Having TM just [*guess*](https://www.tampermonkey.net/documentation.php#_grant) at what's needed (if anything) doesn't seem like a great idea either. Do you have a link or a Q/A describing what you're thinking about? – CertainPerformance Sep 18 '19 at 07:10
  • Since `@grant none` is a special mode that disables the sandbox, it should never be used unless you understand what that is and you really want this for your script. Also, Greasemonkey author is known to make pretty controversial decisions from time to time like breaking the API so I would take any of their advice with a grain of salt so if GM wants some `@grant` being present you can probably try `@grant foo`. – wOxxOm Sep 18 '19 at 07:46
  • Personally I would *strongly* advise against using `@grant none` because by disabling the sandbox it allows the page to intercept, spoof, or break the standard things that your script uses like Array.prototype or Object.prototype. I'm assuming of course that Greasemonkey properly guards this stuff in its sandbox, which it should. – wOxxOm Sep 18 '19 at 08:11

2 Answers2

5

Your adblocker appears to be blocking the https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js url. Either turn off the adblocker, or choose a different source for jQuery.

Once that's fixed, what you're seeing in the userscript interface is a linter warning, not a Javascript error. If you specify a page for the script to run on, the script will still run just fine. The warning is there to tell you that you haven't explicitly defined a $ variable yet; it doesn't know that what you've @required will define $.

To make the linter happy, tell it that $ is a global variable which is already defined:

...
// @include          https://example.com/
// ==/UserScript==

/* global $ */

$(document).ready(function(){
  console.log('ready');
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

You can use vanilla JS to load jQuery.

// Loads jQuery and triggers a callback function when jQuery has finished loading
function addJQuery(callback) {
    let script = document.createElement('script');
    script.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js');
    script.addEventListener('load', function() { callback(); }, false);
    document.body.appendChild(script);
}

// The main script
function main() {
     // put your tampermonkey code here
}

// Load jQuery and then execute the main function
addJQuery(main);

This loads on any site regardless of adblocker.

Kxmode
  • 251
  • 1
  • 5
  • 11