Greasemonkey debugging, get real line numbers

随声附和 提交于 2019-12-23 23:36:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!