0

I'm making a game client and I have it where you can launch the game from the browser like 'mygame://whatever'. I'm doing some debugging and I want it to show 'Received this data: whatever'. It does launch the app but it just shows undefined in the data box. How do I fix this?

main.js:

const {app, BrowserWindow} = require('electron');

let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({
    width: 800, 
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })
  mainWindow.loadFile('index.html');
}

app.on('ready', createWindow);

var link;

app.on('open-url', function (event, data) {
  event.preventDefault();
  link = data;
});

app.setAsDefaultProtocolClient('mygame');

// Export so you can access it from the renderer thread
module.exports.getLink = () => link;

index.html:

<!DOCTYPE html>
<html>
  <body>
    <p>Received this data <input id="data"></p>
    <script>
      const {getLink} = require('electron').remote.require('./main.js');
      document.querySelector('#data').value = getLink();
    </script>
  </body>
</html>

package.json:

{
  "name": "mygame",
  "version": "1.0.0-dev",
  "description": "Development copy.",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "dist": "electron-builder"
  },
  "author": "Me",
  "build": {
    "protocols": {
      "name": "mygame-protocol",
      "schemes": [
        "mygame"
      ]
    }
  },
  "devDependencies": {
    "electron": "^6.0.9",
    "electron-builder": "^21.2.0"
  }
}

All help is appreciated!

SirValex
  • 11
  • 1

1 Answers1

1

this code work only for iOS

app.on('open-url', function (event, data) {
    event.preventDefault();
    link = data;
});

In win32 you must use that code

process.argv[1]

so you can add that code in createWindow() like that:

function createWindow () {
    // Crea la finestra del browser
    let win = new BrowserWindow({
        width: 800,
        height: 600,
    });

    //other stuff

    link = process.argv[1];
}

Resources that might be useful

giovybus
  • 349
  • 1
  • 3
  • 10