// ==UserScript==
// @author zhzLuke96
// @name 油管视频旋转
// @name:en youtube player rotate plug
// @version 0.2
// @description 油管的视频旋转插件.
// @description:en rotate youtebe player.
// @namespace https://github.com/zhzLuke96/
// @match https://www.youtube.com/watch?v=*
// @grant none
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
var $ = (...args) => document.querySelector.apply(document,args)
function css_load(text) {
var ret = {}
for (var row of text.split(";")) {
if (row != "") {
var t = row.split(":")
ret[t[0].trim()] = t[1].trim();
}
}
return ret
}
function css_dump(obj) {
var ret = ""
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
ret += `${key}:${obj[key]};`
}
}
return ret
}
var $ytp = $(".html5-main-video")
var $vc = $(".html5-video-player")
var state = 0
function rotate_vid() {
var css = css_load($ytp.style.cssText)
state = (state + 1) % 4
if (css["transform"] == undefined) state = 1
var deg = state * 90
var x = state % 2 == 0 ? $ytp.clientWidth : $ytp.clientHeight
var y = state % 2 == 1 ? $ytp.clientWidth : $ytp.clientHeight
css.left = ~~(($vc.clientWidth - x * (y / $vc.clientHeight)) / 2) + "px"
css["transform"] = `rotate(${deg}deg)`
css["transform"] += ` scale(${$vc.clientHeight / y})`
$ytp.style.cssText = css_dump(css)
}
function toggle_tans(el, repr) {
var css = css_load(el.style.cssText)
var t = css["transform"]
if (t) {
if (t.indexOf(repr) != -1) {
css["transform"] = t.replace(repr, "")
} else {
css["transform"] += " " + repr
}
} else {
css["transform"] = repr
}
el.style.cssText = css_dump(css)
}
function addbutton(html, options, onRight = true) {
var p, push;
if (onRight) {
p = $(".ytp-right-controls")
push = n => p.insertBefore(n, p.firstElementChild)
} else {
// left
p = $(".ytp-left-controls")
push = n => p.appendChild(n)
}
var b = $(".ytp-settings-button").cloneNode(true)
b.innerHTML = html
b.className = "ytp-button"
push(b)
if (options.click) b.addEventListener("click", options.click)
if (options.css) b.style.cssText = options.css
if (options.id) b.id = options.id
if (options.title) b.title = options.title
return void 0;
}
// rotate 90
addbutton(`
`, {
click: rotate_vid,
css: "fill:white;width:20px;margin-right:1rem;",
id: "rotate-btn",
title: "rotate"
})
// flip horizintal
addbutton(`
`, {
click() {
if ($ytp.style.cssText.indexOf("transform") == -1)state = 0
if (state % 2 == 1) toggle_tans($ytp, "rotateX(180deg)")
else toggle_tans($ytp, "rotateY(180deg)")
},
css: "fill:white;width:20px;margin-right:1rem;",
id: "horizintal-btn",
title: "horizintal"
})
// flip vertical
addbutton(`
`, {
click() {
if ($ytp.style.cssText.indexOf("transform") == -1)state = 0
if (state % 2 == 0) toggle_tans($ytp, "rotateX(180deg)")
else toggle_tans($ytp, "rotateY(180deg)")
},
css: "fill:white;width:10px;margin-right:1rem;",
id: "vertical-btn",
title: "vertical"
})
})();