b站“視頻評(píng)論區(qū)”抽獎(jiǎng) 講解(含JS源碼)
新前言
因?yàn)?b站升級(jí)原因,原來(lái)評(píng)論的頁(yè)數(shù)也變成了無(wú)限下拉加載的形式。導(dǎo)致舊版程序無(wú)法使用。 新版本的程序簡(jiǎn)單重構(gòu)了一下,可參考下面的 2.0版本 新代碼,使用方法一樣。
舊前言
如有 動(dòng)態(tài)評(píng)論區(qū) 抽獎(jiǎng)需要,可以參考:[JS實(shí)現(xiàn)b站動(dòng)態(tài)抽獎(jiǎng)“公平”方案——?jiǎng)討B(tài)+轉(zhuǎn)發(fā)]
因?yàn)檗D(zhuǎn)發(fā)抽獎(jiǎng)只支持小人數(shù),如果人少,可以使用:[JS實(shí)現(xiàn)b站動(dòng)態(tài)轉(zhuǎn)發(fā)抽獎(jiǎng)(小人數(shù))新方案講解]? [B站動(dòng)態(tài)轉(zhuǎn)發(fā)抽獎(jiǎng)腳本+教程]
視頻講解

正文
1.打開(kāi)視頻頁(yè)面

2.鼠標(biāo)右鍵打開(kāi)“檢查”工具 或 按F12

代碼如下,復(fù)制粘貼進(jìn)入“console”,然后回車
// 獲取時(shí)間
function get_date() {
? ? var date = new Date();
????var h = date.getHours();
???? var m = date.getMinutes();
???? var s = date.getSeconds();
???? h = h < 10 ? ('0' + h) : h;
???? m = m < 10 ? ('0' + m) : m;
???? s = s < 10 ? ('0' + s) : s;
? ? var currentDate = "[" + h + ":" + m + ":" + s + "] ";
? ? return currentDate;
}
// 從map獲取下標(biāo)為index的鍵
function get_map_key(map, index)
{
???? var i = 0;
???? for (var [key, value] of map) {
???????? if(i == index)
???????? {
???????? ???? return key;
???????? }
???????? i++;
???? }
}
// 從map獲取下標(biāo)為index的值
function get_map_value(map, index)
{
???? var i = 0;
???? for (var [key, value] of map) {
???????? if(i == index)
???????? {
???????? ???? return value;
???????? }
???? ???? i++;
???? }
}
// 遍歷map
function get_map(map)
{
???? for (var [key, value] of map) {
???????? console.log(key + " = " + value);
???? }
}
console.log(get_date() + "程序開(kāi)始運(yùn)行");
console.log(get_date() + "定義圖存儲(chǔ)數(shù)據(jù)(自動(dòng)去重)");
let name_map = new Map();
let id_map = new Map();
console.log(get_date() + "開(kāi)始載入數(shù)據(jù)");
var page = 1;
var my_loop;
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
???? for(var i=0; i<document.getElementsByClassName("list-item reply-wrap").length; i++)
???? {
???????? var name = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
???????? var id = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
???????? //console.log(name+",加入圖");
???????? name_map.set(name, page);
???????? id_map.set(id, page);
???? }
????console.log(get_date() + "第"+page+"頁(yè)數(shù)據(jù)存入Map完畢");
???? if(null != document.getElementsByClassName("next")[0])
???? {
???????? page++;
???????? //console.log("自動(dòng)翻頁(yè)...");
???????? document.getElementsByClassName("next")[0].click();
???????? //return true;
???? }
???? else
???? {
???????? console.log(get_date() + "全部數(shù)據(jù)加載完畢");
???????? console.log(get_date() + "總共" + name_map.size + "名用戶");
???????? // 生成隨機(jī)數(shù),直接打印中獎(jiǎng)?wù)咝畔?/span>
???????? var lucky_num = parseInt(Math.random()*(name_map.size),10);
???????? console.log(" ");
???????? console.log("中獎(jiǎng)用戶ID為:" + get_map_key(id_map, lucky_num));
???????? console.log("中獎(jiǎng)用戶名為:" + get_map_key(name_map, lucky_num));
???????? console.log("中獎(jiǎng)?wù)呶挥陧?yè):" + get_map_value(name_map, lucky_num));
???????? console.log(" ");
???????? console.log("程序運(yùn)行結(jié)束");
???????? clearInterval(my_loop);
???????? // 這就是注釋
???????? //return false;
???? }
}
// 定時(shí)調(diào)用函數(shù)
my_loop = setInterval(draw, 1500);

4.等待結(jié)果

ok 結(jié)束。

抽取多人版本
同理 控制臺(tái)貼入代碼,頁(yè)數(shù)加載完后?然后輸入 go() 回車即可
// 獲取時(shí)間
function get_date() {
? ? var date = new Date();
???? var h = date.getHours();
???? var m = date.getMinutes();
???? var s = date.getSeconds();
???? h = h < 10 ? ('0' + h) : h;
???? m = m < 10 ? ('0' + m) : m;
???? s = s < 10 ? ('0' + s) : s;
? ? var currentDate = "[" + h + ":" + m + ":" + s + "] ";
? ? return currentDate;
}
// 從map獲取下標(biāo)為index的鍵
function get_map_key(map, index)
{
???? var i = 0;
???? for (var [key, value] of map) {
???????? if(i == index)
???????? {
???????? ???? return key;
???????? }
???????? i++;
???? }
}
// 從map獲取下標(biāo)為index的值
function get_map_value(map, index)
{
???? var i = 0;
???? for (var [key, value] of map) {
???????? if(i == index)
???????? {
???????? ???? return value;
???????? }
???????? i++;
???? }
}
// 遍歷map
function get_map(map)
{
???? for (var [key, value] of map) {
???? ???? console.log(key + " = " + value);
???? }
}
console.log(get_date() + "程序開(kāi)始運(yùn)行");
console.log(get_date() + "定義圖存儲(chǔ)數(shù)據(jù)(自動(dòng)去重)");
let name_map = new Map();
let id_map = new Map();
console.log(get_date() + "開(kāi)始載入數(shù)據(jù)");
var page = 1;
var my_loop;
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
???? for(var i=0; i<document.getElementsByClassName("list-item reply-wrap").length; i++)
???? {
???????? var name = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
???????? var id = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
???????? //console.log(name+",加入圖");
???????? name_map.set(name, page);
???????? id_map.set(id, page);
???? }
???? console.log(get_date() + "第"+page+"頁(yè)數(shù)據(jù)存入Map完畢");
???? if(null != document.getElementsByClassName("next")[0])
???? {
???????? page++;
???????? //console.log("自動(dòng)翻頁(yè)...");
???????? document.getElementsByClassName("next")[0].click();
???????? //return true;
???? }
???? else
???? {
???????? console.log(get_date() + "全部數(shù)據(jù)加載完畢");
???????? console.log(get_date() + "總共" + name_map.size + "名用戶");
???????? clearInterval(my_loop);
???????? // 這就是注釋
???????? //return false;
???? }
}
function go()
{
???? var lucky_num = parseInt(Math.random()*(name_map.size),10);
????
???? console.log(" ");
???? console.log("中獎(jiǎng)用戶ID為:" + get_map_key(id_map, lucky_num));
???? console.log("中獎(jiǎng)用戶名為:" + get_map_key(name_map, lucky_num));
???? console.log("中獎(jiǎng)?wù)呶挥陧?yè):" + get_map_value(name_map, lucky_num));
???? console.log(" ");
}
// 定時(shí)調(diào)用函數(shù)
my_loop = setInterval(draw, 1500);

2.0 b站新版本評(píng)論區(qū) 多人
數(shù)據(jù)加載完畢后,使用 go(中獎(jiǎng)人數(shù)) 即可抽取
console.log("程序開(kāi)始運(yùn)行");
console.log("定義集合存儲(chǔ)數(shù)據(jù)");
let name_set = new Set();
let id_set = new Set();
console.log("開(kāi)始載入數(shù)據(jù)");
// 循環(huán)變量
var my_loop;
// 下滑延時(shí) 500毫秒 網(wǎng)速/加載速度較慢的朋友們最好放慢速度 提高準(zhǔn)確性
var r_time = 500;
// 評(píng)論數(shù)
var comment_num = 1;
if(document.getElementsByClassName("b-head-t")[0].innerText.indexOf("萬(wàn)") != -1)
{
comment_num = 10000 * (parseInt(document.getElementsByClassName("b-head-t")[0].innerText) + 1);
}
else
{
comment_num = parseInt(document.getElementsByClassName("b-head-t")[0].innerText);
}
// 下滑
function r()
{
window.scroll(0, 1920*comment_num);
// 沒(méi)有評(píng)論后自動(dòng)停止下滑 并 抽獎(jiǎng)
if(document.getElementsByClassName("loading-state")[0].innerText == "沒(méi)有更多評(píng)論")
{
// 停止下滑循環(huán)
stop_r();
// 抽獎(jiǎng)函數(shù)
draw();
}
}
// 停止下滑循環(huán)
function stop_r()
{
clearInterval(my_loop);
}
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
? ? var len = document.getElementsByClassName("con").length;
for(var i=0; i<len; i++)
{
var name = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
var id = document.getElementsByClassName("list-item reply-wrap")[i].getElementsByClassName("con")[0].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
// console.log(name+",加入集合");
name_set.add(name);
id_set.add(id);
}
console.log("全部數(shù)據(jù)加載完畢");
console.log("總共"+name_set.size+"名用戶");
// 這就是注釋
//return false;
}
// 獲取幸運(yùn)兒
function go(num)
{
for(var i=0; i<num; i++)
{
// 生成隨機(jī)數(shù),直接打印中獎(jiǎng)?wù)咝畔?/span>
var lucky_num = parseInt(Math.random()*(name_set.size), 10);
console.log(" ");
console.log("中獎(jiǎng)用戶ID為:"+Array.from(id_set)[lucky_num]);
console.log("中獎(jiǎng)用戶名為:"+Array.from(name_set)[lucky_num]);
console.log(" ");
}
}
// 開(kāi)始自動(dòng)下滑 r_time毫秒一次
my_loop = setInterval(r, r_time);
// 全部數(shù)據(jù)加載完畢后,使用 go(中獎(jiǎng)數(shù)) 抽取中獎(jiǎng)?wù)?/span>