淘宝弹幕助手v1

获取淘宝直播弹幕信息并发送详情

目前为 2023-09-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         淘宝弹幕助手v1
// @namespace    http://tampermonkey.net/
// @version      4.0
// @description  获取淘宝直播弹幕信息并发送详情
// @match        https://liveplatform.taobao.com
// @match        https://liveplatform.taobao.com/restful/index/live/control/*
// @icon         https://cdn.52ym.vip/temp/a0whr-l6z8y-001.ico
// @grant        none
// @license      MIT
// ==/UserScript==
console.log("开启直播了..................") 
const TIME = 3000;

const liveDetailUrl = ""
const platformMap = new Map([
    ['liveplatform.taobao.com', handleTaoBaoBarrage]
]);
 
let barrageIds = []; // 存储已处理的弹幕ID
 
// const WS = new WebSocket(HOST); // 创建WebSocket对象
// WS.onopen = function () {
//     console.log('连接成功');
// };
// WS.onclose = function () {
//     console.log('断开连接');
// };
 
function handleRequest(username, content, id) {
    // 构造弹幕数据对象
    let data = {
        nickname: username,
        content: content
    };
 
    console.log(data);
    // 发送弹幕数据到WebSocket服务器
    // WS.send(JSON.stringify(data));
 
    // 将已处理的弹幕ID添加到列表中
    barrageIds.push(id);
    if (barrageIds.length > 300) {
        barrageIds.splice(0, 100);
    }
}
 
function handleTaoBaoBarrage() {
    let barrageContainer = document.querySelector('.alpw-container > div.alpw-comment-item:last-child');
    console.log("current barrageContainer:",barrageContainer)
    if (!barrageContainer) {
        return; // Exit if the barrage container element is not found
    }
    let barrageElements = barrageContainer.querySelectorAll('div.alpw-comment-item');
    for (let i = 0; i < barrageElements.length; i++) {
        let barrageElement = barrageElements[i];
        let usernameElement = barrageElement.querySelector('div.alpw-userinfo > div.alpw-left > div.alpw-username > div.alpw-username-text');
        let contentElement = barrageElement.querySelector('div.alpw-comment-content');
 
        // 检查弹幕元素是否存在
        if (!usernameElement || !contentElement) {
            continue; // Skip if either username or content element is not found
        }
 
        let username = usernameElement.textContent.trim();
        let content = contentElement.textContent.trim();
 
        let id = username + content;
        // 检查是否已处理过该弹幕
        if (barrageIds.includes(id)) {
            continue;
        }
        handleRequest(username, content, id);
    }
}
 
 
(function () {
    const currUrl = window.location.href
    platformMap.forEach((value, key) => {
        if (currUrl.indexOf(key) !== -1) {
            setInterval(value, TIME);
        }
    })
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址