Greasy Fork

Greasy Fork is available in English.

斗鱼自动回复,可重复回复(刷屏)

斗鱼自动回复,自定义时间,次数,回复内容!

当前为 2018-10-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         斗鱼自动回复,可重复回复(刷屏)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  斗鱼自动回复,自定义时间,次数,回复内容!
// @author       Pi7bo1
// @match        https://www.douyu.com/*
// @grant        none
// @supportURL   [email protected]
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...

    // 定义初始变量
    var dySendMsg = function (text, maxCount, stime) {
        this.lastMsg = ""; // 上次喊话内容
        this.count = 0; // 当前喊话次数
        this.stext = text || new Date(); // 喊话内容
        this.stime = stime || 30; // 间隔时间 单位/秒
        this.auto = ""; //自动变量,用来定义/清除定时器
        this.maxCount = maxCount || 99; //最大喊话次数
        this.status = "stop"; //当前状态
    };

    //初始化发送模板
    dySendMsg.prototype.init = function () {
        var othis = this;

        var jsSendMsg = document.getElementById('js-chat-speak');

        //添加自定义定义样式
        var _styleElement = document.createElement('style');
        var _css = ".dyFunctionSend{background: #fff;width:100%;box-sizing: border-box;position:absolute;padding:5px 6px;z-index: 999;border: 1px solid #d2d2d2;bottom: 90px;left: 0;}";
        _css += ".dyFunctionSend .form-control{box-sizing: border-box;background-color: #FFFFFF;resize:none;background-image: none;border: 1px solid #e5e6e7;border-radius: 1px;color: inherit;display: block;padding: 6px 12px;transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;width: 100%;}";
        _css += ".dyFunctionSend .form_item{margin:5px 0;font-size:14px}";
        _css += ".dyFunctionSend .form-control:focus{border-color: #1ab394;}";
        _css += ".dyFunctionSend_hide {display: none;}";
        _css += ".dySend_btn {margin:5px;display: inline-block;height: 33px;line-height: 33px;padding: 0 18px;background-color: #009688;color: #fff;white-space: nowrap;text-align: center;font-size: 14px;border: none;border-radius: 2px;cursor: pointer;}";
        _css += ".dySend_btn:hover {opacity: 0.8}";
        _css += ".dySend_btn:active {opacity: 1}";
        _css += ".dySend_start_btn,.dySend_stop_btn{background-color: #1E9FFF;display:none}";
        _css += ".dySend_start_btn.show_d,.dySend_stop_btn.show_d{display:inline-block}";
        _css += ".ChatSendMsgBtn {display: inline-block;vertical-align: middle;width: 22px;height: 22px;margin-right: 8px;cursor: pointer;}";
        _css += ".ChatSendMsgBtn .Horn4Category-btn{background:url('https://luo-1255851923.cos.ap-guangzhou.myqcloud.com/images/20181022140905.png') 50% no-repeat;background-position: 0 0px;}";
        _css += ".ChatSendMsgBtn.active .Horn4Category-btn{background-position-x: -24px;}";

        _styleElement.innerHTML = _css;
        document.getElementsByTagName('head')[0].appendChild(_styleElement);


        //添加点击的弹框模板
        var dyFunctionSend = document.createElement('div');
        var _html = '';
        _html += '<div class="form_item"><div class="col-100-w"><label>文本内容:</label></div>';
        _html += '<div class="col-100-w"><textarea maxlength="56" class="form-control" name="" id="dyFunctionSend_text" rows="2"></textarea></div></div>';
        _html += '<div class="form_item"><div class="col-100-w"><label>喊话次数:</label></div>';
        _html += '<div class="col-100-w"><input class="form-control" placeholder="默认喊话喊话次数为 99" onkeyup="value=value.replace(/[^\d]/g,"")" type="text" name="" id="dyFunctionSend_count"></div></div>';
        _html += '<div class="form_item"><div class="col-100-w"><label>喊话时间间隔:</label></div>';
        _html += '<div class="col-100-w"><input class="form-control" placeholder="默认喊话时间间隔为 30" onkeyup="value=value.replace(/[^\d]/g,"")" type="text" name="" id="dyFunctionSend_time"></div></div>';
        _html += '<div class="col-100-w"><span style="color:#FF5722" id="dyFunctionSend_error">&#x3000;</span></div>';
        _html += '<div class="col-100-w" style="text-align: center;"><span onclick="javascript:$dy.save()" class="dySend_save_btn dySend_btn">保存</span><span onclick="javascript:$dy.start()" class="dySend_start_btn show_d dySend_btn">开始</span><span onclick="javascript:$dy.stop()" class="dySend_stop_btn dySend_btn">停止</span></div>';
        _html += '<div style="top:-4px" onclick="javascript:$dy.changeDySendPanel()" class="PrivateLetter-close"><i></i></div>';


        dyFunctionSend.classList.add('dyFunctionSend', 'dyFunctionSend_hide');
        dyFunctionSend.innerHTML = _html;
        dyFunctionSend.id = "dyFunctionSend";

        var chatMsgBox = document.querySelectorAll('.layout-Player-aside')[0];
        chatMsgBox.appendChild(dyFunctionSend);

        // 斗鱼按钮栏插入按钮
        var timer = setInterval(function () {
            try {
                document.querySelectorAll('.ShieldTool-content')[0].querySelectorAll('.ShieldTool-checkText')[0].innerText = "屏蔽特效";
                var sendMsgBtn = document.createElement('div');
                sendMsgBtn.onclick = othis.changeDySendPanel;
                sendMsgBtn.classList.add('ChatSendMsgBtn');
                sendMsgBtn.innerHTML = `<div title="喊话设置"  class="Horn4Category-btn Horn4Category-btn--popTrigger"></div>`
                document.querySelectorAll('.ChatToolBar')[0].appendChild(sendMsgBtn);
                clearInterval(timer);
            } catch (err) {
               
            }
        }, 50)
    }

    //开关面板
    dySendMsg.prototype.changeDySendPanel = function () {
        document.getElementById('dyFunctionSend').classList.toggle('dyFunctionSend_hide');
        document.querySelectorAll('.ChatToolBar')[0].querySelectorAll('.ChatSendMsgBtn')[0].classList.toggle('active');
    }


    // 保存当前设置
    dySendMsg.prototype.save = function () {

        if (this.status == "start") {
            document.getElementById('dyFunctionSend_error').innerText = "需要先停止喊话才能进行修改";
            return;
        }

        document.getElementById('dyFunctionSend_error').innerText = "";

        this.stext = document.getElementById('dyFunctionSend_text').value;
        this.maxCount = document.getElementById('dyFunctionSend_count').value || new Date();
        this.stime = document.getElementById('dyFunctionSend_time').value || 30;
    }

    // 开始
    dySendMsg.prototype.start = function () {
        this.autoSend();
        this.status = "start";
        document.querySelectorAll('.dySend_start_btn')[0].classList.toggle('show_d');
        document.querySelectorAll('.dySend_stop_btn')[0].classList.toggle('show_d');
    }

    // 停止 清除定时器
    dySendMsg.prototype.stop = function () {
        this.count = 0;
        this.status = "stop";
        clearTimeout(this.auto);
        document.querySelectorAll('.dySend_start_btn')[0].classList.toggle('show_d');
        document.querySelectorAll('.dySend_stop_btn')[0].classList.toggle('show_d');
    }

    // 自动发送
    dySendMsg.prototype.autoSend = function () {

        // this.changeDySendPanel();
        var jsSend = document.querySelectorAll('.ChatSend')[0];

        // 处理内容
        var currentMsg = (this.count % 2 == 0 ? "‍" : "") + this.stext;

        // 如果相同
        if (this.lastMsg == currentMsg) {
            currentMsg = "‍" + currentMsg;
        }

        //输入框赋值
        jsSend.querySelectorAll('textarea.ChatSend-txt')[0].value = currentMsg;

        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        jsSend.querySelectorAll('.ChatSend-button')[0].dispatchEvent(e);

        // 变量信息修改
        this.count += 1;
        this.lastMsg = currentMsg;
        
        console.log('当前共发送' + this.count + "条弹幕");

        if (this.count >= this.maxCount) {
            this.stop();
            return;
        }

        this.auto = setTimeout(this.autoSend.bind(this), this.stime * 1000 + (Math.floor(Math.random() * 50)) / 100);
    }

    window.$dy = new dySendMsg();
    $dy.init();

})();