所以在Electron App中,我试图打开一个URL,然后访问window.external但是我收到了错误:
使用原点"文件阻止了一个框架://"从访问跨源框架
如果我在Electron应用程序中添加external.html并使index.html打开本地应用程序,则会按预期调用外部窗口。
你会看到我在网上找到了webSecurity:false和allow-file-access,但都没有帮助。
如果有人知道如何绕过跨原始框架,我将非常感激。
文件夹结构:
Electron
|--main.js
|--index.html
Local WebServer
|-external.html
main.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);
的index.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>
external.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>