Electron无法执行Javascript来检查Div是否存在

Electron无法执行Javascript来检查Div是否存在,javascript,html,electron,Javascript,Html,Electron,我试图检查当前页面视图中是否存在具有类名的div。我尝试使用javascript查找元素并使用IPC发送结果,但我得到一个“Script failed to execute error”,我不确定如何解决这个问题 下面是我的代码,它打开一个页面,通过执行javascript检查div是否存在,将结果从渲染器发送回main,并根据返回的内容应用正确的css。由于我对前端的了解有限,我觉得这是一种不正确的方法 index.js const{app,BrowserWindow,globalShortc

我试图检查当前页面视图中是否存在具有类名的div。我尝试使用javascript查找元素并使用IPC发送结果,但我得到一个“Script failed to execute error”,我不确定如何解决这个问题

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

index.js

const{app,BrowserWindow,globalShortcut}=require'electron' 常量路径=要求“路径”; 常数fs=要求“fs”; const{ipcMain}=require'electron' const DOMAIN_URL=instagram.com 让我们赢; 函数createWindow{ 常量选项={ 宽度:500, 身高:550, 可调整大小:false, 最大化:错误, 是的, 标题栏:“希德尼斯特”, 标题:“InstaSend”, 网络首选项:{ 节点集成:错误 }, }; //创建主窗口 win=新浏览器窗口选项 win.loadURL'https://'+域URL //检查我们是否在登录页面 win.webContents.on'dom-ready',函数e{ win.webContents.executeJavaScript` var ipcrender=require'electron'。ipcrender; var hasLogin=document.getElementsbyClassNamegr27e[0]; 发送'checkLogin',hasLogin `; }; 在'checkLogin'上,functionevent,hasLogin{ console.loghasLogin; 如果!hasLogin{ win.resizeable=true win.maximable=true win.SetSize1100950,正确 win.webContents.insertCSSfs.readFileSyncpath.join_uudirname,“/assets/dash.css”,“utf8”; }否则{ win.webContents.insertCSSfs.readFileSyncpath.join_uudirname,“/assets/ig.css”,“utf8”; } } } 应用程序在“就绪”功能上{ 创建窗口 }; 错误输出:

您必须将nodeIntegration设置为true。否则不能使用require


此外,您有一个输入错误,请检查大写字母以写入getElementsByClassName

不幸的是,我在chrome的远程检查中看到了相同的错误。您使用大写B代替getElementsByClassName写入getElementsByClassName。现在可以了吗?现在可以了,谢谢!第一次做前台网站。。我正在用Atom编辑。是否有任何方法可以检查嵌入字符串中的代码语法,或者这是我们必须处理的问题?是的,您必须打开Electron应用程序的开发工具,就像在Chrome Ctrl+Maj+i ou Cmd+Alt+i中一样
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.
webPreferences: {
            nodeIntegration: true
        },