Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Enable a way for browser games to access the username of current player

A topic by Gerhard Wonner created Dec 17, 2019 Views: 1,106 Replies: 8
Viewing posts 1 to 9
(1 edit) (+2)

I am constantly developing a score-based browser game which is hosted on itch.io. Therefore it would be nice to have a leaderboards. But it would be tedious to ask the players to enter their names if they are already logged in itch.io.

Thus it would be great if you could somehow pass the username into the iframe where the game is running. Maybe you could use Window.postMessage() to achieve this.
Thank you! :-)

Admin(+1)

A good idea indeed. We want to add something like this but we’re not sure when it will happen yet. Sorry. In the meantime you’ll have to ask the player for their name directly.

(5 edits) (+1)

Thank you for taking it into account! :-)

You should be done with the following two lines of code:

let frame = document.getElementById('your-iframe-id');
frame.contentWindow.postMessage( 
    /*JavaScript-variable containing the username as string*/, 
    /*itch-io-domain-address*/);

This way the current username is just always thrown via postMessage() into the iframe. The programmer of the game is then free to catch or ignore it. If he wants to catch it, he could do this with the following code:

window.addEventListener('message', event => {
    // IMPORTANT: check the origin of the data!
    if (event.origin.startsWith(/*itch-io-domain-address*/)) {
        // good case:        
        // The data was sent from itch.io.
        // Data sent with postMessage is stored in event.data:
        console.log(event.data); // Would print the username to the console.
    } else {
        // bad case:
        // The data was NOT sent itch.io!
        // Be careful! Do not use it. This else branch is here
        // just for clarity, you usually shouldn't need it.
        return;
    } 
});

I have stolen this idea and the source-code from here. ;-)

Admin(+5)

We would not approach it like this. We would require the user to opt into sharing their information with the game. We would expose it to the game developer as an itch.io JavaScript API where they can make an async request to get information about the current user.

Thanks

(1 edit) (+1)

Of course you have to wait until the iframe is loaded before sending the message. Otherwise the games javascript-code would not be ready to catch it. E.g. you could add an event-listener to your iframe, so your code would look like:

let frame = document.getElementById('your-iframe-id');
frame.addEventListener("load", function () {
    frame.contentWindow.postMessage( 
        /*JavaScript-variable containing the username as string*/, 
        /*itch-io-domain-address*/);
});

Howdy, did this ever come to pass? I'm looking around to find how to do leaderboards for my html5 games

(+1)

I’ve seen a few posts related to this topic but looks like there are still no options for HTML builds (only on the itch app). Any progress on this front? Any plans in the future? What can we expect?

years later, seems like this is still sadly not possible

Yes, this feature would be really nice to have.