38

I am trying to get a Greasemonkey script to run on a page. But it does not.

How do you debug scripts?

What are the smallest possible baby-steps to start a new Greasemonkey script?

Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • 1
    BTW i solved my specific problem. It turned out that "included pages" in the "manage user scripts" dialog included "lavpriskoekken.dk/*" and not "*lavpriskoekken.dk/*" – Jesper Rønn-Jensen Sep 11 '09 at 13:26
  • 4
    Same thing happens to me. Hello World will run correctly, then I'll add a line and it stops working. If I take that like back out, it never works again. Does greasemonkey crash or something and require a reboot of the interpreter? How do you do that? –  Feb 09 '11 at 21:11
  • You don't need to "reboot" Greasemonkey. It will try to run your script every time a page that matches your included pages is loaded into the browser. So the way to "reboot" is to hit Refresh on your browser. – Keir Finlow-Bates Apr 20 '11 at 12:51
  • 4
    I'm having the same problem as Ken, adding a line makes a script stop responding, and removing that line doesn't fix the problem. Driving me batty. It's like the Greasemonkey engine is caching a broken version of the script and not letting it go or something. Frustrating. – Scott Jan 27 '12 at 13:30
  • 1
    Same issue as Ken and Scottie - simple "hello world" script works, add a line and it fails, remove the line, and it won't work anymore. Driving me nuts. – Tim Holt Jan 30 '12 at 23:58
  • @JesperRønn-Jensen Since the "accepted" answer doesn't fit the title of the question it shouldn't be the accepted answer even though it relates to one of the questions in the details. Maybe you have come up with the baby steps you asked for by now and can give a fitting answer, e.g.: make new GM script, set include to exact page address, have an alert; if it works keep copying actual code, then replace pieces of include with wildcards. – valid Mar 18 '15 at 20:59
  • The fix for me was using the "Web console" with Ctrl+Shift+K, not Ctrl+Shift+J – jaggedsoft Oct 28 '16 at 02:57

9 Answers9

49

When I was starting with user scripts, my first error was not using wildcards.

// Broken:
// @include        https://www.example.com/

// Working:
// @include        https://www.example.com/*

That's the simplest way for the script to not even apply, and thus not run at all. In addition, make sure to check for syntax errors; a misplaced semicolon can prevent the script from being parsed, and thus it is ignored.

Myrddin Emrys
  • 42,126
  • 11
  • 38
  • 51
  • This worked for me but that's just vague! I am creating a script for FB and I had `@include https://www.facebook.com` and it didn't load although the exact address is `https://facebook.com`. So the "pattern" matches even when there is no forward slash, but it doesn't match when the string is 100% the same. Weird. – MarioDS Feb 23 '15 at 18:58
  • 7
    To be fair, www.facebook.com != facebook.com. – Myrddin Emrys Feb 24 '15 at 13:50
9

For me there was a syntax error in the script and Greasemonkey didn't give an error message. Copy-pasting the script to the JS Console revealed the secret.

Bernát
  • 1,362
  • 14
  • 19
7

you can write logs everywhere in your scripts to get better traces of what if being done in them:

GM_log("Hello, World!");

http://wiki.greasespot.net/GM_log

More info: http://wiki.greasespot.net/Greasemonkey_Manual:Other_Useful_Tools#JavaScript_Console

Another tip: Take a look at the whole greasemonkey wiki. They have a lot of good stuff in there: http://wiki.greasespot.net/Main_Page

Santi
  • 4,428
  • 4
  • 24
  • 28
  • 15
    Thanks but at the time I needed this, my script was not even running -- could not even run statements like `alert("hello");`. So what i hope is to build a checklist with steps you can check if your script is not even running. – Jesper Rønn-Jensen Sep 12 '09 at 05:34
  • 5
    As of Greasemonkey 3.2 (Firefox 38, OS X) `GM_log` does not seem to work. – Lenar Hoyt May 31 '15 at 20:38
  • 1
    @LenarHoyt is correct. `console.log();` should be used instead. – fedxc Jul 03 '19 at 18:06
5

My greasemonkey script was working; I added a line; it stopped working; I removed the line; it still didn't work.

I determined that greasemonkey had installed a broken updater.php script when I reloaded the script with the new line added. Reloading the old script kept the broken version of the updater.php script. I eventually found an error message referring to the updater.php script in my Firefox Web console. I replaced the updater.php script with a file containing a comment after loading the new version of the script, and my script started being executed.

On mac, my updater.php was in: /Users/csimmons/Library/Application Support/Firefox/Profiles/81ri7k71.default/gm_scripts/Extended_LoU_Tweak

You may be able to find a similar updater.php file in your Firefox default profile gm_scripts/ directory.

fuxia
  • 62,923
  • 6
  • 54
  • 62
Chuck Simmons
  • 491
  • 5
  • 5
5

In Firefox, open Tools > Error Console and watch it as you load the page. If you're getting overwhelmed by Warnings from the page you can filter on Errors, which still often highlights your script problem.

Keir Finlow-Bates
  • 993
  • 2
  • 14
  • 27
  • 2
    It supposed to be this way in FF. However nowadays UserScript certain errors (like the script fails to compile) neither show up in Web Console (`Ctrl+Shift+K`) nor Browser Console (`Ctrl+Shift+J`). Looks like we have to switch to Chrome to debug GM scripts .. – Tino Jan 03 '16 at 02:04
4

Make sure that Greasemonkey is enabled in the first place:

Lenar Hoyt
  • 5,971
  • 6
  • 49
  • 59
2

Also, the syntax of @include is to have only one matching pattern. If you want another one, add another @include line.

aksu
  • 5,221
  • 5
  • 24
  • 39
Christophe
  • 13
  • 4
2

If the URL of a page is set with Javascript instead of by navigating (like Instagram giving its popup windows a permalink URL), Greasemonkey will show the script as running but it actually won't start. Try reloading the URL and seeing if it actually goes to the same page. If not, you'll have to match the home page URL instead of the popup URL.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
1

I had this trouble too; this was the solution for me:

  • Check the (and Modify the tricky) part of the code I was working on when it happen

  • uninstall and reinstall GreaseMonkey

  • then i got a real error report

EDIT: Today the probleme come more often than usually (new release?) It looks like Firefox web tools are fighting with greasemonkey or JS engine for error messages.

So when you're looking somewhere (ctrl+alt+k) the error message appears somewhere else (try opening the other console) it's like a funny game.

toriningen
  • 7,196
  • 3
  • 46
  • 68
zede
  • 1
  • 2