Electronの脆弱性でアレをtrue設定はNGですよね。

2020.11.23

Logging

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>

タグ

600, 800, BrowserWindow, const, createWindow, Electron, false, function, Height, mainWindow, new, NG, nodeIntegration, off, OK, path, require, true, webPreferences, Width, アレ, エラー, エレクトロン, これ, ため, バージョン, もの, リグ, 下記, 不正解, , , 使用, 公式, 場合, 対策, 感じ, , 書き方, 結果, 脆弱性, 設定, 駄目,