Exposing a function for child window to post to.

For developers using the Construct 2 Javascript SDK

Post » Fri Aug 24, 2012 3:35 pm

@Ashley I thought I would start a thread here for my questions about getting data from child windows. The original thread is here for anyone interested: http://www.scirra.com/forum/pass-a-value-from-a-child-browser-to-game_topic55919.html

So far I have got a function to open a url in a new window using window.open and a string param in edittime.js for the user to set the url to open. This works fine.

What I am pondering though is how to go about exposing the function that the child window posts to:

function getToken(authToken) {
     userToken = authToken;
}

this basically sets a variable called authToken which is essentially my plugin's returned expression.

I am a javascript noob but so far I am swimming. I am just having trouble figuring out how to expose this function.

Thanks in advance.

M
B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Fri Aug 24, 2012 4:34 pm

Some extra info, I am trying post back from the child browser using:
window.opener.cr.plugins_.MYPLUGIN.prototype.getToken(myVal);

Is this vaguely right?

B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Sat Aug 25, 2012 8:49 pm

Apparently I have been missing a trick, The correct way to do this is with parent.postMessage and an event listener.

I shall report back on my findings.
B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Mon Aug 27, 2012 7:30 am

Just a follow up to this. I have managed to implement cross document messaging which seems to work on mobile using window.postMessage.

My plugin has an action which calls the following function:
[code]
function gameAuthorise(authUrl) {
     window.open(authUrl);
     window.addEventListener('message', function (event) {
     userToken = event.data;
     bRuntime.trigger(cr.plugins_.myplugin_mp.prototype.cnds.authComplete, bInst);
     });
}
[/code]

On the server that proxies the oAuth the following code sends the token to my event listener. The token is returned as an expression of my plugin.

[code]
        <div id="mytoken"><? echo htmlentities($_GET['token']) ?></div>
        <script>
            var token = document.getElementById("mytoken")
            window.opener.postMessage(token.innerHTML , '*');
            window.close();
        </script>
[/code]

This code worked for me, obviously you will need to set up the trigger for your own plugin.

I hope this helps anyone who is building custom plugins for accessing 3rd party web services that require oAuth.
B
16
S
2
G
4
Posts: 67
Reputation: 3,295


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 1 guest