javascript - 内联html中的module.export

标签 javascript node.js electron export

当我尝试在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/

相关文章:

javascript - 如何在 Node.js 中使用 JSON2

javascript - 在 Node 中遍历 page.evaluate

macos - 为什么在 “about”菜单中显示的应用程序名称与应用程序名称不同? ( Electron ,MacOS)?

javascript - 如何在创建我的元素之前使用 "on click"

javascript - Internet Explorer 焦点在 addEventListener 触发焦点之前?

javascript - Knex - 更新来自选择值的行

javascript - Serverless express 关闭 mongodb 连接

node.js - 字谜数组程序和数组组结果

javascript - 新的 Electron 浏览器窗口从指定的 ui-router 状态开始

node.js - 在哪里可以找到 Node 替换变量的查找列表?