简体   繁体   English

Chrome 开发者工具 - 动态创建的元素

[英]Chrome Developer Tools - Dynamically Created Element

Is there a way to find out which JS script created a dynamic element in Chrome's Developer Tools?有没有办法找出哪个 JS 脚本在 Chrome 的开发人员工具中创建了动态元素? If I do 'view page source' on the page, the element isn't there.如果我在页面上“查看页面源”,则该元素不存在。 I can see the element though in Chrome's Developer Tools.我可以在 Chrome 的开发人员工具中看到该元素。 Is there a way to find out specifically which JavaScript file and what line in my JavaScript file created the element?有没有办法具体找出哪个 JavaScript 文件以及我的 JavaScript 文件中的哪一行创建了该元素?

To help clarify: I know which element is created...what I don't know is which .js file created it and specifically what line in that .js file帮助澄清:我知道创建了哪个元素……我不知道是哪个 .js 文件创建了它,特别是该 .js 文件中的哪一行

Updated answer :更新的答案

Below you've said:你在下面说过:

I know which element it is...what I don't know is which .js file created it and specifically what line in that .js file我知道它是哪个元素...我不知道是哪个 .js 文件创建了它,特别是该 .js 文件中的哪一行

That's not how the question originally read.这不是问题最初的阅读方式。 :-) :-)

If you know which element it is, two options for you:如果您知道它是哪个元素,则有两种选择:

  1. You can use Dev Tools to trigger a breakpoint when its parent element is modified:您可以使用 Dev Tools 在其父元素被修改时触发断点:

    1. Load the page加载页面

    2. Open Dev Tools打开开发工具

    3. Go to the Elements panel转到元素面板

    4. Navigate to the parent element that the target element will eventually be added to导航到目标元素最终将添加到的父元素

    5. Right-click the parent element and click Break on... > Subtree Modifications右键单击父元素并单击Break on... > Subtree Modifications

    Now, Chrome will trigger a breakpoint when the parent element's subtree is modified, and so you can see what JavaScript code is adding the element.现在,当父元素的子树被修改时,Chrome 会触发断点,因此您可以看到添加元素的 JavaScript 代码。

    Unfortuantely, it won't fire that breakpoint if the element is added during the main loading of the page (eg, during the parsing of the HTML, by script that runs immediately rather than waiting).不幸的是,如果在页面的主要加载期间(例如,在解析 HTML 期间,通过立即运行而不是等待的脚本)添加元素,则不会触发该断点。

  2. If there's any text in the element that seems specific to it (content, id , class , some attribute, whatever), once the page is loaded you can use Chrome's powerful search feature to try to find that text:如果元素中的任何文本似乎是特定于它的(内容、 idclass 、某些属性等等),一旦页面加载完毕,您就可以使用 Chrome 强大的搜索功能来尝试查找该文本:

    1. Load the page加载页面

    2. Open Dev Tools打开开发工具

    3. Go to the Sources tab转到“来源”选项卡

    4. Click Ctrl+Shift+F, which is "find in files" — it looks in all of the files associated with the page, not just the "current" file单击 Ctrl+Shift+F,即“在文件中查找”——它会查找与页面关联的所有文件,而不仅仅是“当前”文件

    5. Type the text that you think might help you identify the code adding the element输入您认为可能有助于识别添加元素的代码的文本

    6. Press Enter, all matches will be shown below按 Enter,所有匹配项将显示在下方

    You can even use regular expressions.您甚至可以使用正则表达式。


Original answer :原答案

No, there's no simple way to differentiate an element created via JavaScript after page load from ones created by the initial HTML parsing.不,没有简单的方法可以区分页面加载后通过 JavaScript 创建的元素与通过初始 HTML 解析创建的元素。

Or at least, there isn't without adding JavaScript to the page that runs before any other JavaScript on the page runs, which I'm guessing is a requirement.或者至少,在页面上的任何其他 JavaScript 运行之前,必须将 JavaScript 添加到页面中,我猜这是一个要求。

But if you can add JavaScript to the page before any other JavaScript runs, it's actually really easy to do:但是,如果您可以在任何其他 JavaScript 运行之前将 JavaScript 添加到页面,那么实际上很容易做到:

Array.prototype.forEach.call(document.querySelectorAll("*"), function(element) {
    element.setAttribute("data-original", "");
});

That marks every single element on the page with an attribute that tells you it was there when that code ran.这会用一个属性标记页面上的每个元素,该属性告诉您该代码运行时它在那里。 You can see those attributes in the Elements panel of the Dev Tools.您可以在开发工具的元素面板中看到这些属性。 And so, if you see an element that doesn't have that attribute, you know it was added later.因此,如果您看到一个没有该属性的元素,您就知道它是后来添加的。

document.querySelectorAll("*") is a big hammer you probably wouldn't want to use in production code, but for temporary use when debugging/developing, it's fine. document.querySelectorAll("*")是一个你可能不想在生产代码中使用的大锤子,但在调试/开发时临时使用,它很好。

And if you want to know about the elements that have been created by other code later, you can do this in the console:如果以后想知道其他代码已经创建的元素,可以在控制台中进行:

Array.prototype.forEach.call(document.querySelectorAll("*"), function(element) {
    if (element.getAttribute("data-original") === null) {
        console.log(element);
    }
});

That'll output every element that wasn't on the page when you ran the earlier code, and Chrome's console is really cool — you can right-click the element display in the console and choose "Reveal in Elements panel" to see exactly where that element is.这将输出运行早期代码时不在页面上的每个元素,Chrome 的控制台非常酷——您可以右键单击控制台中的元素显示并选择“在元素面板中显示”以查看确切位置那个元素是。

You can use chrome-devtools-protocol's experimental feature.您可以使用 chrome-devtools-protocol 的实验性功能。 Check this, https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getNodeStackTraces检查这个, https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getNodeStackTraces

First, send 'DOM.setNodeStackTracesEnabled' to chrome dev protocl.首先,将“DOM.setNodeStackTracesEnabled”发送到 chrome dev protocl。 Second, use 'DOM.getNodeStackTraces' message.其次,使用“DOM.getNodeStackTraces”消息。 So, you can get call stack information from dynamic creation element.因此,您可以从动态创建元素中获取调用堆栈信息。

I wrote my own program using these functions.我使用这些函数编写了自己的程序。

Image: https://imgur.com/a/TtL5PtQ图片: https : //imgur.com/a/TtL5PtQ

Here is my project: https://github.com/rollrat/custom-crawler这是我的项目: https : //github.com/rollrat/custom-crawler

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

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