Private messaging system #1606

Open
Spamme1 opened this Issue Aug 19, 2012 · 2 comments

Comments

Projects
None yet
3 participants

Spamme1 commented Aug 19, 2012

I have the same script running in two different domains, it's a page with an iframe to another domain. To communicate between the two instances of the script (between the two domains) I use the window post message method, but this method isn't really stealth. If somebody want to detect the script it would be enough to add an event listener to the message event of the window.
It would be nice to have a private messaging system in GM, where you can send private messages to other scripts and they would be undetectable from the document.

Contributor

cletusc commented Sep 20, 2012

You can already solve this using GM_setValue/GM_setValue, at least for communication among instances of the same script. Here is a very crude way of doing it, note that this is untested but it should get the idea across:

function addMessage(options) {
    var messages = JSON.parse(GM_getValue('messages', '[]'));
    messages.push(options);
    GM_setValue('messages', JSON.stringify(messages));
}
function getMessages() {
    var messages = JSON.parse(GM_getValue('messages', '[]'));
    return messages;
}

// Pass message
addMessage({
    // Your message details here
    domain: location.href,
    text: 'hello world'
});

// Get messages
var messages = getMessages();
for (var i = 0; i < messages.length; i++) {
    var message = messages[i];
    // Test for stuff here
    if (message.domain.indexOf('example.com')) {
        // Do stuff here
    }
}

Something like GM_addValueChangeListener() would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment