当我尝试在HTML文件中导出变量并在main.js文件中导入时,它会引发错误:
在我的index.html开头引用到SyntaxError: Unexpected token '<'
的<!DOCTYPE html>
,我不知道为什么require
不使用module标签抓取脚本吗?
我只希望变量显示在main.js中
我用global.foo
尝试了一下,但是那也不起作用
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Minecraft Launcher</title>
<link href="https://fonts.googleapis.com/css2?family=Open Sans" rel="stylesheet">
<style>
body {
background-color: rgb(0, 0, 0);
background-position: center;
color: white;
font-family: 'Open Sans', sans-serif;
}
</style>
</head>
<body>
<input type="button" value="Try me" id="startbutton">
<input type="text" name="input" id="input">
<script>
require('./renderer.js')
</script>
<script type="module">
module.exports.foo = 5;
</script>
</body>
</html>
main.js:const { app, BrowserWindow } = require('electron')
// Variables
let startbutton;
app.on('ready', () => {
createWindow();
})
function startvariables() {
console.log("jup")
startbutton = window.document.getElementById("input")
console.log(startbutton.value)
}
function createWindow() {
var mainWindow = new BrowserWindow({
width: 950,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadFile('./index.html')
mainWindow.webContents.openDevTools()
}
setInterval(() => {
var foo1 = require('./index.html')
console.log(foo1)
}, 200)
最佳答案
那绝对是行不通的:require
的 Node 和 Electron 实现都希望有一个JS文件,无论是来自主进程还是呈示器进程。无法提取HTML文件中<script>
标记的内容。
看来您可能正在尝试将一些数据从渲染器传递到主过程: Electron 为此提供IPC system。
关于javascript - 内联html中的module.export,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62899288/