TamperMonkey-不同子域上的脚本之间的消息 [英] TamperMonkey - message between scripts on different subdomains

查看:160
本文介绍了TamperMonkey-不同子域上的脚本之间的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个脚本.每个都在我们公司"Example.com".

I have two scripts. Each runs on a different subdomain of our company "Example.com".

Script #1 -- house.example.com
Script #2 -- bob.fred.example.com

相同的域,不同的子域.

Same domain, different subdomains.

当特定元素出现在house.example.com上时,我需要向运行在bob.fred.example.com上的脚本发送一条消息

When a particular element appears on house.example.com, I need to send a message over to the script running on bob.fred.example.com

由于Google扩展可以在扩展之间交换消息,因此必须使用TamperMonkey在脚本之间在同一扩展中交换消息,尤其是在相同的二级域上运行时.

Since Google extensions can exchange messages between extensions, there must be a way with TamperMonkey to exchange messages within the same extension, between scripts -- especially if they run on the same second-level domain.

有人能指出我正确的方向吗?一个或两个例子是值得的.

Can anyone point me in the right direction? An example or two would be worth their weight in gold.

更新:尽管Gothdo引用了浏览器标签之间的Javascript通信/windows 包含此问题的答案,因此他没有考虑到所涉及的跨域策略. 该参考问题的所有答案均未提供跨域浏览器选项卡通信的明确答案,这是该问题的重点. 我现在已经研究并解决了这个问题问题,从许多SO和非SO来源获得想法.如果这个问题再次出现,我将发布我的解决方案.

Update: Although Gothdo referenced Javascript communication between browser tabs/windows as containing an answer to this question, he failed to take into consideration the cross-origin policies involved. None of the answers in that referenced question provide a clear answer for cross-origin browser tab communications, which was the main point of this question. I have now researched and solved this problem, getting ideas from a number of SO and non-SO sources. If this question is re-opened, I will post my solution.

推荐答案

您可以使用GM_getValueGM_setValue& GM_addValueChangeListener实现跨表用户脚本的通信.

You can use GM_getValue, GM_setValue & GM_addValueChangeListener to achieve cross-tab user script communication.

在用户脚本标题中添加以下行.

Add the following lines in your user script header.

// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addValueChangeListener

以下几行粗代码将简化跨表用户脚本的通信.

The following lines of rough code will simplify the cross-tab user script communication.

function GM_onMessage(label, callback) {
  GM_addValueChangeListener(label, function() {
    callback.apply(undefined, arguments[2]);
  });
}

function GM_sendMessage(label) {
  GM_setValue(label, Array.from(arguments).slice(1));
}

因此,您要做的就是发送和接收消息.

So all you'll need to do is the following to send and receive messages.

GM_onMessage('_.unique.name.greetings', function(src, message) {
  console.log('[onMessage]', src, '=>', message);
});
GM_sendMessage('_.unique.name.greetings', 'hello', window.location.href);

注意如果发送的消息与以前相同,则发送消息可能不会触发您的回叫.这是由于GM_addValueChangeListener未触发,因为该值未更改,即即使调用GM_setValue,该值也与以前相同.

NOTE Sending messages may not trigger your callback if the message sent is the same as before. This is due to GM_addValueChangeListener not firing because the value has not changed, i.e. same value as before even though GM_setValue is called.

这篇关于TamperMonkey-不同子域上的脚本之间的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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