iframe with local resource in Electron

2019-05-13 17:07发布

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>

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>

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

Is it possible such scenario in Electron?

1条回答
何必那么认真
2楼-- · 2019-05-13 17:37

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?.

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.

查看更多
登录 后发表回答