简体   繁体   English

如何从脚本中检查Electron应用程序的DOM?

[英]How do I inspect an Electron app's DOM from a script?

I have an Electron app running on my computer (Slack). 我的计算机上运行了一个Electron应用程序(Slack)。 I would like to run some JavaScript that reads HTML DOM of this app. 我想运行一些读取此应用程序的HTML DOM的JavaScript。 Is this possible? 这可能吗? If so, is there a "getting started" somewhere? 如果是这样,某个地方是否有“入门”?

In my mind, since Electron uses Node.js to host HTML / JavaScript, I can possibly read the DOM. 在我看来,由于Electron使用Node.js来托管HTML / JavaScript,我可以阅读DOM。 At the same time, I could see this not being allowed because it could be a security risk. 与此同时,我可以看到这不被允许,因为它可能存在安全风险。 I'm ok with the security risk since it's running on my machine. 我很安全,因为它在我的机器上运行。 So, I assume there would be a UAC prompt. 所以,我假设会有一个UAC提示。 Still, I'm just trying to figure out how to read the DOM from an external script if possible. 尽管如此,我只想弄清楚如何在可能的情况下从外部脚本中读取DOM。

Thank you 谢谢

From my understanding you want to inspect or manipulate some HTML of a electron app which is installed? 根据我的理解,你想检查或操纵一些已安装的电子应用程序的HTML?

This is how I figured out how to access (on Mac OS) using Slack as an example: 这就是我如何使用Slack作为示例来解决如何访问(在Mac OS上):

  1. Go to your Applications Folder -> Slack -> Right click "Show Package Contents" 转到您的应用程序文件夹 - > Slack - >右键单击“显示包内容”
  2. Go To "Contents->Resources -> app.asar.unpacked" 转到“目录 - >资源 - > app.asar.unpacked”
  3. You can check out how for example parts of the slack app work. 您可以查看松弛应用程序的示例部分是如何工作的。

I tried this also with GChat app and they have an app folder. 我也尝试使用GChat应用程序,他们有一个app文件夹。 Technically speaking, you could add a script or something into the index.html / index.jade (slack) and hijack into the main.js or index.js scripts. 从技术上讲,您可以在index.html / index.jade(slack)中添加脚本或其他内容,并劫持main.js或index.js脚本。

For example I was able to search for BrowserWindow Object inside the Chat App of Google Chat and add .webContents.openDevTools(); 例如,我能够在Google Chat的聊天应用程序中搜索BrowserWindow对象并添加.webContents.openDevTools(); easily. 容易。

Yet any solution involves manual work. 但任何解决方案都涉及手工操作

For example in the main.js of of GChat I beautified the code, I searched for the Electron method buildFromTemplate and found the specific function where the View Menu is created. 例如,在main.js中,我美化了代码,我搜索了Electron方法buildFromTemplate并找到了创建View菜单的特定功能。 I simply added the following to that 我只是添加了以下内容

 {
          role: "toggledevtools",
          label: "Toogle Dev Tools"
        }

And at the end I was able to easily toogle devtools (seen in the screenshot) 最后我能够轻松地访问devtools(在屏幕截图中看到)

在此输入图像描述

If you are thinking of accessing DOM through console (dev tools) like in a browser, then it's something you cannot do. 如果您正在考虑通过控制台(开发工具)访问DOM,就像在浏览器中一样,那么这是您无法做到的事情。 Because dev tools are available only in development environment, once you build it, you cannot access it. 由于开发工具仅在开发环境中可用,因此一旦构建它,就无法访​​问它。

So running a script may require the electron app to include the script in the source code : 因此,运行脚本可能需要电子应用程序在源代码中包含脚本:

<script src="your-script.js"></script>

So if they included your script in their app, then you could change the content of the script to achieve what you are looking for. 因此,如果他们将您的脚本包含在他们的应用程序中,那么您可以更改脚本的内容以实现您正在寻找的内容。

You can add your script into the page (before it loads) and have it there to interact with any page you open in electron. 您可以将您的脚本添加到页面中(在加载之前)并将其与您在电子中打开的任何页面进行交互。 The easiest way to do that is using preload paramater of new window. 最简单的方法是使用新窗口的preload参数。

Bear in mind, that you can even make connection with your nodejs script so there is really nice way of communicating with opened pages and your own script in node/electron. 请记住,您甚至可以与nodejs脚本建立连接,这样就可以很好地与已打开的页面和节点/电子中的脚本进行通信。

More here: https://electronjs.org/docs/api/browser-window 更多信息: https//electronjs.org/docs/api/browser-window

preload String (optional) - Specifies a script that will be loaded before other scripts run in the page. preload String(可选) - 指定在页面中运行其他脚本之前加载的脚本。 This script will always have access to node APIs no matter whether node integration is turned on or off. 无论是打开还是关闭节点集成,此脚本始终都可以访问节点API。 The value should be the absolute file path to the script. 该值应该是脚本的绝对文件路径。 When node integration is turned off, the preload script can reintroduce Node global symbols back to the global scope. 关闭节点集成时,预加载脚本可以将节点全局符号重新引入全局范围。 See example here. 见这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM