在 Electron 中具有本地资源的 iframe [英] iframe with local resource in Electron

查看:20
本文介绍了在 Electron 中具有本地资源的 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 Electron 应用程序中渲染 iframe:

I need to render iframe in my Electron application:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <iframe sandbox='allow-scripts' src='frm.html'></iframe>
  </body>
</html>

frm.html 链接本地文件脚本 foo.js,它是我的 Electron 应用程序的一部分

where the frm.html links the local file script foo.js which is part of my Electron application

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
    <script src="foo.js"></script>
</head>
<body>
    <p>Inside iframe</p>
</body>
</html>

当我在 Electron 中运行应用程序时,我可以在 devtools 控制台中看到此错误

When I run the application in Electron I can see this error in devtools console

Not allowed to load local resource: file:///C:/electron/app1/foo.js

在 Electron 中是否可能出现这种情况?

Is it possible such scenario in Electron?

推荐答案

这是 iframe 的安全功能.这是一个类似的问题,讨论将链接文件加载到 iframe 中:Displaying local htm file iniframe?.

This is a security feature of the iframe. Here is a similar question that talks about loading linked files into an iframe: Displaying local htm file in iframe?.

话虽如此,您是否考虑过改用 webview 标签?http://electron.atom.io/docs/v0.30.0/api/web-view-tag/.webview 标签与 iframe 非常相似,但让您能够更好地控制其周围的安全性.我测试了将本地文件加载到 webview 中的方式,就像您尝试将 frm.html 加载到 iframe 中一样,它可以完美地工作.

That being said, have you considered using the webview tag instead? http://electron.atom.io/docs/v0.30.0/api/web-view-tag/. The webview tag is very similar to an iframe, but gives you more ability to control the security around it. I tested loading a local file into a webview in the same way you attempt to load frm.html into the iframe and it works flawlessly.

这篇关于在 Electron 中具有本地资源的 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆