forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
397ce35
commit d5b4f0a
Showing
10 changed files
with
340 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
此文件夹存放直播弹幕监听的JS脚本。 | ||
如何使用? | ||
浏览器打开你需要监听的直播间,然后F12打开控制台,粘贴脚本里的代码到控制台,回车运行 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
let socket = null; | ||
|
||
function connectWebSocket() { | ||
// 创建 WebSocket 连接,适配服务端 | ||
socket = new WebSocket("ws://127.0.0.1:5000"); | ||
|
||
// 当连接建立时触发 | ||
socket.addEventListener("open", event => { | ||
console.log("ws连接打开"); | ||
|
||
// 向服务器发送一条消息 | ||
// socket.send("ws连接成功"); | ||
}); | ||
|
||
// 当收到消息时触发 | ||
socket.addEventListener("message", event => { | ||
console.log("收到服务器数据:", event.data); | ||
}); | ||
|
||
// 当连接关闭时触发 | ||
socket.addEventListener("close", event => { | ||
console.log("WS连接关闭"); | ||
|
||
// 重连 | ||
setTimeout(() => { | ||
connectWebSocket(); | ||
}, 1000); // 延迟 1 秒后重连 | ||
}); | ||
} | ||
|
||
// 初始连接 | ||
connectWebSocket(); | ||
|
||
|
||
// 选择需要观察变化的节点 | ||
const targetNode = document.querySelector('.Barrage-list'); | ||
|
||
// 创建观察器实例 | ||
const observer = new MutationObserver(mutations => { | ||
mutations.forEach(mutation => { | ||
// 这里处理新增的DOM元素 | ||
if(mutation.type === 'childList') { | ||
mutation.addedNodes.forEach(node => { | ||
// 判断是否是新增的弹幕消息 | ||
if(node.classList.contains('Barrage-listItem')) { | ||
// 新增的动态DOM元素处理 | ||
// console.log('Added node:', node); | ||
|
||
const spans = node.getElementsByTagName('span'); | ||
|
||
let username = ""; | ||
let content = ""; | ||
|
||
for (let span of spans) { | ||
if (span.classList.contains('Barrage-nickName')) { | ||
const targetSpan = span; | ||
// 获取用户名 | ||
username = targetSpan.textContent.trim().slice(0, -1); | ||
} else if (span.classList.contains('Barrage-content')) { | ||
const targetSpan = span; | ||
// 获取弹幕内容 | ||
content = targetSpan.textContent.trim(); | ||
} | ||
} | ||
|
||
// 获取到弹幕数据 | ||
if (username != "" && content != "") { | ||
const data = { | ||
type: "commit", | ||
username: username, | ||
content: content | ||
}; | ||
console.log(data); | ||
socket.send(JSON.stringify(data)); | ||
} | ||
} | ||
}) | ||
} | ||
}); | ||
}); | ||
|
||
// 配置观察选项 | ||
const config = { | ||
childList: true, | ||
subtree: true | ||
}; | ||
|
||
// 开始观察 | ||
observer.observe(targetNode, config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.