【问题标题】:How to access the contents of an iframe in nw.js?如何在 nw.js 中访问 iframe 的内容?
【发布时间】:2016-10-07 02:41:16
【问题描述】:

我需要在 nw.js 中捕获 iframe 的内容。该页面位于不同的域中。我尝试正常访问它(myIframe.document),但我得到一个空的 html 文档(可能是某种形式的跨域安全或其他)。但是,我可以从控制台访问 iframe 的内容,我猜我缺少一些设置,可以让我直接从我的代码中执行相同的操作。但是,我不需要 iframe 就可以访问 nw.js(出于安全原因,如果不可能的话会更好)。

<iframe src="http://google.com" width="800" height="600" id="myIframe"></iframe>
<script>
    $(function()
    {
        // Making sure it's actually loaded with help of jquery
        $('#myIframe').ready(function () {
            console.log(window.frames[0].document);
            // Returns:
            // <html>
            // <head></head>
            // <body></body>
            // </html>
        });
    });
</script>

【问题讨论】:

  • 我想你会在stackoverflow.com/questions/19627591/… 找到一些帮助——它是关于访问 iframe 和操作它的。稍加调整,我相信它会为你工作。
  • @Ewald 我查看了您的代码,结果发现问题出在我对 jquery 的使用上。当 jquery 的 ready 回调被调用时 iframe 并没有真正准备好,但是当 onload 事件被触发时它已经准备好了。
  • 太好了,很高兴您发现了问题,祝您项目顺利。

标签: javascript html iframe nw.js


【解决方案1】:

要获取 URL 的整个文档,您可以在加载后使用此方法:

var Content=(new XMLSerializer()).serializeToString(iframeID.contentDocument);

【讨论】:

    【解决方案2】:

    事实证明我可以像在 nw.js 中那样访问 iframe 的内容,而我只是在原始代码中做错了。

    问题在于 jquery ready 的使用。当我在触发ready 时尝试访问iframe 时,我得到了一个空文档,但是当我从iframe 的onload 回调中执行相同操作时,我实际上得到了所有内容。

    <iframe src="http://google.com" width="800" height="600" id="myIframe" onload="loaded()"></iframe>
    <script>
        function loaded()
        {
            console.log(window.frames[0].document);
            // Returns the actual page
        }
    </script>
    

    PS nw.js 中的iframe 加载不安全内容(例如外部站点)时建议使用nwdisablenwfaketop 属性.

    <iframe src="http://google.com" width="800" height="600" nwdisable nwfaketop></iframe>
    

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 2016-05-03
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多