0
votes

I have develop a simple nw.js (ex node-webkit) application. It open a fullscreen Iframe with attribute

nwfaketop

Inside this iframe ( logically in different domain) I need to call a parent nodejs function. How can I do? Here code of my index.html main nw.js app:

<html>
<body style="margin:0">
<iframe src="https://www.mydomain.it/test.html" style="width:100%;height:100%;border:0;padding:0" nwfaketop></iframe>
<script>
    var gui = require('nw.gui');
    var win = gui.Window.get();
    win.showDevTools();
    var clipboard = gui.Clipboard.get();

    function copy_do(text){
        clipboard.set(text, 'text');
    }

    onload = function() {
        win.maximize();
    }
</script>
</body>
</html>

And here code inside iframe on https://mydomain.it/test.html

<!doctype html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <title>Test page</title>
</head>
<body>
<div style="text-align: center;">Test page</div>
<script>
parent.copy_do("stackoverflow");
</script>
</body>
</html>

As you see I need to call function

copy_do();

from parent main file.

How can I do? Thanks in advance

2
I think it's not allowed, according to security reasons. On web the only way to communicate between iframe and parent window is window.parent.postMessage, I think it should also work in nw.js. blog.teamtreehouse.com/cross-domain-messaging-with-postmessage May be in your case you pass js code what you want to run in a message. - Pavel Evstigneev

2 Answers

2
votes

You can register a callback function from your main application into your iframe's content window. and then call that function from your iframe. example:

in your main application:

function copy_do(text){
    clipboard.set(text, 'text');
}

var iframe = document.getElementsByTagName('iframe')[0];
iframe.onload = function(){
   iframe.contentWindow.myCallback = copy_do;
}

in your iframe script:

window.myCallback('sometext');
0
votes

Try this. The function of onload() can not catch child document's complete status. Please check the document ready state is "complete".

<iframe id="ifrmChild" src="http://yourdomain/iframe.html" style="width:100%;height:100%;border:0;padding:0" nwfaketop></iframe>

<script>

    window.doit = function(text){
        console.log('Hey!!!!!');
    }

    var interval = setInterval(function () {
        var iframe = document.getElementById("ifrmChild");
        var cw = iframe.contentWindow;
        if (cw.document.readyState == "complete")
        {
        clearInterval(interval);
        iframe.contentWindow["doit"] = window.doit;
        iframe.contentWindow.eval("doit('ssss');");

    }
});