【问题标题】:How to pass information between 2 tabs running same GreaseMonkey userscript?如何在运行相同 GreaseMonkey 用户脚本的 2 个选项卡之间传递信息?
【发布时间】:2018-03-28 17:28:15
【问题描述】:

我编写了一个 GreaseMonkey (javascript) 用户脚本,它在浏览器 (FireFox) 选项卡中运行,指向 www.site1.com/stuff。另一个选项卡指向www.site1.com(没有stuff),并且不是第一个选项卡的子窗口(例如,不是通过在第一个选项卡上运行的用户脚本打开的)。用户脚本(独立?)在两个选项卡上运行。

我希望第一个浏览器选项卡上的用户脚本执行将字符串变量传递给第二个浏览器选项卡。虽然GM_setValueGM_getValue 在单个用户脚本中的存储/检索工作得很好,但该存储区域似乎无法被用户脚本的其他执行访问。 localStorage 遭受同样的失败。举个明确的例子:

  • 当用户脚本检测到它在www.site1.com/stuff 上运行时,它会将一个值放入存储中:GM_setValue('parValue', 'aaabbbccc');

  • 在第一个选项卡完全加载并有足够的时间将此值放入存储空间后,手动打开第二个选项卡。当用户脚本检测到第二个选项卡在www.site1.com(没有stuff)上运行时,代码会尝试检索值:var parVal = GM_getValue('parValue')。在我的用户脚本中,parVal 将具有 null 值;每个用户脚本执行似乎使用不同的存储区域。

在以下限制条件下,我如何实现这个看似简单的任务,即让同一用户脚本的两次执行都安全/从公共存储区域中检索:
* 第一个选项卡的 URL 末尾的 stuff 可以由用户随意更改(为每个可能的 stuff 可能性编写单独的用户脚本是不可能的)。
*选项卡永远不会有父/子关系,因为它们是独立生成的(从技术上讲,第二个选项卡是第一个选项卡的孙子,但我不知道这两个选项卡的窗口名称是什么或如何引用它们在代码中)。
* 使用在 GreaseMonkey 用户脚本中运行的 javascript

是否有某种可以使用的全局跨表存储区域,可以在 GreaseMonkey 用户脚本中实现?理论上,GM_setValue 应该适用于这种情况吗?我花了大量时间研究以下相关 SO 问题的答案,但无法找到适用于上述条件和/或可以实施到 GreaseMonkey 用户脚本中的解决方案: Communication between tabs or windows, Javascript: sharing data between tabs, https://superuser.com/questions/1005448/can-a-greasemonkey-script-know-whats-been-loaded-into-another-tab, Sending a message to all open windows/tabs using JavaScript,

【问题讨论】:

  • that storage area does not seem to be accessible to the other execution of the userscript - 你错了...... GM_setValue 存储每个脚本的值,不管 URL - 相同的脚本,相同的存储
  • localStoragewindow 上工作并提供message 事件,这是一个出色的“IPC”解决方案
  • 谢谢@JaromandaX。我只是没有在我的代码中看到预期的行为。我将制作一个简化的用户脚本,仅测试此功能并在此处发布。我很可能错误地使用了GM_setValueGM_getValue
  • 谢谢@dandavis。对于我的特定应用程序,不需要消息事件(我不认为),但感谢您的评论,因为我想知道 localStorageGM_getValue/setValue 有何不同。谁知道呢,当我深入研究我的代码开发时,也许我会发现我确实需要window 上的message 事件。
  • 令我惊讶的是,我专注于“GM_setValue”功能的精简用户脚本与@JaromandaX 描述的完全一样。 (我没有对localStorage 做过同样的测试)。该测试已添加到上面重新编辑的问题中(以防它帮助其他人)。

标签: javascript local-storage parameter-passing greasemonkey


【解决方案1】:

事实证明,'GM_setValue/getValue' 确实允许在并行运行相同用户脚本的 2 个选项卡之间共享信息。我用下面的测试代码证明了这一点。我从一个指向www.google.com 的选项卡开始,收到警报,然后在同一个浏览器窗口中打开另一个选项卡,并将 URL 指向 www.yahoo.com。警报表明该值已成功从在google.com 上执行的用户脚本放置它的存储中检索到。

// ==UserScript==
// @name        hello world
// @namespace   http://www.sharelatex.com
// @include     https://www.google.com/*
// @include     https://www.yahoo.com/*
// @grant       GM_setValue
// @grant       GM_getValue
// ==/UserScript==

if (window.location.href.indexOf("google.com") != -1) {  // on Google URL 
    alert("on Google site, storing value");
   // you will see the above alert verifying that code understands  
   // the code is running on the google.com tab
    GM_setValue('passValue', 'aabbcc'); 
} else { 
    alert("on Yahoo site, retrieving value");
   // the above alert will be seen, verifying that the code 
   // understands the code is running on the yahoo.com tab
    var pvalue = GM_getValue('passValue'); 
    alert("The retrieved value is " + pvalue);
   // the above alert should show aabbcc, verifying that the 
   // userscript running on google.com successfully stored the value 
  // and the script on yahoo.com successfully retrieved it. 
}

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2020-03-19
    • 2014-06-02
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2017-06-17
    • 2012-02-05
    相关资源
    最近更新 更多