Javascript 用“原点”阻止帧;文件://“;从访问交叉原点帧开始
在将electron从4.1.4升级到5.0.0之后,我得到了这个错误 阻止原点为“file://”的帧访问跨原点帧。在htmliframelement.preload(renderer.js:31:78)Javascript 用“原点”阻止帧;文件://“;从访问交叉原点帧开始,javascript,electron,Javascript,Electron,在将electron从4.1.4升级到5.0.0之后,我得到了这个错误 阻止原点为“file://”的帧访问跨原点帧。在htmliframelement.preload(renderer.js:31:78) 我添加了新浏览器窗口({webPreferences}),如图所示,但此错误仍然存在 这是我的index.html <html> <head> <meta charset="UTF-8"> <link rel="shortcut ic
我添加了
新浏览器窗口({webPreferences})
,如图所示,但此错误仍然存在
这是我的index.html
<html>
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
</head>
<body>
<iframe data-bind="visible: showIframe, attr:{src:appUrl}" allow="autoplay; geolocation; microphone; camera" allowfullscreen></iframe>
</body>
<script>
require('./renderer.js');
</script>
</html>
这是我的renderer.js
(function () {
const {
ipcRenderer,
shell
} = require('electron');
const {
appConf
} = require('./config.json');
const checkInternetConnected = require('check-internet-connected');
/*
* For screenshare
*/
var appFrame = document.getElementsByTagName('iframe')[0];
function preload() {
document.getElementsByTagName('iframe')[0].contentWindow.desktopCapturer = require('electron').desktopCapturer;
document.getElementsByTagName('iframe')[0].contentWindow.electronOpenUrl = openUrlElectron;
document.getElementsByTagName('iframe')[0].contentWindow.deviceType = 'win';
}
appFrame.addEventListener('load', preload);
function sendToIFrame(type, data) {
appFrame.contentWindow.postMessage({
type: type,
data: data
}, "*");
}
function openUrlElectron(url) {
shell.openExternal(url);
}
// codes...
// codes...
// codes...
})();
该应用程序现在可以正常工作,但我知道我的desktopCapturer无法工作。我认为contentWindow脚本提升导致了这个问题或我不知道的问题。我以前遇到过这个错误,我通过设置
节点集成来修复它,这允许处理跨源请求
这样做的一个副作用是,将不再工作,但我找到了一种解决方法,使用预加载脚本(如果您希望我详细说明,请告诉我)。在Chrome 67默认启用站点隔离安全功能后,这是一个已知的问题,它会反映在任何使用Chrome版本的框架中,包括它(例如Electron 5+)
使用--disable web security进行调试时,可能还需要禁用站点隔离(使用--disable features=IsolateOrigins,Site per process)来访问跨源帧
这里有一些关于它的开放性问题
在Electron 5+中,在解决此问题之前,您可以在应用程序“就绪”事件之前添加此行
app.commandLine.appendSwitch('disable-site-isolation-trials');
“我添加了新浏览器窗口({webPreferences})
”这取决于你在webPreferences
中放了什么。你在webPreferences
对象中设置了webSecurity:false
吗?设置webSecurity:false
但是错误仍然存在是的,我知道有一种方法可以使用预加载脚本。如果我使用预加载脚本,我不能使用iframe。嗨,我不知道你说iframe是什么意思,你能详细说明一下吗?这解决了问题,但使PDF查看器无法正常工作。
app.commandLine.appendSwitch('disable-site-isolation-trials');