我正在尝试使用 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 已经回答的内容。
一种。在 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/