Exposing a function for child window to post to.

  • 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: 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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?

  • 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.

  • 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:

    function gameAuthorise(authUrl) {
         window.open(authUrl);
         window.addEventListener('message', function (event) {
         userToken = event.data;
         bRuntime.trigger(cr.plugins_.myplugin_mp.prototype.cnds.authComplete, bInst);
         });
    }
    

    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.

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

    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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)