Search code examples
javascriptjqueryuserscriptsfirefox2

How do I get jQuery to work with a Greasemonkey 0.8 script (On Firefox 2), with no internet to the PC?


I'm using Firefox 2.0.0.11 and Greasemonkey 0.8.x and the latest jQuery version (1.3.2) that works with Greasemonkey 0.8.
I try to load this Userscript:

// ==UserScript==
// @name        TEST
// @include     *
// @require     jquery.min.js
// @grant       GM_getValue
// ==/UserScript==

$(document).ready(function(){
    alert('jQuery!');
});     

but I receive the error:

Error: $ is not defined
Source File: file:///G:/Firefox/Data/profile/gm_scripts/huhu/huhu.user.js
Line: 8

I know that I have to update this old browser. But it's not possible. I have to work with that!

I want to load jQuery code on a local PC with no internet connection. I have copied jquery.min.js in the same folder where the userscript is.

G:/Firefox/Data/profile/gm_scripts/huhu/jquery.min.js

Any ideas where the problem is?

Regards, Bernte


Solution

  • There are two ways to install a Greasemonkey script, from a local drive, such that jQuery will work.
    But, first, here are the...

    Prerequisites common to both methods:

    1. Older versions of Greasemonkey (and Firefox) got confused easily. So, use Greasemonkey's Script Manager to uninstall any old versions of the script.

    2. Go to the gm_scripts folder, in your Profile Folder, and physically delete the folder for your script, if it's present.

    3. Make sure your script source-code is in a file with a user.js extension. EG: Hello World.user.js

    4. Make sure your script source-code is in a folder that is not in a system TMP or temp folder. For example, place the source file in C:\My GM scripts\.

    5. Likewise, this is not, and do not use, any folder in the Firefox Profile Folder tree.

    6. When in doubt, or if things appear "flaky", restart Firefox.


    Method 1, the computer has internet access:

    1. Merely point the @require at Google's copy of the correct jQuery version. For GM 0.8, it should be no later than jQuery 1.3.2.

      // ==UserScript==
      // @name     Hiya Ma
      // @include  http://stackoverflow.com/*
      // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
      // ==/UserScript==
      
      $("body").prepend ('<div>Hello world!</div>');
      


    2. Use the Firefox, Open File menu (CtrlO) to open the source file. (EG: C:\My GM scripts\Hello World.user.js)

      Or:
      Drag-and-drop the source file to any open Firefox tab.

    3. Greasemonkey will prompt to install the script. Follow the prompts.


    Method 2, the computer cannot access the web:

    1. Download the appropriate jQuery version (1.3.2 in this case), and save it as jquery.min.js, in the same folder as the script source (C:\My GM scripts\, in this example).

    2. Do not use any path in the @require directive:

      // ==UserScript==
      // @name     Hiya Ma
      // @include  http://stackoverflow.com/*
      // @require  jquery.min.js
      // ==/UserScript==
      
      $("body").prepend ('<div>Hello world!</div>');
      


    3. Install the script as in steps 2 and 3 of Method 1. You do not need to open or drag the jQuery file.


    Also Note:

    1. G:/Firefox/Data/profile/gm_scripts/huhu/jquery.min.js look like a Firefox profile directory. You place no files here. Greasemonkey will copy and rename files as appropriate.

    2. @grant is not supported until Greasemonkey version 1.0. Do not use it here.

    3. $(document).ready() is not needed here, nor in most GM scripts. Greasemonkey fires at the correct time by default.

    4. These methods were verified against Firefox 2.0.0.20 and Greasemonkey 0.8.20100408.6, but they pretty much apply to all versions of FF+GM until GM 1.0.


    What to do if you get an `NS_ERROR_FILE_ACCESS_DENIED` error:

    This error was not uncommon with older versions of Greasemonkey. The two most frequent causes were poor temp-file housekeeping (by FF/GM) and Firefox profile corruption.

    Do the following to remedy this in most cases:

    1. Repeat the prerequisites, listed above.

    2. Purge the system temp folder(s) of anything that resembles the script source file, or any @required files, or any @resourced files.

      In this example, look for hiya_ma.user.js and jquerymin.js, in particular.

      In Windows, the temp folder(s) have the address(es): %TMP% and %TEMP%.

    3. In the gm_scripts folder, make sure that config.xml has no references to deleted or missing scripts. If there are no (other) scripts, delete config.xml.

    4. If all else fails, uses Firefox's Profile Manager to create a new profile and reinstall your scripts there.