js获取抖音弹幕

发布时间 2023-11-16 14:53:29作者: 樱桃树下的约定
function dom() {  
    // 创建一个MutationObserver实例  
    let mutationObserver = new MutationObserver(function(mutationsList, observer) {  
        for(var mutation of mutationsList) {  
            console.log("用户:",mutation.target.lastChild.lastChild.childNodes[1].textContent,'信息:', mutation.target.lastChild.lastChild.lastChild.textContent);  
        }  
    });  
    // 选择要监视的DOM元素,这里假设是抖音Web端的弹幕容器  
    let targetElement = document.querySelector("#_douyin_live_scroll_container_ > div > div > .Iy_uuM55 > .__playerIsFullChatroom > .ro3xcipQ > .lF9JOwdS > div > .webcast-chatroom___messages > div > .webcast-chatroom___item-offset > div > div:nth-child(1)");  
 
    if(targetElement){  
        // 启动观察器,开始监视目标元素的变化  
        mutationObserver.observe(targetElement, { attributes: true, childList: true });  
    }else{  
        console.log('Target element not found');  
    }  
}