该问题是针对先前的GM版本的。问题可能出现在用户脚本可以运行的不同范围周围,如这里所述。然而,正如这里所述,对于Greasemonkey 4.0,此功能目前未经记录。
我有这个Greasemonkey演示脚本:
// ==UserScript==
// @name GM_getValue, GM_setValue don't work demo
// @version 0.2
// @author You
// @include /^https:\/\/stackoverflow.com/$/
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-end
// ==/UserScript==
console.log('script started');
var id = GM_getValue('testName', 0);
console.log('got ' + id);
id++;
GM_setValue('testName', id);
将其调用为https://stackoverflow.com/,可以清楚地看到它被调用。
然而,在控制台上我得到了这个错误:
Script error:
ReferenceError: GM_getValue is not defined
Stack trace:
userScript@user-script:demosrv/GM_getValue%2C%20GM_setValue%20don%27t%20work%20demo:372:5
scopeWrapper@user-script:demosrv/GM_getValue%2C%20GM_setValue%20don%27t%20work%20demo:381:9
@user-script:demosrv/GM_getValue%2C%20GM_setValue%20don%27t%20work%20demo:361:17
我已经查了很多文档,但似乎GM_{get,set}Value
根本不存在。
为什么会这样?如何使其工作?
我正在使用Firefox浏览器。