// ==UserScript==
// @name 虎牙直播功能增强
// @namespace https://gitee.com/Kaiter-Plus/TampermonkeyScript/tree/master/虎牙直播功能增强
// @author Kaiter-Plus
// @description 给虎牙直播添加额外功能
// @version 0.2
// @match *://*.huya.com/\w*
// @icon https://www.huya.com/favicon.ico
// @grant none
// @run-at document-end
// @note 2020/12/10 给虎牙直播添加同步时间功能
// @note 2020/12/28 给虎牙直播添加画面镜像功能
// @note 2021/01/29 代码重构
// @downloadURL none
// ==/UserScript==
;(function () {
'use strict'
// 定时器
let timer = null
// 判断镜像状态
let isReverse = false
// 直播界面容器
let container = null
// 直播播放器
let liveVideoNode = 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 init() {
timer = setInterval(() => {
if (!!document.getElementById('player-ctrl-wrap')) {
container = document.getElementById('player-ctrl-wrap')
liveVideoNode = document.getElementById('hy-video')
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()
}
// 插入图标
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
}
}
// 初始化图标样式
initStyle()
// 初始化
init()
})()