// ==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
// @downloadURL none
// ==/UserScript==
/*jshint multistr: true */
$("body").append("\
\
弹幕间隔:
秒
\
弹幕内容(多条用|或换行分隔):
\
\
Play
\
");
// 实现可拖动
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("斗鱼自动弹幕已准备.");