// ==UserScript==
// @name u-Youtube
// @name:zh-CN 功夫制霸YouTube
// @name:zh-TW 功夫製霸YouTube
// @name:en u-Youtube
// @name:ua u-Youtube
// @name:ja カンフー支配YouTube
// @name:ko 쿵푸 지배YouTube
// @name:it u-Youtube
// @name:de u-Youtube
// @name:fr u-Youtube
// @namespace https://github.com/rasso1/u-Youtube
// @version 1.21
// @description make Youtube under ur control
// @description:zh-TW make Youtube under ur control
// @description:en make Youtube under ur control
// @description:ja あなたのコントロール下でYouTubeを作る
// @description:ko 유튜브를 당신의 통제하에 두십시오
// @description:it rendi youtube sotto il tuo controllo
// @description:de Machen Sie YouTube unter Ihrer Kontrolle
// @description:fr faire de youtube sous votre contrôle
// @author ok!
// @match https://www.youtube.com/*
// @match https://youtube.com/*
// @run-at document-start
// @contributionURL https://paypal.me/kongfuY
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var expand_description = 1;var subscription_up = 1;
var sec_sub,sec_fwd,wheel_sec,buffer_time,dark_mode,video_quality,video_speed,english_mode,comment_right,temp_buffer,temp_speed,speed_display,current_time;
var inner_hight=window.innerHeight;var inner_width=window.innerWidth*0.38;
var youtube_hand = {
message_box: {
last_message: null,
show: function(message) {
youtube_hand.message_box.last_message = message;
document.querySelector(".ytp-bezel-text").innerText = message;
document.querySelector(".ytp-bezel-text").parentNode.parentNode.style.display = "";
document.querySelector(".ytp-bezel-text").parentNode.parentNode.classList.remove("ytp-bezel-text-hide");
setTimeout(youtube_hand.message_box.hide, 2000, message);
},
hide: function(message_hide) {
if (message_hide != youtube_hand.message_box.last_message) {
return;
}
document.querySelector(".ytp-bezel-text").innerText = "";
document.querySelector(".ytp-bezel-text").parentNode.parentNode.style.display = "none";
}
},
dark_eye:function (){
if(localStorage.getItem('dark_mode')==1){
if(!document.querySelector("html").hasAttribute("dark")){
document.querySelector("html").setAttribute("dark",true);}
}
else if(document.querySelector("html").hasAttribute("dark"))
{document.querySelector("html").removeAttribute("dark")}
if(localStorage.getItem("comment_right")==1&&document.querySelector("#columns #primary #primary-inner #sections")&&document.querySelector("#secondary #secondary-inner")){
//评论右上移
document.querySelector("#secondary #secondary-inner").append(document.querySelector("#columns #primary #primary-inner #sections"));
document.querySelector("#secondary #secondary-inner #sections").style="width:"+inner_width+"px;height:"+inner_hight+"px;overflow-y:auto";
//related 左下移
document.querySelector("#columns #primary #primary-inner").append(document.querySelector("#secondary #secondary-inner #related"));
}
},
dark_eye1:function (){
if(localStorage.getItem('dark_mode')==1){
var style_type = document.createElement("style");style_type.id = "style_type";
style_type.innerHTML = `#end > div.dropdown-hover > div{font-size:12px;color:#bbb !important;background-color:#202020 !important;}
#end > div.dropdown-hover > button{background-color:#222 !important;} #end > div.dropdown-hover path{fill:transparent !important;stroke:white !important;}
#end > div.dropdown-hover > div > div{color:#aaa !important;background-color:#202020 !important;}
#end > div.dropdown-hover > div > div p{font-size:12px;color:#bbb !important;background-color:#202020 !important;}
.dropdown-hover button div{font-size:12px;color:#aaa !important;background-color:#333 !important;}
#sec_sub,#sec_fwd,#wheel_sec,#buffer_time{font-size:12px;color:#bbb !important;background-color:#333 !important;border-radius:5px;}`;
document.head.appendChild(style_type);
}
},
//播放设置
play_menu:function(){
var html_app = document.createElement("div");
html_app.innerHTML = `
`;
html_app.classList.add('dropdown-hover');
document.querySelector("#end").appendChild(html_app);
//
var speed_slider=document.getElementById("speed_slider"),speed_button=document.getElementById("speed_button"),
y=document.getElementById("sec_sub"),z=document.getElementById("wheel_sec"),x=document.getElementById("buffer_time"),w=document.getElementById("dark_mode"),
s=document.getElementById("video_quality"),v=document.getElementById("sec_fwd"),t=document.getElementById("english_mode"),c=document.getElementById("comment_to");
dark_mode = localStorage.getItem('dark_mode');comment_right = localStorage.getItem('comment_right');
var english_mode = localStorage.getItem('english_mode');
speed_display=document.getElementById("speed_display");
const mandarins = document.querySelectorAll('.mandarin');const englishs = document.querySelectorAll('.english');
video_speed = localStorage.getItem('speed');sec_fwd = localStorage.getItem('sec_fwd');sec_sub = localStorage.getItem('sec_sub');
wheel_sec = localStorage.getItem('wheel_sec');buffer_time = localStorage.getItem('buffer_time');video_quality = localStorage.getItem('video_quality');
english_mode = localStorage.getItem('english_mode');
//判读英文菜单
if(localStorage.getItem('english_mode')==1){
for (const mandarin of mandarins) {
mandarin.className -= ' invisible';
}
for (const english of englishs) {
english.className += ' invisible';
}
}
video_speed?speed_display.innerHTML=video_speed+"x":speed_display.innerHTML="1x";
setTimeout(function (){
if(video_speed&&document.querySelector('#movie_player'))
document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;},4000);
speed_slider.value = localStorage.getItem('speed')*25;
speed_slider.oninput=function(){localStorage.setItem('speed',this.value/25);video_speed = localStorage.getItem('speed');video_speed?speed_display.innerHTML=video_speed+"x":speed_display.innerHTML="1x";
document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
}
speed_button.onclick=function(){localStorage.setItem('speed',1);video_speed = localStorage.getItem('speed');video_speed?speed_display.innerHTML=video_speed+"x":speed_display.innerHTML="1x";
document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
speed_slider.value = localStorage.getItem('speed')*25;
}
sec_fwd?v.value=sec_fwd:v.value="";
v.onchange=function(){localStorage.setItem('sec_fwd',this.value);sec_fwd = localStorage.getItem('sec_fwd');sec_fwd?v.value=sec_fwd:v.value="";
}
sec_sub?y.value=sec_sub:y.value="";
y.onchange=function(){localStorage.setItem('sec_sub',this.value);sec_sub = localStorage.getItem('sec_sub');sec_sub?y.value=sec_sub:y.value="";
}
wheel_sec?z.value=wheel_sec:z.value="";
z.onchange=function(){localStorage.setItem('wheel_sec',this.value);wheel_sec = localStorage.getItem('wheel_sec');wheel_sec?z.value=wheel_sec:z.value="";
}
buffer_time?x.value=buffer_time:x.value="";
x.onchange=function(){localStorage.setItem('buffer_time',this.value);buffer_time = localStorage.getItem('buffer_time');buffer_time?x.value=buffer_time:x.value="";
}
c.checded=comment_right; if(comment_right!=1){document.getElementById("comment_to").removeAttribute('checked');}
c.onchange=function(){if(c.checked){localStorage.setItem('comment_right','1');youtube_hand.dark_eye();}
else{localStorage.setItem('comment_right',0);
//评论右上移
document.querySelector("#columns #primary #primary-inner").append(document.querySelector("#secondary #secondary-inner #sections"));
//related 左下移
document.querySelector("#secondary #secondary-inner").prepend(document.querySelector("#related"));
}
comment_right = localStorage.getItem('comment_right');
}
w.checded=dark_mode; if(dark_mode!=1){document.getElementById("dark_mode").removeAttribute('checked');}
w.onchange=function(){if(w.checked){localStorage.setItem('dark_mode','1');youtube_hand.dark_eye();youtube_hand.dark_eye1();}
else{localStorage.setItem('dark_mode',0);
document.querySelector("html").removeAttribute("dark");
document.querySelector('#style_type').remove();
}
dark_mode = localStorage.getItem('dark_mode');
}
t.checded=english_mode; if(english_mode!=1){document.getElementById("english_mode").removeAttribute('checked');}
t.onchange=function(){if(t.checked){localStorage.setItem('english_mode','1');
for (const mandarin of mandarins) {
mandarin.className -= ' invisible';}
for (const english of englishs) {
english.className += ' invisible';}
}
else{localStorage.setItem('english_mode',0);
for (const mandarin of mandarins) {
mandarin.className += ' invisible';}
for (const english of englishs) {
english.className -= ' invisible';}
}
english_mode = localStorage.getItem('english_mode');
}
if(video_quality){
for(var i=0;i div.html5-video-container > video");
v_elem = document.querySelector("#movie_player");
if (video_elem){
//main_opr();
video_elem.onloadeddata = function (){
main_opr();
setTimeout(function(){video_elem.onloadeddata = "";},3000)
}
video_elem.ondurationchange = function(){
video_elem.currentTime=0;
current_time=0;
main_opr();
}
clearInterval(main_timer);}
},100)
//主应用操作
function main_opr() {
//视频质量选择
var avail_quali;var current_time;
if((avail_quali = v_elem.getAvailableQualityLevels())&&avail_quali.indexOf(video_quality) == -1){
v_elem.setPlaybackQualityRange(avail_quali[0],avail_quali[0]);
}
else{v_elem.setPlaybackQualityRange(video_quality,video_quality);}
//点开视频介绍
if(expand_description){
if(document.getElementById("action-panel-details")) {
document.getElementById("action-panel-details").classList.remove("yt-uix-expander-collapsed");
}
if(document.querySelector("#more > .more-button.style-scope.ytd-video-secondary-info-renderer")) {
document.querySelector("#more > .more-button.style-scope.ytd-video-secondary-info-renderer").click();
}
}
//前跳过秒数+速度+播放质量设定
video_elem.oncanplay = function(){
if(video_speed&&video_elem){
video_elem.playbackRate = video_speed;}
// if(current_time>200){current_time=0}
if(sec_fwd&&(video_elem.currentTime < sec_fwd/1)){
video_elem.currentTime += sec_fwd/1;
video_elem.oncanplay = "";
}
// video_elem.currentTime += 1;
}
//暂停缓存
sec_sub = localStorage.getItem('sec_sub');
video_elem.onpause = function(){
if(!buffer_time||buffer_time == 0){return;}
if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration)){current_time=0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";}
else{current_time = parseInt(video_elem.currentTime);
video_elem.oncanplaythrough = function(){
if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration)){current_time=0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";}
else{video_elem.currentTime +=1;
if(sec_sub){
if((video_elem.currentTime-current_time)>(buffer_time?buffer_time:0)||(video_elem.duration<(video_elem.currentTime+parseInt(sec_sub)+10)))
{video_elem.currentTime = current_time;
console.log(current_time);console.log(video_elem.currentTime);
video_elem.oncanplaythrough='';current_time=0;}
}
else{
if((video_elem.currentTime-current_time)>(buffer_time?buffer_time:0)||(video_elem.duration<(video_elem.currentTime+30)))
{video_elem.currentTime = current_time;
video_elem.oncanplaythrough='';}
}
}
}
video_elem.currentTime += 1;
}
}
video_elem.onplay = function(){
if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration-1)){current_time = 0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";} // new
else if(current_time){video_elem.currentTime = current_time;
video_elem.oncanplaythrough = "";
}
}
video_elem.onended = function(){current_time = 0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";
}
//中键全屏
video_elem.addEventListener("mouseup", function(e) {
e.preventDefault();
if(e.button==1){
document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-fullscreen-button.ytp-button").click();
}
}, false);
//按c开启/停止缓冲功能,按e调整视频速度
document.onkeydown = key_down;
function key_down(e){
//document.addEventListener("keydown", function(e) {
if (document.activeElement.id != "" && document.activeElement.id != "movie_player") {
return;}
else{
if (e.keyCode == 69) {
e.preventDefault();
if(buffer_time){
temp_buffer = buffer_time;
buffer_time="";
current_time=0;
//localStorage.setItem('buffer_time','');
youtube_hand.message_box.show("no cashing set");
}
else if(temp_buffer){buffer_time=temp_buffer;
// localStorage.setItem('buffer_time',temp_buffer);
youtube_hand.message_box.show("cashing "+buffer_time+" seconds set");
}
else{youtube_hand.message_box.show("no cashing time preset");}
}
else if(e.keyCode == 68) {
if(video_speed != 1){
temp_speed = video_speed;
video_speed=1;
video_elem.playbackRate = video_speed;
speed_display.innerHTML="1x";
//localStorage.setItem('speed','1')
youtube_hand.message_box.show("video speed 1x set");
}
else if(temp_speed){
if(localStorage.getItem('speed')!=temp_speed){video_speed=localStorage.getItem('speed');}
else{video_speed=temp_speed;}
video_elem.playbackRate = video_speed/1;
speed_display.innerHTML=video_speed+"x";
//localStorage.setItem('speed',temp_speed);
youtube_hand.message_box.show("video speed "+video_speed+"x set");
}
else{ youtube_hand.message_box.show("no video speed preset");}
}
document.onkeydown = "";
setTimeout(function(){document.onkeydown = key_down;},2000);
}
}
// 滚轮前进后退
video_elem.onwheel = function(e){e.preventDefault()};
v_elem.onwheel = wheel_e;
function wheel_e (event) {
event.preventDefault();
video_elem.currentTime += parseInt(event.deltaY>0?wheel_sec/1:-wheel_sec) ;
v_elem.onwheel="";
setTimeout(function(){v_elem.onwheel= wheel_e;},300);
//同时重设播放速度
// video_speed = localStorage.getItem('speed');
// document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
}
//每隔25秒获取播放剩余时间,如在25秒内则执行主程序
if(sec_sub !== ""&&sec_sub !=0){
const sec_sub2=sec_sub/1+12;
setInterval(function(){
let left_duration = get_dura();
if((left_duration < sec_sub) && (left_duration != 0))
{
next_exec();
}
}, 1000);
}
//next part 执行
function next_exec(){
const sel_part2 = document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button");
if (sel_part2)
{console.log("clicked next---");sel_part2.click();
// video_elem.currentTime=0;
current_time=0;}
}
//获取播放剩余时间
function get_dura() {
const sel_start = video_elem.currentTime;
const sel_fin = video_elem.duration;
if (sel_fin) {
return sel_fin-sel_start;
}
return 0;
}
}
},
move_menu:function (){
//点开显示其余items
document.querySelector("#sections > ytd-guide-section-renderer:nth-child(2) > #items > ytd-guide-collapsible-entry-renderer > #expander-item > #endpoint").click();
//移动频道到左顶部
document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)").parentNode.append(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)"))
//页面写入script和style
var a_bug = document.createElement("script");
a_bug.innerHTML = `// 监听draggable的相关事件
function dragstart_handler(ev) {
console.log("dragStart");
// Add the target element's id to the data transfer object
ev.dataTransfer.setData("text", ev.target.id);
ev.dataTransfer.setData("text1", ev.target);
console.log(ev.dataTransfer.getData("text1"));
ev.dataTransfer.dropEffect = "move";
}
function dragEnter(ev) {
console.log("dragenter");
ev.preventDefault();
ev.target.className += ' drag-over';
}
function dragLeave(e) {
ev.preventDefault();
this.className = 'droppable';
}
function dragover_handler(ev) {
ev.preventDefault();
//ev.dataTransfer.dropEffect = "move"
}
function drop_handler(ev) {
ev.preventDefault();
// Get the id of the target and add the moved element to the target's DOM
var data = ev.dataTransfer.getData("text");
var first=ev.target.firstChild; //得到第一个元素
ev.target.insertBefore(document.getElementById(data),first); //在第原来的第一个元素之前插入
//ev.target.appendChild(document.getElementById(data));
}
function dragend_handler(ev) {
console.log("dragEnd");
// Remove all of the drag data
ev.dataTransfer.clearData();
//localStorage.setItem("itemstate",document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items").outerHTML);
//itemdetail写入localStorage
var json = [];
var data = {};
var items_length=document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer").length
for(var i=0;i ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer")[i].id;
//data.imgsrc = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer #img")[i].src;
data.text =document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer")[i].innerText;
//data.href = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint")[i].href;
json.push(data);
}
var jsonString = JSON.stringify(json);//[{"id":1,"imgsrc":"test1","text":2}]
localStorage.setItem("itemdetail",jsonString);
//localStorage.setItem("item_new","");
}
function noAllowDrop(ev) {
console.log("stopPropagation");
ev.stopPropagation();
}
//点击订阅或退订时,删除localStorage内已有相同item
if(document.querySelector("#inner-header-container #subscribe-button")){var subscribe_button = document.querySelector("#inner-header-container #subscribe-button")}
else if(document.querySelector("#meta-contents #top-row > #subscribe-button")){var subscribe_button = document.querySelector("#meta-contents #top-row > #subscribe-button");}
// var subscribe_button = document.querySelector("#inner-header-container #subscribe-button")?document.querySelector("#inner-header-container #subscribe-button"):document.querySelector("#meta-contents #top-row > #subscribe-button");
if(subscribe_button){
subscribe_button.onclick=function(){
//localStorage.setItem("item_new",document.querySelector("#text > a").innerText);
//localStorage.removeItem("itemdetail",document.querySelector("#channel-name #text").innerText);
let obj = JSON.parse(localStorage.itemdetail);
for(let j=obj.length-1;j>-1;j--){
if(obj[j]&&obj[j].text==document.querySelector("#channel-name #text").innerText){
let items_down = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
items_down.forEach(function(element,index,array) {
if(element.firstElementChild.title==document.querySelector("#channel-name #text").innerText){
document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items").append(element);
return false;
}
})
delete obj[j];}
}
localStorage.setItem("itemdetail",JSON.stringify(obj));
console.log("delete suc");
}
}
`;
document.body.appendChild(a_bug);
var style_type2 = document.createElement("style");style_type2.id = "style_type";
style_type2.innerHTML = `#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer{margin: 1em 0em 1em 0em !important;}
.drag-over {
border-style: dashed;
}
.droppable{}
`;
//#expandable-items > ytd-guide-entry-renderer{margin: 1.2em 0em 1.2em 0em !important;}
document.head.appendChild(style_type2);
//开始操作
var menu_opr = setInterval(function(){
// const droppables = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)");
if(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items > ytd-guide-entry-renderer")){
const droppables = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) > #items");
const draggable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
const no_draggable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint");
// const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint > tp-yt-paper-item");
// const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint *");
const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items *");
const draggable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer");
const no_draggable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer > #endpoint");
// const no_droppable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer > #endpoint *");
droppables.setAttribute("ondrop","drop_handler(event);");
droppables.setAttribute("ondragover","dragover_handler(event);");
// droppables.setAttribute("ondragenter","dragEnter(event);");
// droppables.setAttribute("ondragleave","dragLeave(event);");
no_draggable.forEach(function(element,index,array) {
element.setAttribute("draggable",false);
});
no_draggable2.forEach(function(element,index,array) {
element.setAttribute("draggable",false);
});
no_droppable.forEach(function(element,index,array) {
element.setAttribute("ondragover","noAllowDrop(event);");
});
// no_droppable2.forEach(function(element,index,array) {
// element.setAttribute("ondragover","noAllowDrop(event);");
//});
draggable.forEach(function(element,index,array) {
if(index ytd-guide-section-renderer:nth-child(1) #items");
let insert_sec = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items > ytd-guide-collapsible-entry-renderer");
let new_content2=document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) #items > ytd-guide-collapsible-entry-renderer #expanded #expandable-items > ytd-guide-entry-renderer");
new_content2.forEach(function(element){parent_item.insertBefore(element,insert_sec)});
const obj = JSON.parse(localStorage.getItem("itemdetail"));
//getItem("itemdetail")然后写入网页列表
if(localStorage.getItem("itemdetail")){
var items_drag = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
for(var j=obj.length-1;j>-1;j--){
// if(j==0){
items_drag.forEach(function(element,index,array) {
//console.log(element.firstElementChild.title);
//console.log(obj[j].text);
if(obj[j]&&element.firstElementChild.title==obj[j].text){
parent_item.prepend(element);
return false;
}
})
}
}
//把当日订阅置顶
document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) > #items").prepend(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(2) #items > ytd-guide-entry-renderer:nth-child(4)"));
clearInterval(menu_opr);
}
},100)
}
}
var load_menu = setInterval(function(){
//youtube_hand.dark_eye();
if(document.querySelector("#end")){
youtube_hand.play_menu();
youtube_hand.play_main();
youtube_hand.dark_eye1();
//document.querySelector("html").onpropertychange = youtube_hand.dark_eye;
clearInterval(load_menu);
}
},100)
setInterval(function(){ youtube_hand.dark_eye();},800)
if(subscription_up){
var move_ma=setInterval(function(){
if(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(2)"))
{
youtube_hand.move_menu();
clearInterval(move_ma);
}
},200)
}
})();