git clone https://github.com/electron/electron-quick-start-typescript
cd electron-quick-start-typescript
yarn
yarn dev
nodeIntegrationを有効にする(セキュリティ的に良くないけど通信にしないので良いとする)
main.ts
抜粋...
const mainWindow = new BrowserWindow({
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
nodeIntegration: true
},
width: 800,
});
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World!</title>
</head>
<body>
<input id="csvLoad" type="button" value="読み込み"/>
<script> var exports = {}; </script>
<script src="./dist/renderer.js"></script>
</body>
</html>
buttonのイベントをトリガーにファイルを読み込みログに出す
renderer.ts
import * as fs from 'fs';
const button = document.getElementById('csvLoad')
if (button !== null) {
button.onclick = () => {
a();
}
}
const a = () => {
fs.readFile('text file path', (error, data) => {
console.log(data.toString())
})
}