Electron(エレクトロン)でrequire(りくわいあ)というものを使用するとエラーになります。Electronの昔のバージョンはこれが使用できたんだって今はこれを脆弱性対策のため、OFF(false)にしている。その設定をtrueにするとOK何だけど、これは公式では認めてない不正解の書き方だとさ。
function createWindow() {
mainWindow = new BrowserWindow({ width: 800, height: 600 , webPreferences: {
nodeIntegration: true
}});
じゃどうするれば良いのか?調べた結果、これが良いみたいです?。下記の書き方はちょっと面倒くさいけれども、こう書かなくては駄目だとさ。requireを使用しない場合はこんな感じで書かなくても良いです。
const path = require('path');
function createWindow() {
mainWindow = new BrowserWindow({ width: 800, height: 600 , webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, "preload.js")
}});
const { contextBridge, ipcRenderer} = require("electron");
const request = require('request');//使ってないけど?
contextBridge.exposeInMainWorld(
"hoge_hoge", {
send: (data) => {
consloe.log(data);
document.getElementById("hoge").innerHtml = "Hey!! " + data;
ipcRenderer.send("Hey!! " + data);
},
receive: (data) => {
consloe.log(data);
//ipcRenderer.on(channel, (event, ...args) => func(...args));
}
}
);
<button id="btn">Hey!!</button>
<span id="hoge"></span>
<script>
document.getElementById("btn").addEventListener("click",(e)=>{
window.hoge_hoge.send("hogeO!!");
});
</script>