module.export 内联 html

module.export in inline html

当我尝试在我的 HTML 文件中导出一个变量并在我的 main.js 文件中导入时,它抛出了一个错误: SyntaxError: Unexpected token '<' 指的是我的 index.html 开头的 <!DOCTYPE html> 我不知道为什么 require 不抓取带有模块标签的脚本? 我只想让变量出现在 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)

那绝对行不通:node 和 electron 的 require 实现需要一个 JS 文件,无论是来自主进程还是渲染器进程。无法提取 HTML 文件中 <script> 标签的内容。

看起来您可能正在尝试将一些数据从您的渲染器传送到您的主进程:Electron 提供 IPC system 为此目的。