如何使 GM_getValue 存在于 Firefox 上的 Greasemonkey 中?

How to make GM_getValue existent in Greasemonkey on Firefox?

复制的候选对象是以前的 GM 版本。问题可能出现在用户脚本可以 运行 的不同范围内,如 here. However, as described here 所述,此功能当前未记录在 Greasemonkey 4.0 中。

我有这个 Greasemonkey 演示脚本:

// ==UserScript==
// @name         GM_getValue, GM_setValue don't work demo
// @version      0.2
// @author       You
// @include      /^https:\/\/whosebug.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://whosebug.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。

GM_getValueGM_setValue 现在在 GreaseMonkey 中已过时。正确的方法是 GM.setValueGM.getValue.

GreaseMonkey 文档经常使用旧的 API 调用名称,这是其中的一个连续错误。可能没有正确更新。

正如文档 here 所说,GM 函数可以 运行 在不同的范围内。不幸的是,直到现在我没有找到任何信息,哪些范围是现有的以及我们如何在它们之间切换。

网络上很多地方使用GM_getValue的旧参考资料都已过时。

重要的事情:

  • 虽然 GM_getValue 是具有 return 值的函数,但 GM.getValueGM.setValue return Promises.
  • 使用 await 调用,您几乎可以像在旧的、不错的版本中那样使用它们。

远程 link 上的这个示例有效:

// ==UserScript==
// @name        Greasemonkey set-and-get Example
// @description Stores and logs a counter of executions.
// @grant       GM.setValue
// @grant       GM.getValue
// ==/UserScript==

(async () => {
  let count_before = await GM.getValue('count', 0);

  // Note awaiting the set -- required so the next get sees this set.
  await GM.setValue('count', count_before + 1);

  // Get the value again, just to demonstrate order-of-operations.
  let count_after = await GM.getValue('count');

  console.log('Greasemonkey set-and-get Example has run', count_after, 'times');
})();

但是,仍然没有关于作用域以及我们如何与它们交互的更清晰的文档。

看来,至少有两个作用域:

  • 在其中之一中,您可以操作 DOM,但无法访问 GM.* API。
  • 在另一种情况下,您可以访问 GM.* API(因此,您可以进行脚本本地持久存储),但无法访问 DOM .
  • 作用域之间,我们可以通过异步调用进行通信。
  • 为什么这个麻烦会增加 安全性,可能连 GM 开发人员都说不出来。

因此,为 4.0 开发新的 GreaseMonkey 脚本的方法是从他们的示例脚本开始,然后遵循增量试验和错误路径。


扩展:我发现的另一个麻烦:脚本中 await 的存在似乎让 greasemonkey 忽略了整个脚本等。在示例中,它似乎没有发生,但在更复杂的脚本中,它确实发生了。我没有深入调试它——我只是忽略 await 并以旧方式使用 Promises (GM.getValue("myValue").then(function(myValue) { ... });)。它使代码更糟糕,但也是如此。