简体   繁体   中英

module.export in inline html

when i try to export a variable in my HTML file and import in my main.js file it it throws an error: SyntaxError: Unexpected token '<' that refeers to the <!DOCTYPE html> at the start of my index.html and i don't know why require doesn't grab the script with the module tag? I just want the variable to show up in the main.js

I tried it with global.foo but that didn't work either

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)

That will definitely not work: node and electron's implementations of require expect a JS file, whether from the main or a renderer process. It is unable to extract the content of a <script> tag within an HTML file.

It looks like you may be trying to communicate some data from your renderer to your main process: electron offers IPC system for this very purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM