Search code examples
javascriptwindowsscreennode-webkitnode-webkit-agent

nodewebkit - documentation said screen will work, but its getting impossible to make it work


I am using latest nodewebkit and running the following code as per documentation ( https://github.com/rogerwang/node-webkit/wiki/Screen ) but its always failing. Can anyone please tell me why i have this error please and how do you fix it?

<!DOCTYPE html>
<html>
<body>
    <script>
      function ScreenToString(screen) {
        var string = "";
        string += "screen: "+ screen.id;
        return string;
      } 

      var gui = require('nw.gui');
      gui.Screen.Init();
      var string  = "" ; 
      var screens = gui.Screen.screens;

      for(var i=0;i<screens.length; i++) {
        string += ScreenToString(screens[i]);
      }
      document.write(string);
    </script>
</body>
</html>

ERROR:

Uncaught node.js Error 

SyntaxError: Unexpected end of input
    at Object.parse (native)
    at Screen.screens (screen.js:65:15)
    at file:///C:/Users/xxx/Downloads/node-webkit-v0.11.0-pre-win-x64/node-webkit-v0.11.0-pre-win-x64/test.html:14:28

Solution

  • This script moves the window to the center of the other screen (if there is one).

    var gui = require('nw.gui');
    
    // initialize the Screen singleton
    gui.Screen.Init();
    
    // get the current window
    var win = gui.Window.get();
    
    function moveToOtherWindow() {
        for(var i = 0; i < gui.Screen.screens.length; i++) {
            var screen = gui.Screen.screens[i];
            // check if the window is horizontally outside the bounds of this screen
            if (win.x < screen.bounds.x || win.x > screen.bounds.x + screen.bounds.width) {
                // move the window to this screen
                win.x = screen.bounds.x + (screen.bounds.width - win.width) / 2;
                win.y = screen.bounds.y + (screen.bounds.height - win.height) / 2;
                break;
            }
        }
    }
    
    moveToOtherWindow();