将devtools中当前选定的元素传递给electron中的javascript

将devtools中当前选定的元素传递给electron中的javascript,electron,Electron,我想让用户通过电子中的devtools选择一个html元素。我知道你可以用变量$0得到这个。问题是它只能在dev工具控制台中访问 我查看了ChromeDevTools协议,找到了Runtime.evaluate,它有一个包含命令行api的参数。这似乎适用于除0美元以外的所有物品。我可以调用clear()、$()和其他东西。但是$0是未定义的 let debug = mainWindow.webContents.debugger; debug.attach(); mainWindow.webCo

我想让用户通过电子中的devtools选择一个html元素。我知道你可以用变量$0得到这个。问题是它只能在dev工具控制台中访问

我查看了ChromeDevTools协议,找到了Runtime.evaluate,它有一个包含命令行api的参数。这似乎适用于除0美元以外的所有物品。我可以调用clear()、$()和其他东西。但是$0是未定义的

let debug = mainWindow.webContents.debugger;
debug.attach();

mainWindow.webContents.on("devtools-opened", () => {
    devToolsOpen = true;
    myEmitter.emit("devtoolsready");
});

mainWindow.webContents.on("devtools-closed", () => {
    devToolsOpen = false;
});

//coords from renderer mouse event
ipcMain.on("inspectelement", (event, pageX, pageY) => {
    mainWindow.webContents.inspectElement(pageX, pageY);
    //if the devtools are not ready yet, wait for the event before executing the function
    if (devToolsOpen === false) {
        myEmitter.once("devtoolsready", devToolsFunction)
    }
    else {
        devToolsFunction();
    }

    async function devToolsFunction() {

        let backendNodeId = (await debug.sendCommand("DOM.getNodeForLocation", { x: pageX, y: pageY })).backendNodeId;
        let resolvedNode = (await debug.sendCommand("DOM.resolveNode", { backendNodeId: backendNodeId }));
        let js$0False = (await debug.sendCommand("Runtime.evaluate", { expression: "alert($0)", includeCommandLineAPI: false }));
        let js$0True = (await debug.sendCommand("Runtime.evaluate", { expression: "alert($0)", includeCommandLineAPI: true }));

        console.log(backendNodeId);
        console.log(resolvedNode);
        //Fails because $0 is not defined, does not alert
        console.log(js$0False);
        //$0 is set to undefined
        console.log(js$0True);
    }
});
我还尝试使用dom.getNodeForLocation通过内部dom表示访问元素,但我不知道如何使用它访问实际的元素


未定义$0对我来说似乎是一个bug,但我不确定。

经过更多的实验,devtools和devtools协议似乎不共享$0。解决方法是将所选节点推送到前端,然后调用setInspectedNode。在此之后,将设置并访问变量

//required for subsequent calls. Not sure if it is needed for every call but maybe 
//it does not update automaticly so it stays here
let doc = (await debug.sendCommand("DOM.getDocument"));

//backend id of the node at mouse position
let backendNodeId = (await debug.sendCommand("DOM.getNodeForLocation", { x: pageX, y: pageY })).backendNodeId;

//needed to push the backend node to the fron
let front = (await debug.sendCommand("DOM.pushNodesByBackendIdsToFrontend", { backendNodeIds: [backendNodeId] }));

//it seems like the devtools context and this one do not share $0.
//Because of this you need to call setInspectedNode, after which it will be set and accessible
let inspect = (await debug.sendCommand("DOM.setInspectedNode", { nodeId: front.nodeIds[0] }));

//write $0 to another var accessable from a normal js context
let js$0True = (await debug.sendCommand("Runtime.evaluate", { expression: "selectedNode = $0", includeCommandLineAPI: true }));

这是唯一有用的谷歌搜索结果,解释了如何做到这一点。令人难以置信的是,我是第一个投票支持这个答案的人!请将此答案标记为已接受。回答你自己的问题并不羞耻!