Can I load a website with nw.js
Asked Answered
C

3

6

I know nw.js is generally meant to load html on the client, but can it also point to a website and show that? When I set the "main" value in package.json to something like google.com it doesn't work.

I'm looking at making a game launcher app that displays a website for my game and has a logon ability as well from the launcher and then have it shell out to the game exe. Because of this I'd like to have parts of the nw.js actually connect to the web and show a website.

Clothe answered 29/1, 2016 at 15:48 Comment(3)
Can you clarify: "have it shell out to the game exe"? Is it true that your game is fully-browser based, and loaded entirely from a remote location? Is the login page local or remote?Ivo
No, the game is an executable on the clients machine. The idea is to load the login page which would be online and when the response comes back that the login was successful and I get a value back, I launch the exe locally (which I can do with nw.js). So I'd be taking advantage of the https protocol the website would have for the id/pw, but still being able to launch the app locally because it's all being ran in nw.js vs a normal browser.Clothe
Try prepending the link with "http://" or "https://".Crowe
C
2

Of course you can (use JavaScript):

Leave the main value to index.html "main": "index.html", then in your index.html (you can also use this on other html pages)

<script>
        // similar behavior as an HTTP redirect
        window.location.replace("http://google.com");
</script>

or

<script>
        // similar behavior as clicking on a link
        window.location.href = "http://google.com";
</script>

Note: as you are loading resources from the web, rendering the website might be noticeable comparing with loading resources from local which is faster.

Cockatiel answered 29/6, 2016 at 1:41 Comment(0)
T
0

You can use Chrome's <webview> tag.

Dummy example:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            html, body, webview { width: 100%; height: 100%; }
            h1 { text-align: center; }
        </style>
    </head>
    <body>
        <h1>Please log in:</h1>
        <webview id="login-view" src="http://example.com/"></webview>
    </body>
</html>

Communication between the "host" (local) page and the "guest" (external) page can be achieved using ContentWindow and WebRequestEventInterface properties.

Tonsillotomy answered 5/7, 2016 at 17:28 Comment(1)
This sounds promising. The idea I had in mind was that the login would return a "session id" that is also stored with this user in the database on the server side. When nw.js gets this session id value it would shell out to the game executable passing this session id as a parameter. The game would then connect to the server and send this session id and username and the server would validate those 2 match and if they do make the connection to the client. So this means after login page comes back and returns the session id, how would I get that ID value from the host nw.js javascript?Clothe
I
0

I would:

  • Make a local login page
  • Have it post login data (user/pass, token, you choose) to your https endpoint, maybe through ajax. Some directions here
  • Read back login status info, use them locally to do some matching logic. You can even pass them to your exe, depending on how you launch it, so that the game itself can verify the info a second time

A less elegant solution (at least, for me) would be to do some iframe/webview juggling. Something similar to how people managed Facebook login flow in Cordova apps:

  • Open the remote login page in a webview/iframe
  • Listen for url change on the iframe
  • Someway, detecting if the session was started or not, by looking at the url of the iframe after the redirection. Facebook did so by redirecting you to a page of choice, or a standard Facebook page, and appending to that url a token, which you could finally use for subsequent calls.

It's pretty general, but i don't know if you can write some custom logic on the remote login page; for example, make it return session data in a JSON instead of a cookie.

Ivo answered 6/7, 2016 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.