爱弹幕(暴力猴)

2022/8/25 13:52:48

当前为 2022-08-25 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        爱弹幕(暴力猴)
// @desc        人人视频弹幕(低端影视版本:https://ddys2.me)
// @namespace   Violentmonkey Scripts
// @match       https://ddys2.me/*
// @grant       none
// @version     1.0.1
// @supportURL  https://github.com/MaybeQHL/my_scripts/issues
// @license     MIT
// @author      maybe
// @email       [email protected]
// @description 2022/8/25 13:52:48
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js
// @grant       GM_notification
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/danmaku.min.js
// ==/UserScript==

const init = async () => {


    const utils = {
        addCss(str_css) {
            var style = document.createElement("style");
            style.textContent = str_css;
            document.getElementsByTagName("head").item(0).appendChild(style);
        },
        getParam(name) {
            var search = document.location.search;
            //alert(search);
            var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
            var matcher = pattern.exec(search);
            var items = null;
            if (null != matcher) {
                try {
                    items = decodeURIComponent(decodeURIComponent(matcher[1]));
                } catch (e) {
                    try {
                        items = decodeURIComponent(matcher[1]);
                    } catch (e) {
                        items = matcher[1];
                    }
                }
            }
            return items;
        },
        async getDanmu(keyword, epindex) {
            try {
                const reSearch = await axios.get('https://api.rr.tv/search/new/multiple', {
                    headers: {
                        clientVersion: '5.19.2',
                        clienttype: 'android_XiaoMi'
                    },
                    params: {
                        keywords: keyword
                    }
                })
                const dramaId = reSearch.data.data.seasonList[0].id;
                // debugger;
                const reDetail = await axios.get('https://api.rr.tv/drama/detail', {
                    headers: {
                        clientVersion: '5.19.2',
                        clienttype: 'android_XiaoMi'
                    },
                    params: {
                        isAgeLimit: false,
                        dramaId
                    }
                })

                const episodeId = reDetail.data.data.episodeList.find(ep => ep.episodeNo == epindex).id

                const reDms = await axios.get(`https://static-dm.rr.tv/v1/produce/danmu/EPISODE/${episodeId}`, {
                    headers: {
                        clientVersion: '5.19.2',
                        clienttype: 'android_XiaoMi'
                    }
                })

                let dms = reDms.data;

                // 格式化弹幕
                let res = (dms).map((it, index) => {
                    const pobj = it.p.split(',')
                    return {
                        id: 'rr_' + index,
                        text: it.d,
                        time: pobj[0],
                        style: {
                            fontSize: '20px',
                            color: '#ffffff'
                        },
                    }
                })

                // 重新排序
                res = res.sort((a, b) => a.time - b.time);

                return res
            } catch (error) {
                console.error(error.message)
                // GM_notification('[my_danmu]' + '暂无弹幕')
                return []
            }
        }
    }


    const initDm = async () => {

        const names = document.querySelector('.cute').textContent.split(' ');
        const name = names[0] + names[1];

        const ep = utils.getParam('ep');
        const episode = Number(ep && ep != 0 ? ep : 1);
        const dms = await utils.getDanmu(name, episode);

        const videoEl = document.querySelector('video');

        const videoWrapEl = videoEl.parentElement;

        // 容器样式
        const classStr = `.videoWrapEl{position:absolute;top:0;left:0;right:0;height:${videoWrapEl.clientHeight / 2}px;line-height:1.2em;} .videoWrapEl div {
        line-height:30px;
    }`;

        utils.addCss(classStr)

        const dmWrapEl = document.createElement('div')
        dmWrapEl.classList.add('videoWrapEl')

        videoWrapEl.append(dmWrapEl)


        var danmaku = new Danmaku({
            container: dmWrapEl,
            media: videoEl,
            comments: dms,
            // engine: 'canvas',
        });


        // danmaku.show();

        // 窗口大小改变重置弹幕
        window.addEventListener('resize', () => {
            danmaku.resize();
        })
    }

    initDm();

}


init();