// ==UserScript==
// @name 虎牙直播功能增强
// @namespace https://gitee.com/Kaiter-Plus/TampermonkeyScript/tree/master/虎牙直播功能增强
// @author Kaiter-Plus
// @description 给虎牙直播添加额外功能
// @version 0.7
// @match *://*.huya.com/*
// @icon https://www.huya.com/favicon.ico
// @noframes
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @grant GM_notification
// @grant GM_registerMenuCommand
// @run-at document-end
// @note 2020/12/10 添加 “同步时间” 功能
// @note 2020/12/28 添加 “画面镜像” 功能
// @note 2021/01/29 代码重构
// @note 2021/03/01 添加 “自动选择最高画质” 功能,并同时提供配置开关,默认关闭
// @note 2021/03/02 添加 “自动选领取百宝箱奖励” 功能,并同时提供配置开关,默认关闭
// @note 2021/03/03 修改 更改配置时为不用重载界面
// @note 2021/03/04 修复了一个小 bug
// @note 2021/03/08 修复了最后两个宝箱不会领取的 bug
// @downloadURL none
// ==/UserScript==
;(function () {
'use strict'
// 判断镜像状态
let isReverse = false
// 定时器
const timer = {
initTimer: null,
playTimer: null,
chestTimer: null
}
// 直播界面容器
let container = null
// 最高画质
let hightestImageQuality = null
// 所有宝箱
let chests = null
// 配置选项
const config = {
// 获取是否自动选择最高画质
isSelectedHightestImageQuality: GM_getValue('isSelectedHightestImageQuality'),
// 是否自动领取宝箱
isGetChest: GM_getValue('isGetChest')
// 获取是否自动网页全屏
// isFullScreen: GM_getValue('isFullScreen')
}
// 创建功能图标
function createTagIcon(option) {
const tag = document.createElement(option.tagName)
tag.id = option.id
tag.className = option.className
tag.title = option.title
tag.innerHTML = option.innerHTML
tag.style = option.style
tag.addEventListener('click', () => {
option.eventListener()
})
return tag
}
// 初始化
function init() {
timer.initTimer = setInterval(() => {
if (!container || chests.length <= 0) {
container = document.getElementById('player-ctrl-wrap')
chests = document.querySelectorAll('#player-box .player-box-list li')
} else {
hightestImageQuality = document.querySelector('.player-videotype-list').children[0]
initStyle()
initTools()
clearInterval(timer.initTimer)
}
}, 1000)
}
// 初始化图标样式
function initStyle() {
GM_addStyle(`
.video-tools-icon {
position: absolute;
top: 11px;
}
.video-tools-icon .icon {
fill: currentColor;
color: #b2b4b4;
}
.video-tools-icon:hover .icon {
fill: currentColor;
color: #fd9400;
}
`)
}
// 初始化工具
function initTools() {
insertIcon()
config.isSelectedHightestImageQuality ? selectedHightestImageQuality() : null
config.isGetChest ? getChest() : null
}
// 插入图标
function insertIcon() {
// 同步时间图标
const sync = createTagIcon({
tagName: 'div',
id: 'ex-videosync',
className: 'video-tools-icon',
title: '同步时间',
innerHTML: `
`,
style: 'left: 96px;',
eventListener: setVideoSync
})
// 镜像图标
const rev = createTagIcon({
tagName: 'div',
id: 'ex-videoReverse',
className: 'video-tools-icon',
title: '镜像画面',
innerHTML: `
`,
style: 'left: 134px;',
eventListener: setVideoRev
})
container.insertBefore(sync, container.childNodes[3])
container.insertBefore(rev, container.childNodes[4])
}
// 同步时间功能
function setVideoSync() {
let videoNode = document.getElementById('hy-video')
const buffered = videoNode.buffered
if (buffered.length == 0) {
// 暂停中
return
}
videoNode.currentTime = buffered.end(0)
}
// 镜像画面功能
function setVideoRev() {
let videoNode = document.getElementById('hy-video')
videoNode.style.transformOrigin = 'center'
if (isReverse) {
videoNode.style.transform = 'rotateY(0deg)'
isReverse = false
} else {
videoNode.style.transform = 'rotateY(180deg)'
isReverse = true
}
}
// 选择最高画质功能
function selectedHightestImageQuality() {
if (hightestImageQuality.className === 'on') return
hightestImageQuality.click()
timer.playTimer = setInterval(() => {
if (document.querySelector('.player-play-btn')) {
document.querySelector('.player-play-btn').click()
document.querySelector('#player-tip-pause').remove()
clearInterval(timer.playTimer)
}
}, 500)
}
// 自动领取宝箱
function getChest() {
timer.chestTimer = setInterval(() => {
// 全部领取结束定时
const lastIndex = chests.length - 1
const lastTimer = chests[lastIndex].querySelector('.player-box-stat2')
const lastGet = chests[lastIndex].querySelector('.player-box-stat3')
if (lastTimer.style.visibility === 'hidden' && lastGet.style.visibility === 'hidden') {
clearInterval(timer.chestTimer)
return
} else {
// 遍历领取
for (const item of chests) {
let get = item.querySelector('.player-box-stat3')
if (get.style.visibility === 'visible') {
get.click()
document.querySelector('.player-chest-btn #player-box').style.display = 'none'
}
}
}
}, 5000)
}
// 功能切换
function switchSetting(settingConfig) {
config[settingConfig.key] = !config[settingConfig.key]
config[settingConfig.key] ? GM_notification(settingConfig.openString) : GM_notification(settingConfig.closeString)
GM_setValue(settingConfig.key, config[settingConfig.key])
if (!config[settingConfig.key]) {
clearInterval(settingConfig.timer)
} else {
settingConfig.execute()
}
}
function switchSelectedHIQ() {
switchSetting({
key: 'isSelectedHightestImageQuality',
openString: '已开启自动选择最高画质',
closeString: '已关闭自动选择最高画质',
timer: timer.playTimer,
execute: selectedHightestImageQuality
})
}
function switchGetChest() {
switchSetting({
key: 'isGetChest',
openString: '已开启自动领取百宝箱奖励',
closeString: '已关闭自动领取百宝箱奖励',
timer: timer.chestTimer,
execute: getChest
})
}
// 添加菜单配置项
GM_registerMenuCommand('自动选择最高画质开关', switchSelectedHIQ)
GM_registerMenuCommand('自动领取百宝箱奖励开关', switchGetChest)
// 初始化
init()
})()