B站看视频自动投币
// ==UserScript==
// @name B站看视频自动投币
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 如果当日投币经验没够50经验,则观看视频的时候自动投币点赞
// @author JKstring
// @match https://www.bilibili.com/video/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant none
// ==/UserScript==
(function() {
const STATE = window.__INITIAL_STATE__;
const MAXEXP = 50;
const EXPPERCOIN = 10;
//投币,aid视频id,multiply投币个数,select_like是否同时点赞
function payCoin(aid, multiply, selectLike) {
fetch("https://api.bilibili.com/x/web-interface/coin/add", {
"headers": {
"accept": "application/json, text/plain, */*",
"content-type": "application/x-www-form-urlencoded",
},
"body": `aid=${aid}&multiply=${multiply}&select_like=${selectLike}&cross_domain=true&eab_x=2&ramval=0&source=web_normal&ga=1&csrf=${getCookie("bili_jct")}`,
"method": "POST",
"mode": "cors",
"credentials": "include"
});
}
async function getTodayEXP() {
const response = await fetch("https://api.bilibili.com/x/web-interface/coin/today/exp", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
},
"method": "GET",
"mode": "cors",
"credentials": "include"
})
const data = await response.json();
return data;
}
async function fetchRelation(aid, bvid) {
const url = `https://api.bilibili.com/x/web-interface/archive/relation?aid=${aid}&bvid=${bvid}`;
const headers = {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
};
const response = await fetch(url, {
headers,
method: "GET",
mode: "cors",
credentials: "include"
});
const data = await response.json();
return data;
}
async function autoPayCoin() {
//判断今日50经验是否圆满
const todayExpData = await getTodayEXP();
if(todayExpData.code != 0 || todayExpData.data>=MAXEXP)
{//请求经验值失败或者当天经验大于50就取消自动
return;
}
//判断当前视频是否已经投币过了
const relation = await fetchRelation(STATE.aid, STATE.bvid);
if(relation.data.coin>0)
{
//已经投币过了,似乎没啥要处理的,需要再加
}else{
//进行投币操作,先进行判断剩余经验适合投2币20经验还是1币10经验
const canPayCoin = STATE.videoData.copyright === 1 ? 2 : 1;
const canGetExp = MAXEXP - todayExpData.data;
const payCoinNum = Math.min((canGetExp/EXPPERCOIN), canPayCoin);
payCoin(STATE.aid,payCoinNum,0);
console.log("投币完成:"+STATE.aid+"||"+payCoinNum);
}
}
autoPayCoin();
})();