【问题标题】:How to test a Greasemonkey script, especially on a local copy of a webpage?如何测试 Greasemonkey 脚本,尤其是在网页的本地副本上?
【发布时间】:2026-02-18 22:15:02
【问题描述】:

我有自己的 javascript,需要用 Greasemonkey 进行测试。我以前从未与 Greasemonkey 合作过;如何测试脚本?

我不是在万维网上测试它,我已经保存了目标页面(Firefox>另存为>网页,完成),所以我在本地测试它。

流程是什么?如何测试脚本?

【问题讨论】:

  • 这对 SO 来说是一个相当广泛的问题。也许您最好阅读 Greasemonkey 文档和教程?

标签: javascript debugging firefox greasemonkey


【解决方案1】:

以下是对 Greasemonkey 脚本进行故障排除的一些指南,包括一般性和网页本地副本。

  1. 要在本地页面(没有本地 Web 服务器)上进行测试,您必须更改 Greasemonkey 的设置。
    打开about:config并将greasemonkey.fileIsGreaseable设置为true

  2. 确保网页的本地副本不在系统的 /tmp/temp 文件夹中。如果是,该脚本将无法可靠运行。

  3. 确保脚本源不在系统的 /tmp/temp 文件夹中。 The script will not install if it is.

  4. 对于处理本地文件的脚本,请确保您有一个针对本地网页副本的适当@include directive。例如:

    // @include file:///D:/web/local%20page%20copies/*
    
  5. 熟悉 Firefox 的错误控制台 (CtrlShiftJ) 和 how it can be used to determine the source of errors in Greasemonkey scripts

  6. 安装Firebug 并熟悉它。 Firebug's excellent console functions 在 GM 脚本中工作得很好——尽管您可能必须在它们前面加上 unsafeWindow.

  7. 首先在 Firebug 的 JavaScript 控制台中尽可能多地测试不使用 GM_ functions 的 javascript。


另请参阅:

【讨论】: