所以在一个电子应用程序中,我试图打开一个 URL,然后访问它上面的 window.external 但我得到了错误:
阻止具有源“file://”的框架访问跨域框架
如果我在 Electron 应用程序中添加 external.html 并使 index.html 打开本地的,则外部窗口会按预期调用。
你会看到我已经添加了 webSecurity: false 和 allow-file-access ,就像在网上找到的一样,但都没有帮助。
如果有人知道我如何绕过跨域框架,将不胜感激。
文件夹结构:
Electron
|--main.js
|--index.html
Local WebServer
|-external.html
主.js:
function createWindow() {
app.commandLine.appendSwitch('allow-file-access');
lobby = new BrowserWindow(
{
width: 1200,
height: 800,
maximizable: true,
center: true,
webPreferences: {
nativeWindowOpen: true,
webSecurity: false
}
})
}
lobby.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
app.on('ready', createWindow);
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>LOBBY TEST</title>
<script></script>
</head>
<body>
<script>
let win = window.open("http://localhost/external.html");
win.external.SilentLogin = (username, password) => {
//DO STUFF HERE
};
</script>
</body>
</html>
外部.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<H1>Windows Externals Test Page</H1>
</head>
<body align="center">
<FORM>
<div align="center">
<INPUT TYPE="button" VALUE="Silent Login" onClick='window.external.SilentLogin("user1", "test");'></INPUT>
</div>
</FORM>
</body>
</html>