u-Youtube

make Youtube under ur control

目前为 2022-04-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name u-Youtube
  3. // @name:zh-CN 功夫制霸YouTube
  4. // @name:zh-TW 功夫製霸YouTube
  5. // @name:en u-Youtube
  6. // @name:ua u-Youtube
  7. // @name:ja カンフー支配YouTube
  8. // @name:ko 쿵푸 지배YouTube
  9. // @name:it u-Youtube
  10. // @name:de u-Youtube
  11. // @name:fr u-Youtube
  12. // @namespace https://github.com/rasso1/u-Youtube
  13. // @version 1.20
  14. // @description make Youtube under ur control
  15. // @description:zh-TW make Youtube under ur control
  16. // @description:en make Youtube under ur control
  17. // @description:ja あなたのコントロール下でYouTubeを作る
  18. // @description:ko 유튜브를 당신의 통제하에 두십시오
  19. // @description:it rendi youtube sotto il tuo controllo
  20. // @description:de Machen Sie YouTube unter Ihrer Kontrolle
  21. // @description:fr faire de youtube sous votre contrôle
  22. // @author ok!
  23. // @match https://www.youtube.com/*
  24. // @match https://youtube.com/*
  25. // @run-at document-start
  26. // @contributionURL https://paypal.me/kongfuY
  27.  
  28. // ==/UserScript==
  29. (function() {
  30. 'use strict';
  31.  
  32. 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;
  33. var expand_description = 1;var inner_hight=window.innerHeight;var inner_width=window.innerWidth*0.38;
  34.  
  35.  
  36. var youtube_hand = {
  37. message_box: {
  38. last_message: null,
  39. show: function(message) {
  40. youtube_hand.message_box.last_message = message;
  41. document.querySelector(".ytp-bezel-text").innerText = message;
  42. document.querySelector(".ytp-bezel-text").parentNode.parentNode.style.display = "";
  43. document.querySelector(".ytp-bezel-text").parentNode.parentNode.classList.remove("ytp-bezel-text-hide");
  44. setTimeout(youtube_hand.message_box.hide, 2000, message);
  45. },
  46. hide: function(message_hide) {
  47. if (message_hide != youtube_hand.message_box.last_message) {
  48. return;
  49. }
  50. document.querySelector(".ytp-bezel-text").innerText = "";
  51. document.querySelector(".ytp-bezel-text").parentNode.parentNode.style.display = "none";
  52. }
  53. },
  54. dark_eye:function (){
  55. if(localStorage.getItem('dark_mode')==1){
  56. if(!document.querySelector("html").hasAttribute("dark")){
  57. document.querySelector("html").setAttribute("dark",true);}
  58. }
  59. else if(document.querySelector("html").hasAttribute("dark"))
  60. {document.querySelector("html").removeAttribute("dark")}
  61. if(localStorage.getItem("comment_right")==1&&document.querySelector("#columns #primary #primary-inner #sections")&&document.querySelector("#secondary #secondary-inner")){
  62. //评论右上移
  63.  
  64. document.querySelector("#secondary #secondary-inner").append(document.querySelector("#columns #primary #primary-inner #sections"));
  65. document.querySelector("#secondary #secondary-inner #sections").style="width:"+inner_width+"px;height:"+inner_hight+"px;overflow-y:auto";
  66. //related 左下移
  67. document.querySelector("#columns #primary #primary-inner").append(document.querySelector("#secondary #secondary-inner #related"));
  68.  
  69. }
  70. },
  71. dark_eye1:function (){
  72. if(localStorage.getItem('dark_mode')==1){
  73. var style_type = document.createElement("style");style_type.id = "style_type";
  74. style_type.innerHTML = `#end > div.dropdown-hover > div{font-size:12px;color:#bbb !important;background-color:#202020 !important;}
  75. #end > div.dropdown-hover > button{background-color:#222 !important;} #end > div.dropdown-hover path{fill:transparent !important;stroke:white !important;}
  76. #end > div.dropdown-hover > div > div{color:#aaa !important;background-color:#202020 !important;}
  77. #end > div.dropdown-hover > div > div p{font-size:12px;color:#bbb !important;background-color:#202020 !important;}
  78. .dropdown-hover button div{font-size:12px;color:#aaa !important;background-color:#333 !important;}
  79. #sec_sub,#sec_fwd,#wheel_sec,#buffer_time{font-size:12px;color:#bbb !important;background-color:#333 !important;border-radius:5px;}`;
  80. document.head.appendChild(style_type);
  81. }
  82. },
  83.  
  84. //播放设置
  85. play_menu:function(){
  86.  
  87.  
  88. var html_app = document.createElement("div");
  89. html_app.innerHTML = `
  90. <button style="border:none;outline:none;background-color:#fff;height:37px;width:37px;margin-right:10px;" aria-label="缓存/速度设置"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" style="pointer-events: none; display: block; width: 100%; height: 100%;"><g class="style-scope yt-icon">
  91. <path d="M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07-.98l2.1-1.65c.2-.15.25-.42.13-.64l-2-3.46c-.12-.22-.4-.3-.6-.22l-2.5 1c-.52-.4-1.08-.73-1.7-.98l-.37-2.65c-.06-.24-.27-.42-.5-.42h-4c-.27 0-.48.18-.5.42l-.4 2.65c-.6.25-1.17.6-1.7.98l-2.48-1c-.23-.1-.5 0-.6.22l-2 3.46c-.14.22-.08.5.1.64l2.12 1.65c-.04.32-.07.65-.07.98s.02.66.06.98l-2.1 1.65c-.2.15-.25.42-.13.64l2 3.46c.12.22.4.3.6.22l2.5-1c.52.4 1.08.73 1.7.98l.37 2.65c.04.24.25.42.5.42h4c.25 0 .46-.18.5-.42l.37-2.65c.6-.25 1.17-.6 1.7-.98l2.48 1c.23.1.5 0 .6-.22l2-3.46c.13-.22.08-.5-.1-.64l-2.12-1.65zM12 15.5c-1.93 0-3.5-1.57-3.5-3.5s1.57-3.5 3.5-3.5 3.5 1.57 3.5 3.5-1.57 3.5-3.5 3.5z" fill="transparent" stroke="black"></path>
  92. </g></svg>
  93. </button>
  94.  
  95. <div style="background:#fff;border-radius: 10px;left: -160px;right: 0;margin-left: 0;margin-right: -100px;padding-top: 10px;" class="dropdown-box fadeInDown"><div class="item clearfix" style="border-radius:10px;"><p style="margin:10px auto;font-size:18px;"><span class="mandarin invisible">播放设置&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="english">Playback Setting&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><font size=2>Chs </font><input style="margin:auto" id="english_mode" class="switch switch-anim" type="checkbox" checked></p>
  96. <div style="margin:10px auto;font-size:12px;"><hr /><hr /></div>
  97. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible" style="margin:10px auto;font-size:16px;">当前速度:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="english">current speed:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="speed_display"></span></p>
  98. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">选择播放速度:&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="english">specify sp<font color=yellow>e</font>ed:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><button id="speed_button" style="background-color: #5bb7fe;border-radius:5px;color:white;font-size:12px;"> &nbsp; 1x&nbsp; </button></p><input type="range" min="0" max="100" value="25" class="slider" id="speed_slider"><br /><br />
  99. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">跳过片头秒数:&nbsp;&nbsp;&nbsp;</span><span class="english">skip the title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="sec_fwd" style="width:40px;height:12px;vertical-align: middle;padding: 5px;background-color:#fff;border: 2px solid #ddd;border-radius:5px;font-size:12px;" placeholder="seconds">
  100. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">跳过片尾秒数:&nbsp;&nbsp;&nbsp;</span><span class="english">skip the end:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="sec_sub" style="width:40px;height:12px;vertical-align: middle;padding: 5px;background-color: #fff;border: 2px solid #ddd;border-radius:5px;font-size:12px;" placeholder="seconds"></p>
  101. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">鼠标滚动秒数:&nbsp;&nbsp;&nbsp;</span><span class="english">mouse scroll:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="wheel_sec" style="width:40px;height:12px;vertical-align: middle;padding: 5px;background-color: #fff;border: 2px solid #ddd;border-radius:5px;font-size:12px;" placeholder="seconds"></p>
  102. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">暂停缓存秒数:&nbsp;&nbsp;&nbsp;</span><span class="english">pause n <font color=yellow>c</font>ashing:&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="buffer_time" style="width:40px;height:12px;vertical-align: middle;padding: 5px;background-color: #fff;border: 2px solid #ddd;border-radius:5px;font-size:12px;" placeholder="seconds"></p>
  103. <p style="margin:10px auto;font-size:16px;"><span class="mandarin invisible">播放质量:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class="english">specify resolution&nbsp;</span> <select id="video_quality" style="border-radius:5px;"><option value="auto">auto</option><option value="hd2160">4K 2160p</option><option value="hd1440">HD 1440p</option><option value="hd1080">HD 1080p</option><option value="hd720">720p</option> <option value="large">480p</option><option value="medium">360p</option><option value="small">240p</option><option value="tiny">144p</option> </select></p>
  104. <p style="margin:10px auto 20px auto;font-size:16px;"><span class="mandarin invisible">评论载右:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="english">comments to right:&nbsp;&nbsp;</span><input style="margin:auto" id="comment_to" class="switch switch-anim" type="checkbox" checked></p>
  105.  
  106. <p style="margin:10px auto 20px auto;font-size:16px;"><span class="mandarin invisible">暗夜模式 :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="english">dark mode:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><input style="margin:auto" id="dark_mode" class="switch switch-anim" type="checkbox" checked></p>
  107. <p style="margin:10px auto 20px auto;font-size:16px;"><span class="mandarin invisible">捐赠&#126980;:&nbsp; </span><span class="english">donate&#127854;:&nbsp;&nbsp;</span><a href="https://paypal.me/kongfuY" style="color:#5bb7fe;">paypal.me/kongfuY</a></p></div></div>
  108.  
  109. <style>#speed_slider {width: 175px;}.invisible {display: none;}.ytd-feed-filter-chip-bar-renderer.style-scope{z-index:999 !important;}.fadeInDown{-webkit-animation:fadeInDown .5s .2s ease both;-moz-animation:fadeInDown .5s .2s ease both}@-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translateY(-10px)}100%{opacity:1;-webkit-transform:translateY(0)}}@-moz-keyframes fadeInDown{0%{opacity:0;-moz-transform:translateY(-10px)}100%{opacity:1;-moz-transform:translateY(0)}}.dropdown{position:relative}.dropdown-box{display:none;position:absolute;z-index:9999999999}.dropdown-box.top{bottom:100%;padding-bottom:10px;left:50%;margin-left:-80px}.dropdown-box.bottom{top:100%;padding-top:10px;left:50%;margin-left:-80px}.dropdown-box.left{left:100%;padding-left:10px;bottom:0}.dropdown-box.right{right:100%;padding-right:10px;bottom:0}.dropdown-box .item{padding:10px;width:250px;border-radius:2px}.dropdown-hover{position:relative}.dropdown-hover:hover .dropdown-box{display:block}</style>
  110. <style>a {color: #670000;}.switch {width: 26px;height: 13px;position: relative;border: 1px solid #dfdfdf;background-color: #fdfdfd;box-shadow: #dfdfdf 0 0 0 0 inset; border-radius: 18px;background-clip: content-box;display: inline-block;-webkit-appearance: none;user-select: none;outline: none;} .switch:before { content: '';width: 13px;height: 13px;position: absolute;top: 0;left: 0;border-radius: 12px;background-color: #fff;box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);}.switch:checked {border-color: #5bb7fe;box-shadow: #5bb7fe 0 0 0 16px inset;background-color: #5bb7fe;}.switch:checked:before {left: 15px;}.switch.switch-anim {transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s;}.switch.switch-anim:before {transition: left 0.3s;}.switch.switch-anim:checked {box-shadow: #5bb7fe 0 0 0 14px inset;background-color: #5bb7fe;transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s;}.switch.switch-anim:checked:before {transition: left 0.3s;}.text-red {color: #aaa;}</style>
  111. `;
  112. html_app.classList.add('dropdown-hover');
  113. document.querySelector("#end").appendChild(html_app);
  114.  
  115. //
  116. var speed_slider=document.getElementById("speed_slider"),speed_button=document.getElementById("speed_button"),
  117. y=document.getElementById("sec_sub"),z=document.getElementById("wheel_sec"),x=document.getElementById("buffer_time"),w=document.getElementById("dark_mode"),
  118. s=document.getElementById("video_quality"),v=document.getElementById("sec_fwd"),t=document.getElementById("english_mode"),c=document.getElementById("comment_to");
  119. dark_mode = localStorage.getItem('dark_mode');comment_right = localStorage.getItem('comment_right');
  120. var english_mode = localStorage.getItem('english_mode');
  121. speed_display=document.getElementById("speed_display");
  122. const mandarins = document.querySelectorAll('.mandarin');const englishs = document.querySelectorAll('.english');
  123.  
  124.  
  125. video_speed = localStorage.getItem('speed');sec_fwd = localStorage.getItem('sec_fwd');sec_sub = localStorage.getItem('sec_sub');
  126. wheel_sec = localStorage.getItem('wheel_sec');buffer_time = localStorage.getItem('buffer_time');video_quality = localStorage.getItem('video_quality');
  127.  
  128. english_mode = localStorage.getItem('english_mode');
  129. //判读英文菜单
  130. if(localStorage.getItem('english_mode')==1){
  131. for (const mandarin of mandarins) {
  132. mandarin.className -= ' invisible';
  133. }
  134. for (const english of englishs) {
  135. english.className += ' invisible';
  136. }
  137. }
  138.  
  139. video_speed?speed_display.innerHTML=video_speed+"x":speed_display.innerHTML="1x";
  140.  
  141. setTimeout(function (){
  142. if(video_speed&&document.querySelector('#movie_player'))
  143. document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;},4000);
  144.  
  145. speed_slider.value = localStorage.getItem('speed')*25;
  146.  
  147. 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";
  148. document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
  149. }
  150.  
  151. 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";
  152. document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
  153. speed_slider.value = localStorage.getItem('speed')*25;
  154. }
  155.  
  156. sec_fwd?v.value=sec_fwd:v.value="";
  157. v.onchange=function(){localStorage.setItem('sec_fwd',this.value);sec_fwd = localStorage.getItem('sec_fwd');sec_fwd?v.value=sec_fwd:v.value="";
  158. }
  159. sec_sub?y.value=sec_sub:y.value="";
  160. y.onchange=function(){localStorage.setItem('sec_sub',this.value);sec_sub = localStorage.getItem('sec_sub');sec_sub?y.value=sec_sub:y.value="";
  161. }
  162.  
  163. wheel_sec?z.value=wheel_sec:z.value="";
  164. z.onchange=function(){localStorage.setItem('wheel_sec',this.value);wheel_sec = localStorage.getItem('wheel_sec');wheel_sec?z.value=wheel_sec:z.value="";
  165. }
  166. buffer_time?x.value=buffer_time:x.value="";
  167. x.onchange=function(){localStorage.setItem('buffer_time',this.value);buffer_time = localStorage.getItem('buffer_time');buffer_time?x.value=buffer_time:x.value="";
  168. }
  169. c.checded=comment_right; if(comment_right!=1){document.getElementById("comment_to").removeAttribute('checked');}
  170. c.onchange=function(){if(c.checked){localStorage.setItem('comment_right','1');youtube_hand.dark_eye();}
  171. else{localStorage.setItem('comment_right',0);
  172. //评论右上移
  173.  
  174. document.querySelector("#columns #primary #primary-inner").append(document.querySelector("#secondary #secondary-inner #sections"));
  175. //related 左下移
  176. document.querySelector("#secondary #secondary-inner").prepend(document.querySelector("#related"));
  177.  
  178. }
  179. comment_right = localStorage.getItem('comment_right');
  180. }
  181. w.checded=dark_mode; if(dark_mode!=1){document.getElementById("dark_mode").removeAttribute('checked');}
  182. w.onchange=function(){if(w.checked){localStorage.setItem('dark_mode','1');youtube_hand.dark_eye();youtube_hand.dark_eye1();}
  183. else{localStorage.setItem('dark_mode',0);
  184. document.querySelector("html").removeAttribute("dark");
  185. document.querySelector('#style_type').remove();
  186. }
  187. dark_mode = localStorage.getItem('dark_mode');
  188. }
  189. t.checded=english_mode; if(english_mode!=1){document.getElementById("english_mode").removeAttribute('checked');}
  190. t.onchange=function(){if(t.checked){localStorage.setItem('english_mode','1');
  191. for (const mandarin of mandarins) {
  192. mandarin.className -= ' invisible';}
  193. for (const english of englishs) {
  194. english.className += ' invisible';}
  195. }
  196. else{localStorage.setItem('english_mode',0);
  197. for (const mandarin of mandarins) {
  198. mandarin.className += ' invisible';}
  199. for (const english of englishs) {
  200. english.className -= ' invisible';}
  201. }
  202. english_mode = localStorage.getItem('english_mode');
  203. }
  204.  
  205. if(video_quality){
  206. for(var i=0;i<s.length;i++){
  207. if(s.options[i].value==video_quality){
  208. s.options[i].selected=true;
  209. }
  210. }
  211. }
  212. s.onchange=function (){
  213. var quality_value = s[s.selectedIndex].getAttribute("value");
  214. localStorage.setItem('video_quality',quality_value);
  215. video_quality = localStorage.getItem('video_quality');
  216. var avail_quali;var v_elem = document.querySelector("#movie_player");
  217. if((avail_quali = v_elem.getAvailableQualityLevels())&&avail_quali.indexOf(video_quality) == -1){
  218. v_elem.setPlaybackQualityRange(avail_quali[0],avail_quali[0]);
  219. }
  220. else{v_elem.setPlaybackQualityRange(video_quality,video_quality);}
  221. }
  222.  
  223. },
  224.  
  225. play_main:function (){
  226.  
  227. sec_fwd = localStorage.getItem('sec_fwd');
  228. sec_sub = localStorage.getItem('sec_sub');
  229. wheel_sec = localStorage.getItem('wheel_sec');
  230. buffer_time = localStorage.getItem('buffer_time');
  231.  
  232. var video_speed = localStorage.getItem('speed');
  233.  
  234. var video_elem,v_elem;
  235.  
  236.  
  237. //每3秒试执行主应用操作
  238. var main_timer = setInterval(function () {
  239. video_elem = document.querySelector("#movie_player > div.html5-video-container > video");
  240. v_elem = document.querySelector("#movie_player");
  241.  
  242. if (video_elem){
  243. //main_opr();
  244. video_elem.onloadeddata = function (){
  245. main_opr();
  246. setTimeout(function(){video_elem.onloadeddata = "";},3000)
  247.  
  248. }
  249. video_elem.ondurationchange = function(){
  250. video_elem.currentTime=0;
  251. current_time=0;
  252. main_opr();
  253. }
  254. clearInterval(main_timer);}
  255. },500)
  256.  
  257. //主应用操作
  258. function main_opr() {
  259. //视频质量选择
  260.  
  261. var avail_quali;var current_time;
  262. if((avail_quali = v_elem.getAvailableQualityLevels())&&avail_quali.indexOf(video_quality) == -1){
  263. v_elem.setPlaybackQualityRange(avail_quali[0],avail_quali[0]);
  264. }
  265. else{v_elem.setPlaybackQualityRange(video_quality,video_quality);}
  266. //点开视频介绍
  267. if(expand_description){
  268.  
  269. if(document.getElementById("action-panel-details")) {
  270. document.getElementById("action-panel-details").classList.remove("yt-uix-expander-collapsed");
  271. }
  272.  
  273. if(document.querySelector("#more > .more-button.style-scope.ytd-video-secondary-info-renderer")) {
  274. document.querySelector("#more > .more-button.style-scope.ytd-video-secondary-info-renderer").click();
  275. }
  276. }
  277.  
  278. //前跳过秒数+速度+播放质量设定
  279. video_elem.oncanplay = function(){
  280. if(video_speed&&video_elem){
  281. video_elem.playbackRate = video_speed;}
  282. // if(current_time>200){current_time=0}
  283. if(sec_fwd&&(video_elem.currentTime < sec_fwd/1)){
  284. video_elem.currentTime += sec_fwd/1;
  285. video_elem.oncanplay = "";
  286. }
  287. // video_elem.currentTime += 1;
  288.  
  289. }
  290.  
  291. //暂停缓存
  292.  
  293. sec_sub = localStorage.getItem('sec_sub');
  294. video_elem.onpause = function(){
  295. if(!buffer_time||buffer_time == 0){return;}
  296. if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration)){current_time=0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";}
  297. else{current_time = parseInt(video_elem.currentTime);
  298. video_elem.oncanplaythrough = function(){
  299. if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration)){current_time=0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";}
  300. else{video_elem.currentTime +=1;
  301. if(sec_sub){
  302. if((video_elem.currentTime-current_time)>(buffer_time?buffer_time:0)||(video_elem.duration<(video_elem.currentTime+parseInt(sec_sub)+10)))
  303. {video_elem.currentTime = current_time;
  304. console.log(current_time);console.log(video_elem.currentTime);
  305. video_elem.oncanplaythrough='';current_time=0;}
  306. }
  307. else{
  308. if((video_elem.currentTime-current_time)>(buffer_time?buffer_time:0)||(video_elem.duration<(video_elem.currentTime+30)))
  309. {video_elem.currentTime = current_time;
  310. video_elem.oncanplaythrough='';}
  311. }
  312. }
  313. }
  314. video_elem.currentTime += 1;
  315. }
  316. }
  317. video_elem.onplay = function(){
  318.  
  319. if(parseInt(video_elem.currentTime)==parseInt(video_elem.duration-1)){current_time = 0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";} // new
  320. else if(current_time){video_elem.currentTime = current_time;
  321. video_elem.oncanplaythrough = "";
  322. }
  323. }
  324. video_elem.onended = function(){current_time = 0;video_elem.onpause = "";video_elem.onplay = "";video_elem.oncanplaythrough ="";
  325. }
  326. //中键全屏
  327.  
  328. video_elem.addEventListener("mouseup", function(e) {
  329. e.preventDefault();
  330. if(e.button==1){
  331. document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-fullscreen-button.ytp-button").click();
  332. }
  333. }, false);
  334. //按c开启/停止缓冲功能,按e调整视频速度
  335. document.onkeydown = key_down;
  336.  
  337. function key_down(e){
  338. //document.addEventListener("keydown", function(e) {
  339. if (document.activeElement.id != "" && document.activeElement.id != "movie_player") {
  340. return;}
  341.  
  342. else{
  343. if (e.keyCode == 67) {
  344. e.preventDefault();
  345. if(buffer_time){
  346. temp_buffer = buffer_time;
  347. buffer_time="";
  348. //current_time=0;
  349. //localStorage.setItem('buffer_time','');
  350. youtube_hand.message_box.show("no cashing set");
  351. }
  352. else if(temp_buffer){buffer_time=temp_buffer;
  353. // localStorage.setItem('buffer_time',temp_buffer);
  354. youtube_hand.message_box.show("cashing "+buffer_time+" seconds set");
  355. }
  356. else{youtube_hand.message_box.show("no cashing time preset");}
  357. }
  358. else if(e.keyCode == 69) {
  359. if(video_speed != 1){
  360. temp_speed = video_speed;
  361. video_speed=1;
  362. video_elem.playbackRate = video_speed;
  363. speed_display.innerHTML="1x";
  364. //localStorage.setItem('speed','1')
  365. youtube_hand.message_box.show("video speed 1x set");
  366. }
  367. else if(temp_speed){
  368. if(localStorage.getItem('speed')!=temp_speed){video_speed=localStorage.getItem('speed');}
  369. else{video_speed=temp_speed;}
  370. video_elem.playbackRate = video_speed/1;
  371. speed_display.innerHTML=video_speed+"x";
  372. //localStorage.setItem('speed',temp_speed);
  373. youtube_hand.message_box.show("video speed "+video_speed+"x set");
  374. }
  375. else{ youtube_hand.message_box.show("no video speed preset");}
  376.  
  377. }
  378. document.onkeydown = "";
  379. setTimeout(function(){document.onkeydown = key_down;},2000);
  380. }
  381. }
  382.  
  383. // 滚轮前进后退
  384. video_elem.onwheel = function(e){e.preventDefault()};
  385. v_elem.onwheel = wheel_e;
  386.  
  387. function wheel_e (event) {
  388. event.preventDefault();
  389.  
  390. video_elem.currentTime += parseInt(event.deltaY>0?wheel_sec/1:-wheel_sec) ;
  391.  
  392. v_elem.onwheel="";
  393. setTimeout(function(){v_elem.onwheel= wheel_e;},300);
  394.  
  395. //同时重设播放速度
  396. // video_speed = localStorage.getItem('speed');
  397. // document.querySelector("#movie_player > div.html5-video-container > video").playbackRate = video_speed;
  398.  
  399. }
  400.  
  401. //每隔25秒获取播放剩余时间,如在25秒内则执行主程序
  402. if(sec_sub !== ""&&sec_sub !=0){
  403. const sec_sub2=sec_sub/1+12;
  404. setInterval(function(){
  405. let left_duration = get_dura();
  406. if((left_duration < sec_sub) && (left_duration != 0))
  407. {
  408.  
  409. next_exec();
  410.  
  411. }
  412. }, 1000);
  413. }
  414.  
  415. //next part 执行
  416. function next_exec(){
  417. 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");
  418. if (sel_part2)
  419. {console.log("clicked next---");sel_part2.click();
  420. // video_elem.currentTime=0;
  421. current_time=0;}
  422. }
  423.  
  424. //获取播放剩余时间
  425. function get_dura() {
  426. const sel_start = video_elem.currentTime;
  427. const sel_fin = video_elem.duration;
  428. if (sel_fin) {
  429. return sel_fin-sel_start;
  430. }
  431. return 0;
  432. }
  433.  
  434.  
  435.  
  436.  
  437. }
  438.  
  439. },
  440.  
  441. move_menu:function (){
  442. //点开显示其余items
  443. document.querySelector("#sections > ytd-guide-section-renderer:nth-child(2) > #items > ytd-guide-collapsible-entry-renderer > #expander-item > #endpoint").click();
  444. //移动频道到左顶部
  445. document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)").parentNode.append(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)"))
  446.  
  447.  
  448. //页面写入script和style
  449. var a_bug = document.createElement("script");
  450. a_bug.innerHTML = `// 监听draggable的相关事件
  451. function dragstart_handler(ev) {
  452. console.log("dragStart");
  453. // Add the target element's id to the data transfer object
  454. ev.dataTransfer.setData("text", ev.target.id);
  455. ev.dataTransfer.setData("text1", ev.target);
  456. console.log(ev.dataTransfer.getData("text1"));
  457. ev.dataTransfer.dropEffect = "move";
  458. }
  459. function dragEnter(ev) {
  460. console.log("dragenter");
  461. ev.preventDefault();
  462. ev.target.className += ' drag-over';
  463. }
  464. function dragLeave(e) {
  465. ev.preventDefault();
  466. this.className = 'droppable';
  467. }
  468. function dragover_handler(ev) {
  469. ev.preventDefault();
  470. //ev.dataTransfer.dropEffect = "move"
  471. }
  472. function drop_handler(ev) {
  473. ev.preventDefault();
  474. // Get the id of the target and add the moved element to the target's DOM
  475. var data = ev.dataTransfer.getData("text");
  476. var first=ev.target.firstChild; //得到第一个元素
  477. ev.target.insertBefore(document.getElementById(data),first); //在第原来的第一个元素之前插入
  478. //ev.target.appendChild(document.getElementById(data));
  479. }
  480. function dragend_handler(ev) {
  481. console.log("dragEnd");
  482. // Remove all of the drag data
  483. ev.dataTransfer.clearData();
  484. //localStorage.setItem("itemstate",document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items").outerHTML);
  485.  
  486. //itemdetail写入localStorage
  487. var json = [];
  488. var data = {};
  489. var items_length=document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer").length
  490. for(var i=0;i<items_length-1;i++){
  491. data = {};
  492. //data.id = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer")[i].id;
  493. //data.imgsrc = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer #img")[i].src;
  494. data.text =document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer")[i].innerText;
  495. //data.href = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint")[i].href;
  496.  
  497. json.push(data);
  498.  
  499. }
  500. var jsonString = JSON.stringify(json);//[{"id":1,"imgsrc":"test1","text":2}]
  501. localStorage.setItem("itemdetail",jsonString);
  502. //localStorage.setItem("item_new","");
  503. }
  504. function noAllowDrop(ev) {
  505. console.log("stopPropagation");
  506. ev.stopPropagation();
  507. }
  508.  
  509.  
  510. //点击订阅或退订时,删除localStorage内已有相同item
  511. if(document.querySelector("#inner-header-container #subscribe-button")){var subscribe_button = document.querySelector("#inner-header-container #subscribe-button")}
  512. else if(document.querySelector("#meta-contents #top-row > #subscribe-button")){var subscribe_button = document.querySelector("#meta-contents #top-row > #subscribe-button");}
  513. // 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");
  514.  
  515. if(subscribe_button){
  516.  
  517. subscribe_button.onclick=function(){
  518.  
  519. //localStorage.setItem("item_new",document.querySelector("#text > a").innerText);
  520. //localStorage.removeItem("itemdetail",document.querySelector("#channel-name #text").innerText);
  521. let obj = JSON.parse(localStorage.itemdetail);
  522. for(let j=obj.length-1;j>-1;j--){
  523. if(obj[j]&&obj[j].text==document.querySelector("#channel-name #text").innerText){
  524. let items_down = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
  525.  
  526. items_down.forEach(function(element,index,array) {
  527.  
  528. if(element.firstElementChild.title==document.querySelector("#channel-name #text").innerText){
  529.  
  530. document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items").append(element);
  531. return false;
  532. }
  533. })
  534.  
  535. delete obj[j];}
  536. }
  537. localStorage.setItem("itemdetail",JSON.stringify(obj));
  538. console.log("delete suc");
  539. }
  540. }
  541. `;
  542. document.body.appendChild(a_bug);
  543.  
  544. var style_type2 = document.createElement("style");style_type2.id = "style_type";
  545. style_type2.innerHTML = `#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer{margin: 1em 0em 1em 0em !important;}
  546. .drag-over {
  547. border-style: dashed;
  548. }
  549. .droppable{}
  550. `;
  551. //#expandable-items > ytd-guide-entry-renderer{margin: 1.2em 0em 1.2em 0em !important;}
  552. document.head.appendChild(style_type2);
  553.  
  554. //开始操作
  555. var menu_opr = setInterval(function(){
  556.  
  557. // const droppables = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1)");
  558. if(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items > ytd-guide-entry-renderer")){
  559. const droppables = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) > #items");
  560. const draggable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
  561. const no_draggable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint");
  562. // const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint > tp-yt-paper-item");
  563. // const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer > #endpoint *");
  564. const no_droppable = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items *");
  565.  
  566. const draggable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer");
  567. const no_draggable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer > #endpoint");
  568. // const no_droppable2 = document.querySelectorAll("#expandable-items > ytd-guide-entry-renderer > #endpoint *");
  569.  
  570. droppables.setAttribute("ondrop","drop_handler(event);");
  571. droppables.setAttribute("ondragover","dragover_handler(event);");
  572. // droppables.setAttribute("ondragenter","dragEnter(event);");
  573. // droppables.setAttribute("ondragleave","dragLeave(event);");
  574. no_draggable.forEach(function(element,index,array) {
  575. element.setAttribute("draggable",false);
  576. });
  577. no_draggable2.forEach(function(element,index,array) {
  578. element.setAttribute("draggable",false);
  579. });
  580.  
  581. no_droppable.forEach(function(element,index,array) {
  582. element.setAttribute("ondragover","noAllowDrop(event);");
  583. });
  584. // no_droppable2.forEach(function(element,index,array) {
  585. // element.setAttribute("ondragover","noAllowDrop(event);");
  586. //});
  587.  
  588.  
  589. draggable.forEach(function(element,index,array) {
  590. if(index<array.length){
  591. element.setAttribute("draggable",true);
  592. element.setAttribute("ondragstart","dragstart_handler(event);");
  593. element.setAttribute("ondragend","dragend_handler(event);");
  594. if(!element.id){element.setAttribute("id","move"+index);}
  595. }
  596. });
  597. let index_x =0;
  598. draggable2.forEach(function(element,index,array) {
  599. index_x=index+7;
  600. element.setAttribute("draggable",true);
  601. element.setAttribute("ondragstart","dragstart_handler(event);");
  602. element.setAttribute("ondragend","dragend_handler(event);");
  603. element.setAttribute("id","move"+index_x);
  604.  
  605. });
  606.  
  607. //把隐藏内容拉出来,放入items
  608. const parent_item = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items");
  609. let insert_sec = document.querySelector("#sections > ytd-guide-section-renderer:nth-child(1) #items > ytd-guide-collapsible-entry-renderer");
  610. 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");
  611. new_content2.forEach(function(element){parent_item.insertBefore(element,insert_sec)});
  612.  
  613.  
  614. const obj = JSON.parse(localStorage.getItem("itemdetail"));
  615.  
  616. //getItem("itemdetail")然后写入网页列表
  617. if(localStorage.getItem("itemdetail")){
  618.  
  619. var items_drag = document.querySelectorAll("#sections > ytd-guide-section-renderer:nth-child(1) > #items > ytd-guide-entry-renderer");
  620.  
  621. for(var j=obj.length-1;j>-1;j--){
  622. // if(j==0){
  623. items_drag.forEach(function(element,index,array) {
  624.  
  625. //console.log(element.firstElementChild.title);
  626. //console.log(obj[j].text);
  627. if(obj[j]&&element.firstElementChild.title==obj[j].text){
  628.  
  629. parent_item.prepend(element);
  630. return false;
  631. }
  632. })
  633.  
  634. }
  635.  
  636.  
  637. }
  638. //把当日订阅置顶
  639. 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)"));
  640.  
  641. clearInterval(menu_opr);
  642. }
  643.  
  644. },100)
  645.  
  646. }
  647.  
  648.  
  649. }
  650.  
  651.  
  652. var load_menu = setInterval(function(){
  653. //youtube_hand.dark_eye();
  654.  
  655. if(document.querySelector("#end")){
  656. youtube_hand.play_menu();
  657. youtube_hand.play_main();
  658. youtube_hand.dark_eye1();
  659.  
  660. //document.querySelector("html").onpropertychange = youtube_hand.dark_eye;
  661.  
  662. clearInterval(load_menu);
  663. }
  664. },100)
  665.  
  666.  
  667.  
  668. setInterval(function(){ youtube_hand.dark_eye();},800)
  669.  
  670.  
  671.  
  672. var move_ma=setInterval(function(){
  673. if(document.querySelector("#sections > ytd-guide-section-renderer:nth-child(2)"))
  674. {
  675. youtube_hand.move_menu();
  676.  
  677. clearInterval(move_ma);
  678. }
  679. },200)
  680.  
  681.  
  682.  
  683. })();