获取2个用户脚本以进行交互? [英] Get 2 userscripts to interact with each other?

查看:119
本文介绍了获取2个用户脚本以进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个脚本.我将它们放在相同的名称空间(@namespace字段)中.

I have two scripts. I put them in the same namespace (the @namespace field).

我希望他们与其他人互动.

I'd like them to interactive with another.

具体来说,我希望 script A RunByDefault设置为123.让 script B 检查是否为RunByDefault==123,然后让 script A 使用超时或其他任何方法来调用脚本B 中的函数.

Specifically I want script A to set RunByDefault to 123. Have script B check if RunByDefault==123 or not and then have script A using a timeout or anything to call a function in script B.

我该怎么做?我不希望合并这些脚本.

How do I do this? I'd hate to merge the scripts.

推荐答案

脚本无法直接彼此交互,并且

The scripts cannot directly interact with each other and // @namespace is just to resolve script name conflicts. (That is, you can have 2 different scripts named "Link Remover", only if they have different namespaces.)

单独的脚本可以使用以下命令交换信息:

Separate scripts can swap information using:

  • Cookies-仅在同域中工作
  • localStorage-仅在同域中工作
  • 通过AJAX将值发送和接收到您控制的服务器-跨域工作.
  • Cookies -- works same-domain only
  • localStorage -- works same-domain only
  • Sending and receiving values via AJAX to a server that you control -- works cross-domain.

就是这样.

同一脚本的正在运行的不同实例,可以使用GM_setValue()GM_getValue()交换信息.此技术的优点是跨域,简单且对目标网页不可见.

Different running instances, of the same script, can swap information using GM_setValue() and GM_getValue(). This technique has the advantage of being cross-domain, easy, and invisible to the target web page(s).

请参见 此坦帕蒙基跨表通讯的工作示例 .

在Chrome(只有Chrome)上,您可能可以使用非标准的

On Chrome, and only Chrome, you might be able to use the non-standard FileSystem API to store data on a local file. But this would probably require the user to click for every transaction -- if it worked at all.

另一个选择是编写一个扩展名(附加组件)以用作帮助程序并执行文件IO.通常,您可以通过postMessage与之互动.

Another option is to write an extension (add-on) to act as a helper and do the file IO. You would interact with it via postMessage, usually.

在实践中,我从未遇到过这样的情况,即合并真正需要共享数据的任何脚本不是更容易,更干净.

此外,脚本无法共享代码,但是它们可以将JS注入目标页面中,并且两者都可以访问该页面.

Also, scripts cannot share code, but they can inject JS into the target page and both access that.

最后,AFAICT脚本始终按顺序运行,而不是并行运行.但是您可以从管理用户脚本面板

Finally, AFAICT, scripts always run sequentially, not in parallel. But you can control the execution order from the Manage User Scripts panel

这篇关于获取2个用户脚本以进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆