// ==UserScript==
// @name 虎牙直播功能增强
// @namespace https://gitee.com/Kaiter-Plus/TampermonkeyScript/tree/master/虎牙直播功能增强
// @author Kaiter-Plus
// @description 给虎牙直播添加额外功能
// @version 0.3
// @match *://*.huya.com/\w*
// @icon https://www.huya.com/favicon.ico
// @noframes
// @grant none
// @grant GM_setValue
// @grant GM_getValue
// @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 添加 “自动选择最高画质” 功能,并同时提供配置开关
// @downloadURL none
// ==/UserScript==
;(function () {
'use strict'
// 定时器
let timer = null
// 判断镜像状态
let isReverse = false
// 直播界面容器
let container = null
// 直播播放器
let liveVideoNode = null
// 最高画质
let hightestImageQuality = null
// 获取是否自动选择最高画质
let isSelectedHightestImageQuality = GM_getValue('isSelectedHightestImageQuality')
// 获取是否自动网页全屏
// let 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 = setInterval(() => {
if (!!document.getElementById('player-ctrl-wrap')) {
container = document.getElementById('player-ctrl-wrap')
liveVideoNode = document.getElementById('hy-video')
hightestImageQuality = document.querySelector('.player-videotype-list').children[0]
initTools()
clearInterval(timer)
}
}, 1000)
}
// 初始化图标样式
function initStyle() {
let style = document.createElement('style')
style.innerHTML = `
.video-tools-icon {
position: absolute;
top: 11px;
}
.video-tools-icon .icon {
fill: currentColor;
color: #b2b4b4;
}
.video-tools-icon:hover .icon {
fill: currentColor;
color: #fd9400;
}
`
document.head.appendChild(style)
}
// 初始化工具
function initTools() {
insertIcon()
if (isSelectedHightestImageQuality) {
selectedHightestImageQuality()
}
}
// 插入图标
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() {
const buffered = liveVideoNode.buffered
if (buffered.length == 0) {
// 暂停中
return
}
liveVideoNode.currentTime = buffered.end(0)
}
// 镜像画面功能
function setVideoRev() {
liveVideoNode.style.transformOrigin = 'center center'
if (isReverse) {
liveVideoNode.style.transform = 'rotateY(0deg)'
isReverse = false
} else {
liveVideoNode.style.transform = 'rotateY(180deg)'
isReverse = true
}
}
// 选择最高画质
function selectedHightestImageQuality() {
if (hightestImageQuality.className === 'on') return
hightestImageQuality.click()
let playTimer = setInterval(() => {
if (document.querySelector('.player-play-btn')) {
document.querySelector('.player-play-btn').click()
document.querySelector('#player-tip-pause').remove()
clearInterval(playTimer)
}
}, 500)
}
function switchSelectedHIQ() {
location.reload()
isSelectedHightestImageQuality
? GM_notification('已关闭自动选择最高画质')
: GM_notification('已开启自动选择最高画质')
GM_setValue('isSelectedHightestImageQuality', !isSelectedHightestImageQuality)
}
// 添加菜单配置项
GM_registerMenuCommand('自动选择最高画质开关', switchSelectedHIQ)
// 初始化图标样式
initStyle()
// 初始化
init()
})()