问题
I'm trying to get a Greasemonkey Userscript to work but it keeps throwing "missing ; before statement" in the Javascript error console.
The Greasemonkey docs say the line number should be ignored but since the script is quite lengthy it would be very helpful to know where the error occurred. How can I find out?
Edit: So, long story short. Line numbers are correct in recent Greasemonkey versions.
回答1:
The latest editions of Greasemonkey seem to report line numbers adequately, but it is still best to debug and test as much of your script as possible in the Firebug console before using it in the Greasemonkey script.
And, as missingno said, jsHint can be good for catching these kinds of problems.
Anyway, suppose I have a script like this:
// ==UserScript==
// @name _Debugging test
// @include http://YOUR_SERVER/YOUR_PATH/*
// ==/UserScript==
unsafeWindow.console.log ('Line 1', 1 + 0);
unsafeWindow.console.log ('Line 2', 1 + 1);
unsafeWindow.console.log ('Line 3', 1 + 2);
unsafeWindow.console.log ('Line 4 **Throw error here**', 1 + 3 + nonExistantVariable);
unsafeWindow.console.log ('Line 5', 1 + 4);
When I run it on 2 of my systems (WinXP, FF: 10.0.2, GM: 0.9.18, Firebug: 1.9.1, and the same except GM: 0.9.17), I get this on Firebug's console:
Line 1 1 Line 2 2 Line 3 3
and this on Firefox's Error console (CtrlShiftJ):
Clicking on the link yields:
Absent true debugging capability, it doesn't get much better than that.
来源:https://stackoverflow.com/questions/9618434/greasemonkey-debugging-get-real-line-numbers