TamperMonkey - message between scripts on different subdomains

I have two scenarios. Each of them works on a different subdomain of our company."Example.com".

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

Same domain, different subdomains.

When a particular item appears on house.example.com, I need to send a message in a script tobob.fred.example.com

Since Google extensions can exchange messages between extensions, there must be a way in which TamperMonkey must exchange messages within the same extension, between scripts, especially if they work in the same second level domain.

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


Update: Although Gothdo refers to the Javascript message between browser tabs / windows as containing an answer to this question, it did not take into account cross-policy issues. None of the answers in this question gives a clear answer for interstitial links to the browser tab, which was the main point of this question. I researched and solved this problem, getting ideas from a number of SO and non-SO sources. If this question is reopened, I will submit my solution.

+4
source share
1 answer

You can use GM_getValue, GM_setValueand GM_addValueChangeListenerto achieve communication between the user script.

script .

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

script.

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));
}

, , , : .

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.

+1

Source: https://habr.com/ru/post/1663569/


All Articles