Greasy Fork

Greasy Fork is available in English.

斗鱼自动弹幕

自动发斗鱼弹幕

当前为 2017-05-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         斗鱼自动弹幕
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  自动发斗鱼弹幕
// @author       Lang
// @match        https://www.douyu.com/*
// @match        http://www.douyu.com/*
// @grant        none

// ==/UserScript==


/*jshint multistr: true */
$("body").append("\
<div id='robot' style='position: absolute; top: 780px; left: 740px; z-index: 900; display: block; background: #65c294; padding:5px;'> \
弹幕间隔:<input id='robot-interval' placeholder='秒数' value='5' style='display:inline-block;width: 30px'/>秒<br />\
弹幕内容(多条用|或换行分隔):<br/>\
<textarea id='robot-danmu' rows='5' cols='30'>6666|666666|6666666666|6666666666666|66666</textarea><br />\
<div id='robot-play' style='display: inline-block; margin-top: 5px;padding:5px; border: 1px solid gray; float:right'>Play</div>\
</div>");

// 实现可拖动
var ismousedown = false;
var dialogleft,dialogtop;
var downX,downY;
dialog = $("#robot");
dialogleft = parseInt(dialog.css("left"));
dialogtop = parseInt(dialog.css("top"));
dialog.mousedown(function(e){
    ismousedown = true;
    downX = e.clientX;
    downY = e.clientY;
});
document.onmousemove = function(e){
    if(ismousedown){
        dialog.css("top", e.clientY - downY + dialogtop + "px");
        dialog.css("left", e.clientX - downX + dialogleft + "px");
    }
};
/*松开鼠标时要重新计算当前窗口的位置*/
document.onmouseup = function(){
    dialogleft = parseInt(dialog.css("left"));
    dialogtop = parseInt(dialog.css("top"));
    ismousedown = false;
};
//console.log($("#js-chat-speak").offset().top);
//dialog.css("top", $("#js-chat-speak").offset().top +"px");
//dialog.css("left", $("#js-chat-speak").offset().left + "px");


msg0 = ["666","66666","66666666", "666666666666", "666666666666666666666666666"];
function Robot() {}
Robot.prototype = {
    constructor: Robot,
    msg: msg0,
    lastText: '',
    suffix: ["!","!!","!!!", " @@", " :)", " T_T", " ##", " **"],
    flag: 0,
    interval: 5000,
    send: function(text) {
        $("#js-send-msg > textarea.cs-textarea").val(text);
        $("#js-send-msg > .b-btn").click();
        console.debug(text);
    },
    getText: function() {
        if(this.msg.length === 1) {
            return this.msg[0] + this.suffix[Math.floor(Math.random() * this.suffix.length)];
        }
        return this.msg[Math.floor(Math.random() * this.msg.length)];
    },
    start: function() {
        var that = this;
        this.stop();
        text = this.getText();
        while(text == this.lastText) {
            text = this.getText();
        }
        // 不能发送,冷却中
        if($("#js-send-msg > .b-btn").hasClass('b-btn-gray')) {
            nextTime = parseInt($("#js-send-msg > .b-btn").text());
            if(isNaN(nextTime)) {
                this.flag = setTimeout(function(){that.start();}, nextTime * 1000);
                return;
            }
        }
        this.send(text);
        this.lastText = text;
        this.flag = setTimeout(function(){that.start();}, this.interval);
    },
    stop: function() {
        clearTimeout(this.flag);
    },
    setMessage: function(arr) {
        if(!(arr instanceof Array)) {
            console.error("setMessage: arr should be an array!");
            return;
        }
        this.msg = arr;
        console.log("setMessage " + arr);
        if(this.msg.length === 0) {
            this.msg = msg0;
        }
    },
    setInterval: function(seconds) {
        sec = parseInt(seconds);
        console.log('set interval to ' + seconds);
        if((!isNaN(sec)) && sec > 0) {
            this.interval = sec * 1000;
        } else {
            console.warn("set interval " + seconds + " failed.");
        }
    }
};
robot = new Robot();
window.robot = robot;

$("#robot-play").click(function(){
    if($("#robot-play").text() != "Stop") {
        $("#robot-play").text("Stop");
        robot.setInterval($('#robot-interval').val());
        robot.setMessage($('#robot-danmu').val().split(/[\n|]/).filter(function(s){return s && s.trim();}));
        robot.start();
    } else {
        robot.stop();
        $("#robot-play").text("Play");
    }
});

console.log("斗鱼自动弹幕已准备.");