// ==UserScript== // @name 虎牙直播功能增强 // @namespace https://gitee.com/Kaiter-Plus/TampermonkeyScript/tree/master/虎牙直播功能增强 // @author Kaiter-Plus // @description 给虎牙直播添加额外功能 // @version 1.1 // @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 // @note 2021/03/10 紧急修复了宝箱不会领取的 bug // @note 2021/03/12 添加了脚本的配置选项 // @note 2021/03/13 优化自动领取百宝箱逻辑 // @downloadURL none // ==/UserScript== ;(function () { 'use strict' // 判断镜像状态 let isReverse = false // 定时器 const timer = { initTimer: null, hightestTimer: null, chestTimer: null } // 控制栏容器 let controlContainer = null // 直播界面容器 let container = null // 最高画质 let hightestImageQuality = null // 所有宝箱 let chests = null // 配置选项 const config = [ // 自动选择最高画质 GM_getValue('isSelectedHightestImageQuality'), // 自动领取百宝箱奖励 GM_getValue('isGetChest') // 获取是否自动网页全屏 // GM_getValue('isFullScreen') ] // 初始化 function init() { timer.initTimer = setInterval(() => { if (!container || chests.length <= 0) { controlContainer = document.querySelector('.duya-header-control') container = document.getElementById('player-ctrl-wrap') // 使用数组保存 chests = Array.from(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(` #J_global_user_tips{ display: none; } .video-tools-icon { position: absolute; top: 11px; } .video-tools-icon .icon { fill: currentColor; color: #b2b4b4; } .video-tools-icon:hover .icon { fill: currentColor; color: #ff9600; } .hy-header-style-normal .hy-nav-title svg { position: relative; top: -5px; left: 4px; fill: currentColor; color: #555; } .hy-header-style-normal .hy-nav-title:hover svg { fill: currentColor; color: #ff9600; } .reset-style { height: 63px!important; width: 220px!important; } .config-arrow { position: absolute; top: 28px; right: -12px; width: 9px; height: 5px; overflow: hidden; transition: transform .5s; background: url(https://a.msstatic.com/huya/main3/assets/img/header/sprite/arrow_93ea4.png) } .hy-header-style-normal .hy-nav-title:hover .config-arrow { transform: rotate(180deg); background: url(https://a.msstatic.com/huya/main3/assets/img/header/sprite/arrow_on_e7b62.png) } .config-item { padding: 5px 15px; display: flex; justify-content: space-around; } @media handheld, only screen and (max-width: 1440px) { .hy-nav-history { display: none; } } @media handheld, only screen and (max-width: 1721px) { .hy-nav-kaibo { visibility: hidden; } } /* 自定义开关 */ .switchButton { position: relative; width: 46px; z-index: 9999; user-select: none; } .switchButton-checkbox { display: none; } .switchButton-label { display: block; cursor: pointer; border: 2px solid #999999; border-radius: 20px; overflow: hidden; } .switchButton-inner { display: block; width: 200%; margin-left: -100%; overflow: hidden; transition: margin 0.3s ease-in 0s; } .switchButton-inner::before, .switchButton-inner::after { content: ""; display: block; float: right; width: 50%; padding: 0; height: 18px; font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; box-sizing: border-box; } .switchButton-switch { position: absolute; display: block; width: 14px; height: 14px; margin: 2px; background: #bbbbbb; top: 0; bottom: 0; right: 24px; border: 2px solid #584c3d; border-radius: 14px; transition: all 0.3s ease-in 0s; } .switchButton-checkbox:checked+.switchButton-label .switchButton-inner { margin-left: 0; } .switchButton-checkbox:checked+.switchButton-label .switchButton-switch { right: 0px; background: #ff9600; } `) } // 初始化工具 function initTools() { insertIcon() config[0] ? selectedHightestImageQuality() : null config[1] ? getChest() : null } // 创建功能图标 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 createConfigItem(configItems) { let str = '' for (const index in configItems) { str = str.concat(`
  • ${configItems[index]}
  • `) } return str } // 插入图标 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 }) // 插入配置选项 const settings = createTagIcon({ tagName: 'div', id: '', className: 'hy-nav-right nav-subscribe', title: '', innerHTML: ` 脚本设置 `, style: 'padding-left: 5px', eventListener: configSettings }) controlContainer.appendChild(settings) // 创建用于添加自定义功能图标的 fragment,避免多次回流(重排) const iconFragment = document.createDocumentFragment() iconFragment.appendChild(sync) iconFragment.appendChild(rev) container.insertBefore(iconFragment, container.childNodes[3]) } // 同步时间功能 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.hightestTimer = setInterval(() => { if (document.querySelector('.player-play-btn')) { document.querySelector('.player-play-btn')[0].click() document.querySelector('#player-tip-pause').remove() clearInterval(timer.hightestTimer) } }, 500) } // 自动领取宝箱 function getChest() { timer.chestTimer = setInterval(() => { if (chests.length > 0) { // 遍历领取 for (const item of chests) { // 是否已经领取 const wait = item.querySelector('.player-box-stat1').style.visibility const timer = item.querySelector('.player-box-stat2').style.visibility const get = item.querySelector('.player-box-stat3').style.visibility // 如果已经领取,删除节点 if (wait === 'hidden' && timer === 'hidden' && get === 'hidden') { chests.splice(chests.indexOf(item), 1) break } // 如果可以领取则领取,领取后删除节点 if (get === 'visible') { item.querySelector('.player-box-stat3').click() chests.splice(chests.indexOf(item), 1) document.querySelector('.player-chest-btn #player-box').style.display = 'none' break } } } else { clearInterval(timer.chestTimer) } }, 5000) } // 配置选项监听事件 function configSettings(e) { const target = e.target if (target.tagName.toLowerCase() === 'input') { // 选择最高画质 if (target.id === 'ON_OFF0') { switchFunction('isSelectedHightestImageQuality', target, selectedHightestImageQuality, timer.hightestTimer) } // 领取百宝箱奖励 if (target.id === 'ON_OFF1') { switchFunction('isGetChest', target, getChest, timer.chestTimer) } e.stopPropagation() } } // 配置选项功能 function switchFunction(key, target, fn, timer) { GM_setValue(key, target.checked) if (target.checked) { fn() } else { clearInterval(timer) } } // 初始化 init() })()