MutationObserver 回调中的 MutationObserver

发布于 2025-01-09 15:46:42 字数 1942 浏览 1 评论 0原文

我在 JavaScript 代码中添加了一个 MutationObserver 并将其应用于 iFrame 中的特定元素。检测工作正常,但现在我需要第二个观察者。在 iFrame 中第一次单击后,它的整个 DOM 发生变化,此时第一个观察者进入并检测 iFrame 中导航元素上的任何其他单击。除此之外,我需要观察一个 div 元素以及它的文本是否会更改。因此,我们的想法是在单击导航按钮后创建第二个观察者,但它似乎不起作用,因为第二个观察者在我的控制台上没有给出任何输出。

这是 JavaScript 代码:

$('iframe').on('load', function() {
        let observer = new MutationObserver(mutationRecords => {
            let completed = false;
            var progress = $('iframe').contents().find('.overview-sidebar__header .progress-bar__percentage-bottom').first();
            let clickedClass = mutationRecords[0].target.attributes[0].nodeValue;
            
            if(clickedClass == 'transition-group') {
                console.log(progress.text());
                
                console.log(Math.ceil(Date.now() / 1000));
            } else if(clickedClass == 'page-wrap' && !mutationRecords[0].nextSibling) {
                let secondObserver = new MutationObserver(mutationRecords => { console.log('Test') });

                secondObserver .observe($('iframe').contents().find('.transition-group').first()[0], {
                    childList: true,
                    subtree: true
                });

                console.log(Math.ceil(Date.now() / 1000));
                
                $('iframe').contents().find('.section-lists .lesson-lists__item').each(function(index) {
                    console.log(index);
                    console.log($(this).find('.lesson-link__name > span').first().text());
                    
                    console.log($(this).find('.lesson-link__progress title').text().split('%')[0]);
                });
            }
        });
        
        observer.observe($('iframe').contents().find('.transition-group').first()[0], {
            childList: true,
            subtree: true
        });
    });

else if 分支中的 secondaryObserver 是这里的问题。有人知道如何解决这个问题吗?

I was adding a MutationObserver in my JavaScript code and applied it to a specific element in an iFrame. The detection works fine but now I need a second observer. After the first click in the iFrame the whole DOM of it changes and that's the moment where the first observer comes in and detects any other clicks on the navigation elements in the iFrame. Additionally to that I need to observe one div element and if it's text will be changed or not. So the idea was to create a second observer after a navigation button is clicked but it seems not to work since the second observer doesn't give any output on my console.

Here is the JavaScript code:

$('iframe').on('load', function() {
        let observer = new MutationObserver(mutationRecords => {
            let completed = false;
            var progress = $('iframe').contents().find('.overview-sidebar__header .progress-bar__percentage-bottom').first();
            let clickedClass = mutationRecords[0].target.attributes[0].nodeValue;
            
            if(clickedClass == 'transition-group') {
                console.log(progress.text());
                
                console.log(Math.ceil(Date.now() / 1000));
            } else if(clickedClass == 'page-wrap' && !mutationRecords[0].nextSibling) {
                let secondObserver = new MutationObserver(mutationRecords => { console.log('Test') });

                secondObserver .observe($('iframe').contents().find('.transition-group').first()[0], {
                    childList: true,
                    subtree: true
                });

                console.log(Math.ceil(Date.now() / 1000));
                
                $('iframe').contents().find('.section-lists .lesson-lists__item').each(function(index) {
                    console.log(index);
                    console.log($(this).find('.lesson-link__name > span').first().text());
                    
                    console.log($(this).find('.lesson-link__progress title').text().split('%')[0]);
                });
            }
        });
        
        observer.observe($('iframe').contents().find('.transition-group').first()[0], {
            childList: true,
            subtree: true
        });
    });

The secondObserver in the else if branch is the problem here. Anyone has an idea how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文