Repository navigation
Expand file tree
/
Copy pathchatroom.user.js
More file actions
59 lines (52 loc) · 2.15 KB
/
Copy pathchatroom.user.js
File metadata and controls
59 lines (52 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ==UserScript==
// @name chatroom
// @namespace tzenganew
// @version 1.0
// @description chatroom
// @author You
// @match https://live.douyin.com/*
// @grant none
// @downloadURL https://raw.githubusercontent.com/tzenganew/userscripts/main/chatroom.user.js
// @updateURL https://raw.githubusercontent.com/tzenganew/userscripts/main/chatroom.user.js
// ==/UserScript==
(function() {
'use strict';
// 寻找目标元素的函数
function findChatroomLists() {
const chatroomLists = document.querySelectorAll('.webcast-chatroom___list');
if (chatroomLists.length >= 2) {
console.log('已找到两个或更多.webcast-chatroom___list元素,开始监听...');
observeChatrooms(chatroomLists);
} else {
console.log(`只找到 ${chatroomLists.length} 个.webcast-chatroom___list元素,10秒后重试...`);
setTimeout(findChatroomLists, 10000);
}
}
// 监听聊天室新增消息的函数
function observeChatrooms(chatroomLists) {
// 创建一个通用的MutationObserver实例
const observer = new MutationObserver(mutationsList => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
// 遍历所有新增的节点并输出innerHTML
mutation.addedNodes.forEach(node => {
if (node instanceof HTMLElement) {
console.log(node.innerHTML);
}
});
}
}
});
// 配置观察选项
const config = { childList: true, subtree: true };
// 对每个找到的聊天室列表元素启动观察
chatroomLists.forEach(list => {
observer.observe(list, config);
});
}
// 页面加载完成后10秒开始寻找元素
window.addEventListener('load', () => {
console.log('页面加载完成,10秒后开始寻找.webcast-chatroom___list元素...');
setTimeout(findChatroomLists, 10000);
});
})();