My very simple Greasemonkey script is not running?

John Galt picture John Galt · Jun 27, 2015 · Viewed 10.8k times · Source

I am having issues with this very basic Greasemonkey script, most likely with the metadata configuration.

Here is the full source of the basic file

// ==UserScript==
// @name        Google Hello
// @namespace   https://google.com
// @description Basic Google Hello
// @include     *
// @version     1
// ==/UserScript==

alert("hi google!");

This script should run when I access Google.com, but the alert is not popping up. What is the issue?

I am attempting to run this script on Ubuntu with Firefox.

Answer

Brock Adams picture Brock Adams · Jun 27, 2015

If alerts() are not firing, chances are you may have clicked Firefox's Prevent this page from creating additional dialogs option, or set a browser preference (older versions of Firefox), or Firefox may have become unstable in memory.

Universal Greasemonkey debug steps:

(With one step added for problems with alert().)

  1. First make sure that the script is even firing for the page in question.
    While browsing that page, click on the down-triangle next to the Greasemonkey icon (Alternatively, you can Open Tools -> Greasemonkey on the Firefox menu.) and verify that the expected script name appears and is checked. EG:
    Greasemonkey states

  2. See if there are any relevant messages/errors on Firefox's Browser Console.
    Activate the console by pressing CtrlShiftJ, or equivalent.

    Here's a screenshot showing how both messages and errors appear in the Browser Console -- caused by both the web page and the Greasemonkey script:
    Sample Browser console results

  3. Open about:config, search for capability.policy.default.Window.alert and delete or reset the value, if it is found.

  4. Uninstall the Greasemonkey script.
  5. Completely clear the browser cache.
  6. Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
  7. Restart Firefox.
  8. Install the Greasemonkey script afresh.
  9. If it still doesn't work, create a new Firefox profile or try a different computer altogether.

Additional issues:

  1. Please supply your versions of three things: (1) The OS, (2) Firefox, (3) Greasemonkey or Tampermonkey or Scriptish, etc.

  2. @include * means that the script will fire for every page! This is almost always a poor practice. (There are some exceptions, but your case is not one.)

  3. @namespace does not control where the page runs. The only thing @namespace does is allow more than one script to have the same name (as long as their @namespaces are different). See the @namespace documentation.

  4. Avoid using alert() for debugging. It's annoying and can mask timing problems.
    Use console.log(). You can see the results, and helpful error messages (hint, hint) on the Browser Console.

  5. Google almost always uses/redirects to www.google.com (For English USA users). So, // @include  https://google.com will almost never work like you want.

    Recommend you use:

    // @match  *://www.google.com/*
    

    as a starting point.

    In Firefox Greasemonkey, you can also use the magic .tld to support most of Google's international domains, like so:

    // @include  http://www.google.tld/*
    // @include  https://www.google.tld/*
    

    Use both lines. Note that this does not perform as well as the @match line does. So, if you only care about one nation/locale, just use @match.


Putting it all together:

  1. Uninstall your script.
  2. Restart Firefox.
  3. Install this script:

    // ==UserScript==
    // @name        Google Hello
    // @namespace   John Galt
    // @description Basic Google Hello
    // @match       *://www.google.com/*
    // @version     1
    // @grant       none
    // ==/UserScript==
    
    console.log ("Hi Google!");
    
  4. Visit Google and note the results on Firefox's Browser Console.

  5. If there is still a problem, follow all of the debug steps above.
  6. If there is still a problem, Open a new question and supply ALL of the following:
    1. The three versions, mentioned above.
    2. The relevant errors and messages you get on the Browser Console.
    3. The exact code and steps needed to duplicate the problem. Make an MCVE for this!
    4. A short summary of what you have tried to solve the problem.