gpt4 book ai didi

javascript - Electron 无法执行Java代码以检查Div是否存在

转载 作者:行者123 更新时间:2023-12-03 12:33:41 25 4
gpt4 key购买 nike

我正在尝试检查当前页面 View 中是否存在具有classname的div。我尝试使用javascript查找元素并使用IPC发送结果,但是出现“脚本执行错误失败”,并且我不确定如何解决。

下面是我的代码,它打开一个页面,通过执行javascript检查div是否存在,将渲染器的结果发送回main并根据发送回的内容应用适当的css。 (由于对前端的了解有限,我觉得这是不正确的方法)。

index.js

const { app, BrowserWindow, globalShortcut } = require('electron')
const path = require('path');
const fs = require('fs');
const {ipcMain} = require('electron')

const DOMAIN_URL = "instagram.com"

let win;

function createWindow () {

const options = {
width: 500,
height: 550,
resizable: false,
maximizable: false,
movable: true,
titleBarStyle: 'hiddenInset',
title: 'InstaSend',
webPreferences: {
nodeIntegration: false
},
};

// create main window
win = new BrowserWindow(options)
win.loadURL('https://'+DOMAIN_URL)

// check if we are at the login page
win.webContents.on('dom-ready', function (e) {
win.webContents.executeJavaScript(`
var ipcRenderer = require('electron').ipcRenderer;
var hasLogin = document.getElementsbyClassName("gr27e")[0];
ipcRenderer.send('checkLogin', hasLogin)
`);
});

ipcMain.on('checkLogin', function(event, hasLogin){
console.log(hasLogin);
if(!hasLogin){
win.resizable = true
win.maximizable = true
win.setSize(1100,950, true)
win.webContents.insertCSS(fs.readFileSync(path.join(__dirname, '/assets/dash.css'), 'utf8'));
}else{
win.webContents.insertCSS(fs.readFileSync(path.join(__dirname, '/assets/ig.css'), 'utf8'));
}

})
}

app.on('ready', function(){
createWindow()
});


错误输出:
internal/process/warning.js:27 (node:89911) UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
at WebFrame../lib/renderer/api/web-frame.ts.WebFrame.<computed> [as executeJavaScript] (electron/js2c/renderer_init.js:1590:33)
at electron/js2c/renderer_init.js:2844:43
at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:2419:57)
at EventEmitter.emit (events.js:210:5)
at Object.onMessage (electron/js2c/renderer_init.js:2188:16)
writeOut @ internal/process/warning.js:27
2internal/process/warning.js:27 (node:89911) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
writeOut @ internal/process/warning.js:27
2internal/process/warning.js:27 (node:89911) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

最佳答案

您必须将nodeIntegration设置为true。否则,您将无法使用require

webPreferences: {
nodeIntegration: true
},

此外,您有错字,请检查大写字母以写 getElementsByClassName

关于javascript - Electron 无法执行Java代码以检查Div是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61032582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com