Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing unique string data to opened tabs? #2328

Closed
CrimsonVex opened this issue Nov 7, 2015 · 10 comments
Closed

Passing unique string data to opened tabs? #2328

CrimsonVex opened this issue Nov 7, 2015 · 10 comments
Milestone

Comments

@CrimsonVex
Copy link

Now that GM_openInTab no longer returns the window object, and thus we can't set window variables from the script that opens the window, how can one pass unique string data to a tab, designated for just that tab?

@janekptacijarabaci
Copy link
Contributor

See also #2134
(used only for context)

@CrimsonVex
Copy link
Author

That simply confirms that GM_openInTab returns null, but doesn't make reference to my use case.

@the8472
Copy link
Contributor

the8472 commented Nov 13, 2015

one workaround would be to add an anchor to the URL and have a script process that anchor. For larger payloads you could use that anchor as ID and then use broadcast channels to have the tabs talk to each other.

Note that web content can listen/manipulate these kinds of communication channels.

@CrimsonVex
Copy link
Author

I would avoid anchors for that very reason.

@pyhedgehog
Copy link

You can use GM_info.uuid label communication channel with it.
As script UUID unique for script+computer+profile it's safe enough.
PS: Don't forget to // @grant GM_info in metadata block.

@CrimsonVex
Copy link
Author

That sounds like it's for communicating between different scripts, not the same script running on a new page.

@arantius arantius added this to the Pony milestone Jan 20, 2016
@pyhedgehog
Copy link

@Sasstraliss Why do you think "it's for communicating between different scripts"? Script can't get UUID of different script - only UUID of the same.

@DoomTay
Copy link

DoomTay commented May 19, 2016

Here's a trick I learned from reddit. A fair amount of pseudocode as far as variables are concerned, but it might help. Assuming the target window and parent window are on sites the script is for...

var stringData;

var awakener = window.opener || window.parent;
if(awakener && awakener != window)
{
    //Used when a page is launched from a link so that passData can...well...pass data. But we can't use onload from there so instead we're using postMessage here to convey to the "parent" window that this window is ready to recieve data
    awakener.postMessage("Loaded","*");
}

window.addEventListener("message", messageDetected, false);

clickableThing.onclick = function() {
    var newWindow = window.open(this.href,this.target);
    passData(window,newWindow,dataToPass);
    return false;
};

function messageDetected(event)
{
    if(typeof(event.data) == "string" && event.data != "Loaded")
    {
        stringData = event.data;
    }
}

function passData(mainWindow,target,data)
{
    mainWindow.addEventListener("message", passItOn, false);
    function passItOn(event)
    {
        if(event.source == target) target.postMessage(data,"*");
    }
}

@pyhedgehog
Copy link

pyhedgehog commented May 21, 2016

@DoomTay, please use triple backquotes to keep source formatting.

@trespassersW
Copy link

@the8472 said:

use broadcast channels

Alas, this is experimental technology only available in Fx38+. A more appropriate solution would be using localStorage API and storage events. see http://stackoverflow.com/questions/2236828/javascript-communication-between-tabs-windows-with-same-origin/12514384#12514384

@arantius arantius modified the milestones: Pony, Bankruptcy Jul 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants