Search code examples
javascriptnode.jsnode-webkit

node-webkit Right Click a Link and Open in Default Browser


I have an iframe in my node-webkit application. I'd like the user to be able to right click links ( tags) in the iframe and be able to select an "Open in Browser" option to open the link using their system's default browser.

Is this possible?


Solution

  • Yes, it is possible and below is the code for each of the files.

    You can download the entire file here

    INDEX.HTML

    <iframe src="iframe.html" frameborder="2" height="200px" width="100%" ></iframe>
    

    IFRAME.HTML

    <body style="background:#cecece;">
    
        <h2>This is an iFrame</h2>
    
        <a href="#" id="link">Right click here</a>
    
        <script>
    
            // Load native UI library
            var nw = require('nw.gui');
    
            // Create an empty menu
            var menu = new nw.Menu(); 
    
            // Add an item with label
            menu.append(new nw.MenuItem({ 
                label: 'open in browser',
                click: function(e) {
                    nw.Shell.openExternal('http://google.com'); 
                }
            }));
    
            // Listen for a right click on link
            document.getElementById('link').addEventListener('contextmenu', function(e) {
                e.preventDefault();
                menu.popup(e.x, e.y);
            });
    
        </script>
    
    </body>
    

    PACKAGE.JSON

    {
      "name": "NW APP",
      "main": "index.html",
      "description": "Contextmenu from within an iframe"
    }