iframe - 使用 Electron 在 iframe 中加载外部 URL 时出错

标签 iframe electron

我正在尝试使用 Electron 创建一个桌面应用程序,但我无法加载像 google.com 这样的外部 URL在 iframe .
下面的代码,在 index.html 内, 触发错误。

   <!DOCTYPE html>
  <html>
    <head>
      <meta charset="UTF-8">
      <title>Hello World!</title>
    </head>
    <body>
      <h1>Hello World!</h1>
      <!-- All of the Node.js APIs are available in this renderer process. -->
      <iframe src="http://www.w3schools.com"></iframe>


      <script>
        // You can also require other files to run in this process
        require('./renderer.js')
      </script>
    </body>
  </html>
错误 :
  index.html:1 Refused to display 'https://www.w3schools.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
  www.w3schools.com/ Failed to load resource: net::ERR_BLOCKED_BY_RESPONSE
是什么导致了这个问题,我该如何解决?

最佳答案

添加到 Sjoerd Dal 已经回答的内容。

  • 使用 IFRAME 添加外部 URL:网站阻止将其网页添加到任何其他网页,以避免点击劫持。这通常通过以下方式完成:
    一种。在 header 中添加响应。这会阻止未列入白名单/非同源的页面包含在 iframe 中
    湾。检查顶部窗口是否与当前窗口相同。
  • 现在回答您的问题,实际上有一个非常简单的方法可以做到这一点:

  • const urls = [
        "https://www.google.com"
    ]
    
    const createWindow = () =>{
        win = new BrowserWindow({
            center: true,
            resizable: true,
            webPreferences:{
                nodeIntegration: false,
                show: false
            }
        });
        win.maximize();
        win.webContents.openDevTools();
        //win.webContents.
        console.log(urls[0]);
    
        win.loadURL(urls[0]);
        // win.loadURL(url.format({
        //     pathname: path.join(__dirname,"index.html"),
        //     protocol: 'file',
        //     slashes: true
        // }));
        win.once('ready-to-show',()=>{
            win.show()
        });
    
        win.on('closed',()=>{
            win = null;
        });
    }
    
    app.on('ready', createWindow);

    关于iframe - 使用 Electron 在 iframe 中加载外部 URL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668767/

    相关文章:

    electron - 如何将Electron crashReporter minidump文件上传到远程服务器并在本地存储?

    javascript - Electron JS子进程没有被杀死

    reactjs - Electron 可移植构建未在本地保存内容

    javascript - Electron 应用程序移动构建文件夹时出错

    javascript - 在弹出窗口内从 iframe 向后台脚本发送消息

    javascript - 访问 iframe

    javascript - Thickbox 或类似窗口高度为 100% 的东西

    javascript - 如何在 iframe 中强制 xml-outlined/pretty 打印?

    JQuery 可在 iframe 外部拖动。可放置在 iframe 内

    javascript - 未检测到 Electron 和 vuejs 更改