'use strict';
// ==UserScript==
// @name 虎牙Plus
// @namespace http://tampermonkey.net/
// @icon https://www.google.com/s2/favicons?domain=huya.com
// @version 1.0.14
// @description 虎牙自动领取任务经验、开宝箱,复制直播流链接,简化页面,去广告, 夜间模式,自动进入剧场模式
// @author Francis
// @match *://*.huya.com/*
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// @license MIT
// ==/UserScript==
let $;
function addUi(){
let style = document.createElement('style');
style.appendChild(document.createTextNode(`
span.copy-stream-link:after {
display: none;
position: absolute;
content: "";
right: -20px;
top: 6px;
width: 18px;
height: 18px;
overflow: hidden;
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA4RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZ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J767dsshwv1gwGm3YstUwav3ltT0ufDea3eZNSeO2ATQrAdNUr2fXVDJM8j3yFZC76Q6sCk6AZ58vQ7/5i1u7OuYkZMQX4aD4JASzbRBq2i7h4Pq8hdvIuRzECxQdugUKd1ZB7PIdEKgIg/ztD/nfzizMSS6HQwkl82a2GQs8b78iZEaKEDy8t0ZlQmZSGci5gH+7WB4EedseQH33fdgVfYIfkFPTDismSCkMTRhB7LYwsRPfTQOWLrDOmPkldVUAFwjZqhseveegTl8OvWOfxNLBREva7a1laOIb1OiK+GwTU0PPI+gafislmQ1k2CTUSilNCUBZJ6SOwXpo6q2WunuayPCVrx5/pn6gaRGMWfth4bIbQGu4K3bJu+o1Gb5jN7WgaP89bruI++rn/H+UkS86r+PRZZdqRh6NHBvePbHetKz0TWnGJK1B4zYACSIPh/zw2Rh60LEjLtrXG/bZSdBjclhsQ6AbeLkYs1akmCbnvA9nkVN0BIq9OWEfgZa+p4sxo5gFzMPtxtcjeTQRP95UdhZTL1TTaJFcKTOVOLNcFtNn1aZlt3/rEsxaWQyt1LqUlkCNnEOMizAysnfUrssotfKmyrmSVdEHkePs7tzgUeobWalPafteqOJ26q8AAwB8rQG0tt5ioQAAAABJRU5ErkJggg==);
background-size: 18px 18px;
}
span.copy-stream-link.copy-success:after {
display: block;
}
.huya-plus-btn{
display: block;
padding:0 10px;
color:#b08444;
background:#FFD29E;
border-radius:15px;
margin-left: 30px;
user-select: none;
transition:all .5s;
}
.huya-plus-btn:hover{
color:#FFF;
background:#ffa801;
}
#huya-ab,
.player-banner-gift,
#player-marquee-wrap,
.room-gg-chat,
.room-mod-ggTop,
#hy-nav-download,
.hy-nav-kaibo,
.hy-nav-item:nth-child(2),
#J_roomGameBuy,
.jump-to-phone,
#week-star-btn,
.g-gift,
#J_bigStreamerStage,
#J_hySide
{
display:none !important;
}
body,
.duya-header-wrap,
#main_col,
.room-hd-l,
.player-gift-wrap,
.chat-room__ft,
.jspPane,
#J_profileNotice>div,
.week-rank__btn,
.J_msg,
.chat-room__list,
.msg-nobleEnter,
.msg-nobleEnter>div,
.msg-nobleSpeak,
.player-face-arrow,
#player-gift-tip,
.jspVerticalBar,
.illegal-report,
.subscribe-hd.sub-on,
.huya-plus-btn,
#player-gift-tip bottom,
#player-gift-tip btn,
.fansBadge-box,
.nav-expand-list,
.tt-user-card,
.share-entrance,
.search-suggest,
.u-links,
.entrance-expand,
.gameBuy-bd,
.guide-to-app,
.chat-room__wrap,
#J_profileNotice,
.msg-onTVLottery,
.room-core,
.msg-noble,
#J_box_msgOfKing,
.msg-of-king,
.subscribe-hd.sub-on,
.huya-plus-btn,
.nav-expand-game dd a,
.subscribe-hd.sub-on,
.huya-plus-btn,
.match-item,
.hy-nav-link,
.hy-nav-title,
.nav-user-title,
#J_roomTitle,
.msg,
.subscribe-hd.sub-on,
.huya-plus-btn,
.cont-item,
.week-rank__btn,
.week-rank-name,
.msg-nobleEnter,
.peo-name,
.search-item,
.history-bd .new-clickstat,
.from,
.to,
.nav-expand-game dd a,
.hy-header-match-preview-name,
#pub_msg_input,
#search-bar-input:focus,
.msg-noble,
#J_box_msgOfKing,
#J_hyUserCard .u-assets,
.room-sidebar,
.duya-header-wrap,
.week-rank__unit,
.chat-room__input,
.chatNotice,
#J_profileNotice,
.plaer-face-icon-bg,
.chat-room__ft__chat,
#tipsOrchat,
.week-rank__btn.active,
#pub_msg_input,
#search-bar-input,
.week-rank__bd li,
.subscribe-live-item,
.subscribe-live-item .txt .msg-row .nick,
.list-hd .title,
.match_body_wrap
{
transition: background .3s, background-color .3s, color .3s, border-color .3s;
}
body.night-mode,
.night-mode .duya-header-wrap,
.night-mode #main_col,
.night-mode .room-hd-l,
.night-mode .player-gift-wrap,
.night-mode .chat-room__ft,
.night-mode .jspPane,
.night-mode #J_profileNotice>div,
.night-mode .week-rank__btn,
.night-mode .J_msg,
.night-mode .chat-room__list,
.night-mode .msg-nobleEnter,
.night-mode .msg-nobleEnter>div,
.night-mode .msg-nobleSpeak,
.night-mode .player-face-arrow,
.night-mode #player-gift-tip,
.night-mode .jspVerticalBar,
.night-mode .illegal-report,
.night-mode .subscribe-hd.sub-on,
.night-mode .huya-plus-btn,
.night-mode #player-gift-tip bottom,
.night-mode #player-gift-tip btn,
.night-mode .fansBadge-box,
.night-mode .nav-expand-list,
.night-mode .tt-user-card,
.night-mode .share-entrance,
.night-mode .search-suggest,
.night-mode .u-links,
.night-mode .entrance-expand,
.night-mode .gameBuy-bd,
.night-mode .guide-to-app,
.night-mode .chat-room__wrap,
.night-mode #J_profileNotice,
.night-mode .msg-onTVLottery,
.night-mode .room-core,
.night-mode .msg-noble,
.night-mode .match_body_wrap
{
background-color: rgb(47, 48, 53) !important;
}
.night-mode #J_box_msgOfKing,
.night-mode .msg-of-king
{
background: rgb(47, 48, 53) !important;
}
.night-mode .subscribe-hd.sub-on,
.night-mode .huya-plus-btn,
.night-mode .nav-expand-game dd a,
.night-mode .subscribe-live-item
{
background-color: #464646 !important;
}
.night-mode .subscribe-hd.sub-on,
.night-mode .huya-plus-btn,
.night-mode .match-item
{
color: #8e8a8a !important;
}
.night-mode .hy-nav-link,
.night-mode .hy-nav-title,
.night-mode .nav-user-title,
.night-mode #J_roomTitle,
.night-mode .msg,
.night-mode .subscribe-hd.sub-on,
.night-mode .huya-plus-btn,
.night-mode .cont-item,
.night-mode .week-rank__btn,
.night-mode .week-rank-name,
.night-mode .msg-nobleEnter,
.night-mode .peo-name,
.night-mode .search-item,
.night-mode .history-bd .new-clickstat,
.night-mode .from,
.night-mode .to,
.night-mode .nav-expand-game dd a,
.night-mode .hy-header-match-preview-name,
.night-mode #pub_msg_input,
.night-mode #search-bar-input:focus,
.night-mode .msg-noble,
.night-mode #J_box_msgOfKing,
.night-mode #J_hyUserCard .u-assets,
.night-mode .follow-ctrl,
.night-mode .subscribe-live-item .txt .msg-row .nick,
.night-mode .list-hd .title
{
color: #E7E7E7 !important;
}
.night-mode .room-sidebar,
.night-mode .duya-header-wrap,
.night-mode .week-rank__unit,
.night-mode .chat-room__input,
.night-mode .chatNotice,
.night-mode #J_profileNotice,
.night-mode .plaer-face-icon-bg,
.night-mode .chat-room__ft__chat,
.night-mode #tipsOrchat
{
border-color: #3e3e3e !important;
}
.night-mode .week-rank__btn.active,
.night-mode #pub_msg_input,
.night-mode #J_weekRankList li:hover,
.night-mode #J_fansRankList li:hover,
.night-mode .seat-item:hover,
.night-mode #search-bar-input,
.night-mode .search-item:hover,
.night-mode .video-link:hover,
.night-mode .history-bd .new-clickstat:hover,
.night-mode .video-item:hover,
.night-mode .match-item:hover,
.night-mode .hy-header-match-preview li:hover,
.night-mode .week-rank__bd li:hover,
.night-mode .follow-ctrl
{
background-color: #565656 !important;
}
.night-mode .subscribe-live-item:hover{
box-shadow: 2px 2px 10px #565656 !important;
}
.night-mode-btn-wrapper{
float: left;
position: relative;
margin-left:15px;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.night-mode-switch-btn{
width: 50px;
border: 1px solid #e4e4e4;
height: 26px;
border-radius: 13px;
background-color:#ff8a00;
}
.night-mode-switch-dot{
width: 24px;
height: 24px;
border-radius: 12px;
background: #FFF;
line-height: 24px;
border:1px solid #b1b1b1;
text-align: center;
color: #6b6b6b;
transition: all .3s;
cursor: pointer;
user-select:none;
}
.night-mode .night-mode-switch-btn{
background-color:#8c8c8c;
}
.night-mode .night-mode-switch-dot{
transform: translateX(24px);
}
.huyaplus-page-full-mode #player-wrap{
height: 100% !important;
}
.huyaplus-page-full-mode #player-gift-wrap{
position: relative;
bottom: 0px !important;
transition: all .5s;
}
.huyaplus-page-full-mode #player-ctrl-wrap{
position: relative;
bottom: 0 !important;
transition: all .5s !important;
}
#player-ctrl-wrap.show, #player-gift-wrap.show{
bottom: 100px !important;
}
`));
document.head.appendChild(style);
let copyStreamLinkNode = document.createElement('div');
copyStreamLinkNode.style = "cursor: pointer;display:inline-flex;vertical-align:top;";
const openWithPlayerBtn = isMacOS() ? `<span class="huya-plus-btn open-with-iina" style="position: relative;">使用IINA打开</span>`: `<span class="huya-plus-btn open-with-potplayer" style="position: relative;">使用PotPlayer打开</span>`;
copyStreamLinkNode.innerHTML = `<span class="huya-plus-btn copy-stream-link" style="position: relative;">复制直播流</span>${openWithPlayerBtn}`;
document.querySelector('.host-title').appendChild(copyStreamLinkNode);
document.querySelector('.copy-stream-link').onclick = async e=>{
GM_setClipboard(await getStreamUrl());
showCopySuccessIcon();
};
$(".open-with-iina").click(async ()=>{
openStreamWithIINA(await getStreamUrl());
});
$(".open-with-potplayer").click(async ()=>{
openStreamWithPotPlayer(await getStreamUrl());
});
}
async function getStreamUrl(){
let url = window.location.href;
let streamUrl = sessionStorage.getItem(url)
if(streamUrl && streamUrl.length > 0){
return streamUrl;
}
streamUrl = await doGetStreamUrl(url);
if(streamUrl && streamUrl.length > 0){
sessionStorage.setItem(url,streamUrl);
return streamUrl;
}
alert("获取直播流失败");
throw new Error("获取直播流失败");
}
async function doGetStreamUrl(url){
try{
let mobileHtml = await new Promise((resolve,reject)=>{
GM_xmlhttpRequest({
method: 'GET',
headers: {
'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
},
url: url,
responseType: 'blob',
onload: resp=>{
resolve(resp.responseText);
},
onerror: e => {
reject(e);
}
})
});
let streamUrl = /\<script\>\s*var liveLineUrl = "(.+)";\s*\<\/script\>/.exec(mobileHtml)[1];
streamUrl = streamUrl.replace(/(_\d+)(.m3u8)/, '$2');
return `https:${streamUrl}`;
}catch(e){
alert("获取直播流链接失败,房间未开播或刷新页面后重试")
throw e;
}
}
function isMacOS(){
var UserAgent = navigator.userAgent.toLowerCase();
return /mac os/.test(UserAgent);
}
function showCopySuccessIcon(){
$('span.copy-stream-link').addClass('copy-success');
setTimeout(()=>{$('span.copy-stream-link').removeClass('copy-success');},1000)
}
function autoReceiveBoxReward(){
let rewardBtns = $(".player-box-list .player-box-stat3").filter((i,it)=>$(it).css("visibility") === 'visible');
if(rewardBtns.size() > 0){
let btn = $(rewardBtns[0]);
btn.click();
let waitComplete = ()=>{
if(btn.css("visibility") === 'hidden'){
$("#player-box").hide();
console.log("开启宝箱");
autoReceiveBoxReward();
} else {
setTimeout(waitComplete,1000);
}
};
setTimeout(waitComplete,1000);
}
}
function autoReceiveTaskXP(){
let taskBtns = $(".tasks .status .status-get");
if(taskBtns.size() > 0){
$.each(taskBtns,(_,btn)=>{
$(btn).click();
});
console.log(`自动领取经验,本次完成${taskBtns.length}个任务`)
}
}
function cleanPage(){
$(".room-gg-chat").remove();
$(".room-footer").remove();
}
function completeChatTask(){
const taskInfo = $(".tasks .tit").filter((i,it)=>$(it).text() === '在1场直播中发送弹幕');
const taskDone = taskInfo.siblings(".status").find(".status-ing").size() === 0;
if(!taskDone){
chat('66666')
}
}
function chat(msg){
$("#pub_msg_input").val(msg).keyup();
$("#msg_send_bt").click();
}
function openStreamWithPotPlayer(streamUrl){
openStreamWithPlayer("PotPlayer://", streamUrl);
}
function openStreamWithIINA(streamUrl){
openStreamWithPlayer("iina://weblink?url=", streamUrl)
}
function openStreamWithPlayer(playerUrlSchema, streamUrl){
window.open(`${playerUrlSchema}${streamUrl}`, "_self")
}
function jqueryLoaded(){
$ = unsafeWindow.$
return $;
}
function pageLoaded(){
if($(".tasks .status").size() == 0){
$(".nav-user-title").mouseenter()
}
return $(".box-icon-word").size() > 0 && $(".tasks .status").size() > 0
}
async function waitLoad(conditionFunc){
return new Promise(function(resolve,reject){
let w = ()=>{
if(conditionFunc()){
resolve();
} else {
setTimeout(w,1000);
}
}
w();
});
}
function nightMode(){
$("#J_global_user_tips").before(`
<div class="night-mode-btn-wrapper">
<div class="night-mode-switch-btn">
<div class="night-mode-switch-dot">日</div>
</div>
</div>
`)
let switchDay = ()=>{
$(".night-mode-switch-dot").text("日");
document.body.classList.remove('night-mode');
localStorage.setItem("night-mode",false);
}
let switchNight = ()=>{
$(".night-mode-switch-dot").text("夜");
document.body.classList.add('night-mode');
localStorage.setItem("night-mode",true);
}
$(".night-mode-switch-btn").click(()=>{
if(document.body.classList.contains("night-mode")){
switchDay();
} else {
switchNight();
}
})
if(localStorage.getItem("night-mode") === 'true'){
switchNight();
}
}
// 自动领取礼物掉落的宝箱
let receiveTimer;
function autoReceiveTreasure(){
if($("#J_treasureChestContainer .btn").size() === 0){
return;
}
let alreadyChat = false;
let receive = ()=>{
if($("#J_treasureChestContainer .btn.usable").size() > 0){
$("#J_treasureChestContainer .btn.usable").click()
clearInterval(receiveTimer);
receiveTimer = undefined;
} else if($("#J_treasureChestContainer .btn").size() > 0) {
if(!alreadyChat){
chat('666')
alreadyChat = true;
}
} else {
clearInterval(receiveTimer);
receiveTimer = undefined;
}
}
if(!receiveTimer){
receiveTimer = setInterval(receive,1000);
}
}
function addEventListener(){
$("#player-fullpage-btn").click(()=>{
if($(".player-narrowpage").size() > 0){
$("#videoContainer").addClass("huyaplus-page-full-mode");
} else {
$("#videoContainer").removeClass("huyaplus-page-full-mode");
$("#player-ctrl-wrap, #player-gift-wrap").removeClass("show");
}
})
$("#player-fullscreen-btn").click(()=>{
if($(".player-narrowscreen").size() > 0 && $(".player-narrowpage").size() > 0){
console.log("add")
$("#videoContainer").removeClass("huyaplus-page-full-mode");
$("#player-ctrl-wrap, #player-gift-wrap").removeClass("show");
} else if($(".player-narrowpage").size() > 0){
console.log("remove")
$("#videoContainer").addClass("huyaplus-page-full-mode");
}
})
$("#player-video").dblclick(()=>{
if($(".player-narrowpage").size() > 0){
$("#videoContainer").addClass("huyaplus-page-full-mode");
} else {
$("#videoContainer").removeClass("huyaplus-page-full-mode");
$("#player-ctrl-wrap, #player-gift-wrap").removeClass("show");
}
})
let hideTimeout;
$(".room-player").on("mousemove",".huyaplus-page-full-mode", ()=>{
$("#player-ctrl-wrap, #player-gift-wrap").addClass("show");
clearTimeout(hideTimeout);
hideTimeout = setTimeout(()=>{
$("#player-ctrl-wrap, #player-gift-wrap").removeClass("show");
},2000);
}).on("mouseleave",".huyaplus-page-full-mode", ()=>{
$("#player-ctrl-wrap, #player-gift-wrap").removeClass("show");
})
}
let timer,treasureTimer;
(async function() {
await waitLoad(jqueryLoaded);
cleanPage();
nightMode();
addUi();
if(treasureTimer) clearInterval(treasureTimer)
treasureTimer = setInterval(autoReceiveTreasure, 30000)
await waitLoad(pageLoaded);
addEventListener();
$(".player-fullpage-btn").click();
completeChatTask();
let intervalInMills = 60 * 1000;
let task = ()=>{
autoReceiveBoxReward();
autoReceiveTaskXP();
};
task();
if(timer) clearInterval(timer);
timer = setInterval(task,intervalInMills);
})();