// ==UserScript==
// @name Bilibili直播间挂机助手
// @namespace SeaLoong
// @version 1.4.4
// @description Bilibili直播间自动签到,领瓜子,参加抽奖,完成任务,送礼等
// @author SeaLoong
// @include /https?:\/\/live\.bilibili\.com\/\d+/
// @require https://greasyfork.org/scripts/38140-bilibili-api/code/Bilibili-API.js
// @grant none
// @run-at document-end
// @license MIT License
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// <-!!!请注意,修改此处设置将不会再生效,请点击页面右下角的"挂机助手设置"打开设置界面进行设置!!!->
var CONFIG = {
USE_SIGN: true, // 自动签到
USE_AWARD: true, // 自动领取瓜子
USE_LOTTERY: true, // 自动参加抽奖
LOTTERY_CONFIG: {
ALLOW_NOT_SHORT_ROOMID: false // 允许在非短房间号直播间进行抽奖(挂在2号直播间)
},
USE_TASK: true, // 自动完成任务
USE_GIFT: false, // 自动送礼物
GIFT_CONFIG: { // 若启用自动送礼物,则需要设置以下项
SHORT_ROOMID: 0, // 送礼物的直播间ID(即地址中live.bilibili.com/后面的数字), 设置为0则表示自动检查当前主播勋章
CHANGE_MEDAL: false, // 设置是否允许 当有当前主播勋章,且当前佩戴的勋章不是当前主播勋章时自动切换为当前主播勋章
SEND_GIFT: ['1'], // 设置默认送的礼物类型编号(见下方列表/点击问号),多个请用英文逗号(,)隔开,为空则表示默认不送出礼物
ALLOW_GIFT: ['1', '4', '6'], // 设置允许送的礼物类型编号(见下方列表/点击问号)(!!任何未在此列表的礼物一定不会被送出!!),多个请用英文逗号(,)隔开,为空则表示允许送出所有类型的礼物
SEND_TODAY: false // 送出包裹中今天到期的礼物(!会送出SEND_GIFT之外的礼物!若今日亲密度已满则不送)
},
SHOW_TOAST: true, // 显示浮动提示
EXCHANGE_SILVER2COIN: false // 消耗700银瓜子兑换1个硬币
};
// <-!!!请注意,修改此处设置将不会再生效,请点击页面右下角的"挂机助手设置"打开设置界面进行设置!!!->
/* 礼物编号及对应礼物、亲密度对照表
(有些数据暂时不清楚,如有知道的可以告诉我,目前采用的亲密度计算方法是:礼物亲密度=向上取整(礼物价值瓜子数/100))
1:辣条:亲密度+1
3:B坷垃:亲密度+99
4:喵娘:亲密度+52
6:亿元:亲密度+10
7:666:亲密度+?
8:233:亲密度+?
25:小电视:亲密度+12450?
39:节奏风暴:亲密度+1000?
105:火力票:亲密度+20
106:哔哩星:亲密度+20
109:红灯笼:亲密度+20
110:小爆竹:亲密度+20
*/
/* 此行以下内容请勿修改,当然你要改那我也没办法 */
var DEBUGMODE = false;
var DEBUG = function(sign, data) {
if (!DEBUGMODE) return;
var d = new Date();
d = '[' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds() + ']';
window.console.debug(d, sign + ':', data);
};
var CONFIG_DEFAULT = {
USE_SIGN: true,
USE_AWARD: true,
USE_LOTTERY: true,
LOTTERY_CONFIG: {
ALLOW_NOT_SHORT_ROOMID: false
},
USE_TASK: true,
USE_GIFT: false,
GIFT_CONFIG: {
SHORT_ROOMID: 0,
CHANGE_MEDAL: false,
SEND_GIFT: ['1'],
ALLOW_GIFT: ['1', '4', '6'],
SEND_TODAY: false
},
SHOW_TOAST: true,
EXCHANGE_SILVER2COIN: false
};
var CONFIG_NAME_LIST = {
USE_SIGN: '自动签到',
USE_AWARD: '自动领取瓜子',
USE_LOTTERY: '自动参加抽奖',
LOTTERY_CONFIG: '抽奖设置',
ALLOW_NOT_SHORT_ROOMID: '允许在任意直播间抽奖(实验)',
USE_TASK: '自动完成任务',
USE_GIFT: '自动送礼物',
GIFT_CONFIG: '送礼设置',
SHORT_ROOMID: '房间号',
SEND_GIFT: '默认礼物类型',
ALLOW_GIFT: '允许礼物类型',
CHANGE_MEDAL: '允许切换勋章',
SEND_TODAY: '送出包裹中今天到期的礼物',
SHOW_TOAST: '显示浮动提示',
EXCHANGE_SILVER2COIN: '银瓜子换硬币'
};
var CONFIG_PLACEHOLDER_LIST = {
SHORT_ROOMID: '为0则自动检测勋章',
SEND_GIFT: "为空则默认不送",
ALLOW_GIFT: '为空则允许所有'
};
var CONFIG_HELP_LIST = {
USE_LOTTERY: '设置是否自动参加抽奖功能,包括小电视抽奖、活动(即B站当前进行的活动)抽奖',
ALLOW_NOT_SHORT_ROOMID: '(实验性)允许在任意直播间进行抽奖(实际上是挂在一个短号直播间参加抽奖)
注意:会消耗更多的系统资源',
SHORT_ROOMID: '送礼物的直播间ID(即地址中live.bilibili.com/后面的数字), 设置为0则表示自动检查当前主播勋章',
CHANGE_MEDAL: '设置是否允许“当有当前主播勋章,且当前佩戴的勋章不是当前主播勋章时自动切换为当前主播勋章”',
SEND_GIFT: function() {
var s = '设置默认送的礼物类型编号,多个请用英文逗号(,)隔开,为空则表示默认不送出礼物';
return s + '
' + gift_list_str;
},
ALLOW_GIFT: function() {
var s = '设置允许送的礼物类型编号(任何未在此列表的礼物一定不会被送出!),多个请用英文逗号(,)隔开,为空则表示允许送出所有类型的礼物';
return s + '
' + gift_list_str;
},
SEND_TODAY: '送出包裹中今天到期的礼物(会送出"默认礼物类型"之外的礼物,若今日亲密度已满则不送)',
EXCHANGE_SILVER2COIN: '消耗700银瓜子兑换1个硬币(每天只能兑换一次)'
};
var CONFIG_CONTROL_LIST = {
USE_GIFT: 'GIFT_CONFIG',
USE_LOTTERY: 'LOTTERY_CONFIG'
};
var NAME = 'Bilibili-LiveRoom-HangHelper';
var API = BilibiliAPI;
var TaskAward_Running = false;
var Toast = {
element: null,
list: [],
count: 0
};
var DOM = {
treasure: {
div: null,
image: null,
canvas: null,
div_tip: null,
div_timer: null
},
storm: {
div: null,
image: null,
canvas: null
},
config: {
is_showed: false,
div_button: null,
div_button_span: null,
div_side_bar: null,
div_position: null,
div_style: null,
div_title: null,
div_title_span: null,
div_title_button: null,
div_button_reset: null,
div_context_position: null,
div_context: null
},
alertdialog: {
div_background: null,
div_position: null,
div_style: null,
div_title: null,
div_title_span: null,
div_content: null,
div_button: null,
button_ok: null
},
lottery: {
iframe: null
}
};
var interval_treasure_timer;
var room_id_list = [];
var lottery_list_last = [],
lottery_check_time = 20;
var gift_list;
var gift_list_str = '礼物编号及对应礼物、亲密度对照表
';
var timediff = 0;
var Info = {
short_id: null,
uid: null,
ruid: null,
roomid: null,
rnd: null,
area_id: null, // area_v2_id
area_parent_id: null,
old_area_id: null, // areaId
csrf_token: function() {
return getCookie('bili_jct');
},
today_feed: null,
day_limit: null,
silver: null,
gold: null,
mobile_verified: null,
medal_list: null,
medal_target_id: null,
task_list: null,
bag_list: null
};
function ts_s() {
return Math.floor(ts_ms() / 1000);
}
function ts_ms() {
return Date.now() + timediff;
}
function getCookie(name) {
var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
if ((arr = document.cookie.match(reg))) {
return unescape(arr[2]);
} else {
return null;
}
}
function setCookie(name, value, seconds) {
seconds = seconds || 0;
var expires = '';
if (parseInt(seconds, 10) !== 0) {
var date = new Date();
date.setTime(date.getTime() + (seconds * 1000));
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + escape(value) + expires + '; path=/';
}
/*
验证码识别算法来自互联网,作者未知
该算法已被简单修改
*/
function getChar(t) {
if (t.sum <= 50) return '-';
if (t.sum > 120 && t.sum < 135) return '+';
if (t.sum > 155 && t.sum < 162) return 1;
if (t.sum > 189 && t.sum < 195) return 7;
if (t.sum > 228 && t.sum < 237) return 4;
if (t.sum > 250 && t.sum < 260) return 2;
if (t.sum > 286 && t.sum < 296) return 3;
if (t.sum > 303 && t.sum < 313) return 5;
if (t.sum > 335 && t.sum < 342) return 8;
if (t.sum > 343 && t.sum < 350) {
if (t.first > 24 && t.last > 24) return 0;
if (t.first < 24 && t.last > 24) return 9;
if (t.first > 24 && t.last < 24) return 6;
}
}
function calcImg() {
/*
* 1.验证码图片->二维点阵
* 2.二维点阵->横向一维压缩
* 3.分析并计算
*/
var ctx = DOM.treasure.canvas[0].getContext("2d");
ctx.drawImage(DOM.treasure.image[0], 0, 0, 120, 40);
var pixels = ctx.getImageData(0, 0, 120, 40).data;
var pix = [];
var i = 0;
var j = 0;
var n = 0;
for (i = 1; i <= 40; i++) {
pix[i] = [];
for (j = 1; j <= 120; j++) {
var c = 1;
if (pixels[n] - (-pixels[n + 1]) - (-pixels[n + 2]) > 200) {
c = 0;
}
n += 4;
pix[i][j] = c;
}
}
//二维点阵pix[40][120]
var line = [];
line[0] = 0;
for (i = 1; i <= 120; i++) {
line[i] = 0;
for (j = 1; j <= 40; j++) {
line[i] += pix[j][i];
}
}
//一维line[120]
var temp = [];
n = 0;
for (i = 1; i <= 120; i++) {
if (line[i] > 0 && line[i - 1] === 0) {
n++;
temp[n] = {};
temp[n].first = line[i];
temp[n].sum = 0;
}
if (line[i] > 0) {
temp[n].sum += line[i];
}
if (line[i - 1] > 0 && line[i] === 0) {
temp[n].last = line[i - 1];
}
}
if (n === 4) {
var result = 0;
var a = getChar(temp[1]) * 10 - (-getChar(temp[2]));
var b = getChar(temp[4]);
if (getChar(temp[3]) === '+') {
result = a - (-b);
} else {
result = a - b;
}
DEBUG('TaskAward: calcImg: 识别验证码: ' + getChar(temp[1]) + getChar(temp[2]) + ' ' + getChar(temp[3]) + ' ' + getChar(temp[4]) + ' = ' + result);
return result;
} else {
DEBUG('TaskAward: calcImg: 识别验证码失败');
return null;
}
}
function solveCaptcha() {
var ctx = DOM.storm.canvas[0].getContext('2d');
ctx.drawImage(DOM.storm.image[0], 0, 0, 112, 32);
return OCRAD(ctx.getImageData(0, 0, 112, 32));
}
function giftIDtoFeed(gift_id) {
for (var i = gift_list.length - 1; i >= 0; i--) {
if (gift_list[i].id == gift_id) {
return Math.ceil(gift_list[i].price / 100);
}
}
return null;
}
window.toast = function(e, n, r) {
var t = Toast.element;
if (!CONFIG.SHOW_TOAST || !t) return;
if ('boolean' === typeof n) n = 'info';
var o = document.createDocumentFragment(),
a = document.createElement('div');
if ('success' !== (n = n || 'info') && 'caution' !== n && 'error' !== n && 'info' !== n)
throw new Error(i + ' 在使用 Link Toast 时必须指定正确的类型: success || caution || error || info');
if (a.innerHTML = '' + e + '',
a.className = 'link-toast ' + n + ' ' + (r ? 'fixed' : ''), !t.className && !t.attributes)
throw new Error(i + ' 传入 element 不是有效节点.');
var c = t.getBoundingClientRect(),
s = c.left,
u = c.top,
l = c.width,
f = c.height,
p = document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft;
// a.style.left = s + l + p + 'px';
a.style.left = s + p + 'px';
var d = document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
// a.style.top = u + f + d + Toast.count * 40 + 'px';
a.style.top = u + d + Toast.count * 40 + 'px';
setTimeout((function() {
a.className += ' out';
setTimeout((function() {
Toast.count--;
Toast.list.unshift();
Toast.list.forEach(function(v) {
v.style.top = (parseInt(v.style.top, 10) - 40) + 'px';
});
a.parentNode.removeChild(a);
}), 200);
}), 4e3);
o.appendChild(a);
document.body.appendChild(o);
var h = document.body.offsetWidth,
v = a.getBoundingClientRect().left,
m = a.offsetWidth;
if (h - m - v < 0) a.style.left = h - m - 10 + p + 'px';
Toast.count++;
Toast.list.push(a);
};
window.Lottery_join = function(lottery_list, room_id_list) {
lottery_list.forEach(function(short_id) {
if (short_id > 0) {
var room_id = room_id_list[short_id];
if (room_id > 0) {
SmallTV(room_id);
Raffle(room_id);
} else {
API.room.room_init(short_id).done(function(response) {
DEBUG('TaskLottery: room_init', response);
if (response.code === 0) {
room_id = response.data.room_id;
if (response.data.short_id > 0 && response.data.short_id != short_id) room_id_list[response.data.short_id] = room_id;
room_id_list[short_id] = room_id;
SmallTV(room_id);
Raffle(room_id);
}
});
}
}
});
return room_id_list;
};
function alertDialog(title, content) {
DOM.alertdialog.div_title_span.html(title);
DOM.alertdialog.div_content.html(content);
DOM.alertdialog.button_ok.click(function() {
$('#' + NAME + '_alertdialog').remove();
});
$('body > .link-popup-ctnr').append(DOM.alertdialog.div_background);
}
function execUntilSucceed(callback, delay, period) {
setTimeout(function() {
if (!callback()) {
var p = period < 1 ? 200 : period;
execUntilSucceed(callback, p, p);
}
}, delay < 1 ? 1 : delay);
}
function tommorrowRun(callback) {
var t = new Date();
do {
t.setHours(t.getHours() + 1);
} while (t.getHours() !== 0);
setTimeout(callback, t.valueOf() - Date.now());
}
function removeBlankChar(str) {
return str.replace(/(\s|\u00A0)+/, '');
}
function addCSS(context) {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = context;
document.getElementsByTagName('head')[0].appendChild(style);
}
function recurLoadConfig(cfg) {
for (var item in cfg) {
var e = $('#' + NAME + '_config_' + item);
if (e[0]) {
switch (typeof cfg[item]) {
case 'string':
case 'number':
e.val(cfg[item]);
break;
case 'boolean':
e.prop('checked', cfg[item]);
if (e.is(':checked')) {
$('#' + NAME + '_config_' + CONFIG_CONTROL_LIST[DOMtoItem(e)]).show();
} else {
$('#' + NAME + '_config_' + CONFIG_CONTROL_LIST[DOMtoItem(e)]).hide();
}
break;
case 'object':
if (Array.isArray(cfg[item])) e.val(cfg[item].join(','));
else recurLoadConfig(cfg[item]);
break;
}
}
}
}
function recurSaveConfig(config) {
var cfg = JSON.parse(JSON.stringify(config || CONFIG_DEFAULT));
if (typeof cfg !== 'object') return cfg;
for (var item in cfg) {
var e = $('#' + NAME + '_config_' + item);
if (e[0]) {
switch (typeof cfg[item]) {
case 'string':
cfg[item] = e.val() || '';
break;
case 'number':
cfg[item] = parseInt(e.val(), 10) || 0;
break;
case 'boolean':
cfg[item] = e.is(':checked');
break;
case 'object':
if (Array.isArray(cfg[item])) {
if (removeBlankChar(e.val()) === '') {
cfg[item] = [];
} else {
cfg[item] = removeBlankChar(e.val()).split(',');
}
} else {
cfg[item] = recurSaveConfig(cfg[item]);
}
break;
}
}
}
return cfg;
}
function loadConfig() {
try {
CONFIG = JSON.parse(localStorage.getItem(NAME + '_CONFIG')) || JSON.parse(JSON.stringify(CONFIG_DEFAULT));
if (typeof CONFIG !== 'object') {
CONFIG = JSON.parse(JSON.stringify(CONFIG_DEFAULT));
localStorage.setItem(NAME + '_CONFIG', JSON.stringify(CONFIG));
}
} catch (e) {
console.info('Bilibili直播间挂机助手读取配置失败');
// localStorage.removeItem(NAME + '_CONFIG');
// localStorage.removeItem('Bilibili-LiveRoom-HangHelper_CONFIG');
CONFIG = JSON.parse(JSON.stringify(CONFIG_DEFAULT));
localStorage.setItem(NAME + '_CONFIG', JSON.stringify(CONFIG));
}
DEBUG('loadConfig: CONFIG', CONFIG);
if (DOM.config.div_context) {
recurLoadConfig(CONFIG);
}
}
function saveConfig() {
if (DOM.config.div_context) {
CONFIG = recurSaveConfig(CONFIG_DEFAULT);
}
localStorage.setItem(NAME + '_CONFIG', JSON.stringify(CONFIG));
// DEBUG('saveConfig: CONFIG', CONFIG);
}
function DOMtoItem(element) {
return element.attr('id').replace(NAME + '_config_', '');
}
function DOMhelptoItem(element) {
return element.attr('id').replace(NAME + '_config_help_', '');
}
/*
window.BilibiliLive.ANCHOR_UID
window.BilibiliLive.COLORFUL_LOGGER
window.BilibiliLive.INIT_TIME
window.BilibiliLive.RND === window.DANMU_RND
window.BilibiliLive.ROOMID
window.BilibiliLive.SHORT_ROOMID
window.BilibiliLive.UID
window.captcha_key
window.$b
*/
function Init() {
if (!API) {
toast('BilibiliAPI初始化失败,脚本已停用!', 'error');
console.error('BilibiliAPI初始化失败,脚本已停用!');
return;
}
if (window.frameElement) {
console.info('已启用任意直播间抽奖!');
DEBUG('Init: window.frameElement', window.frameElement);
execUntilSucceed(function() {
if (window.BilibiliLive && parseInt(window.BilibiliLive.ROOMID, 10) !== 0) {
if (parseInt(window.BilibiliLive.UID, 10) !== 0) {
Info.short_id = window.BilibiliLive.SHORT_ROOMID;
Info.uid = window.BilibiliLive.UID;
Info.roomid = window.BilibiliLive.ROOMID;
Info.ruid = window.BilibiliLive.ANCHOR_UID;
Info.rnd = window.BilibiliLive.RND;
window.toast = window.top.toast;
window.parent.Lottery_join = window.Lottery_join;
DEBUG('Iframe: Init: Info', Info);
execUntilSucceed(function() {
if ($('.live-room-app.p-relative')[0]) {
document.getElementsByTagName('head')[0].innerHTML = '';
$('.live-room-app.p-relative').remove();
}
}, 9e3, 3e3);
}
return true;
}
}, 1, 500);
return;
}
console.info('Bilibili直播间挂机助手: 已加载');
InitAlertDialogGui();
InitConfigGui();
loadConfig();
saveConfig();
execUntilSucceed(function() {
DEBUG('Init: BilibiliLive', window.BilibiliLive);
if (timediff === 0 && parseInt(window.BilibiliLive.INIT_TIME, 10) !== 0) {
timediff = window.BilibiliLive.INIT_TIME - Date.now();
DEBUG('Init: timediff', timediff);
}
if (window.BilibiliLive && parseInt(window.BilibiliLive.ROOMID, 10) !== 0) {
if (parseInt(window.BilibiliLive.UID, 10) !== 0) {
Info.short_id = window.BilibiliLive.SHORT_ROOMID;
Info.uid = window.BilibiliLive.UID;
Info.roomid = window.BilibiliLive.ROOMID;
Info.ruid = window.BilibiliLive.ANCHOR_UID;
Info.rnd = window.BilibiliLive.RND;
room_id_list[Info.short_id] = Info.roomid;
if (CONFIG.USE_AWARD) {
execUntilSucceed(function() {
var _treasure_box = $('#gift-control-vm div.treasure-box.p-relative');
if (_treasure_box[0]) {
_treasure_box.attr('id', 'old_treasure_box');
_treasure_box.hide();
DOM.treasure.div = $('