youtube-playback-rate

Sets video playback rate and does other stuff.

  1. // ==UserScript==
  2. // @name youtube-playback-rate
  3. // @namespace mikeOS
  4. // @version 0.5
  5. // @description Sets video playback rate and does other stuff.
  6. // @author Favlist.ru
  7. // @include *youtube.com*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. var rates = new Array(1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,2.1,2.2,2.3);
  12. var video;
  13. var channel_name;
  14. var upload_date;
  15. if (!localStorage.myratescheck) localStorage.myratescheck = "true";
  16.  
  17. window.setInterval(function(){
  18. if (document.location.href.indexOf("watch") > 0){
  19. video = document.getElementsByTagName("video")[0];
  20. if (document.getElementById("player-container")) displayProgress(video);
  21.  
  22. if (!document.getElementById("myrate")){
  23. var div = document.getElementById("player-container");
  24. var defaultrate = 1; if(localStorage.defaultrate && localStorage.myratescheck == "true") defaultrate = localStorage.defaultrate;
  25. for (var i = 0; i<rates.length; i++){
  26. var button = document.createElement("button");
  27. button.innerHTML = rates[i];
  28. button.setAttribute("value",rates[i]);
  29. button.setAttribute("class","favlist_yt_rate_button");
  30. if (i===0) button.setAttribute("style","margin-left:10px");
  31. button.setAttribute("onclick","this.setAttribute(\"disabled\",true);document.getElementById(\"myrate\").disabled=false;document.getElementById(\"myrate\").id=\"\";this.setAttribute(\"id\",\"myrate\");document.getElementsByTagName(\"video\")[0].playbackRate=this.value;localStorage.setItem(\"defaultrate\",this.value);this.blur();");
  32. if (defaultrate == rates[i]) {button.setAttribute("id","myrate");button.setAttribute("disabled",true);}
  33. div.appendChild(button);
  34. }
  35.  
  36. var label = document.createElement("label");
  37. label.setAttribute("style","position:relative;top:1px;left:6px;padding:1px 3px 1px 3px;border:1px solid #E0E0E0;background-color:#FAFAFA;color:#C9C9C9");
  38.  
  39. var check = document.createElement("input"); check.setAttribute("type","checkbox"); check.setAttribute("id","myratescheck");
  40. check.setAttribute("style","display:none");
  41. check.setAttribute("onclick","localStorage.setItem(\"myratescheck\",this.checked);p=this.parentNode;if(this.checked){p.style.border=\"1px solid #C7C7C7\";p.style.color=\"#808080\";}else{p.style.border=\"1px solid #E0E0E0\";p.style.color=\"#C9C9C9\";}");
  42. if(localStorage.myratescheck == "true") {check.setAttribute("checked",true);
  43. label.setAttribute("style","position:relative;top:3px;left:6px;padding:1px 6px 1px 6px;border:1px solid #545454;background-color:#545454;color:#808080;font-size:17px");
  44. }
  45. label.appendChild(check);
  46. var txt = document.createElement("span"); txt.innerHTML = "&#10003;";label.appendChild(txt);
  47. div.appendChild(label);
  48.  
  49. video.playbackRate=defaultrate;
  50. }
  51. var myrate = document.getElementById("myrate");
  52. video.playbackRate=myrate.value;
  53.  
  54. if (document.getElementById('channel_name') === null){
  55. var txt1 = document.createElement("span"); txt1.innerHTML = document.getElementsByClassName('date')[0].textContent.replace("Published on ","");
  56. txt1.setAttribute('id','upload_date');
  57. txt1.setAttribute("style","color:#A7A7A7;font-size:12px;margin-left:10px");
  58. document.getElementById("player-container").appendChild(txt1);
  59. upload_date = document.getElementById('upload_date').textContent;
  60.  
  61. var txt2 = document.createElement("span"); txt2.innerHTML = document.getElementById('owner-container').textContent;
  62. txt2.setAttribute('id','channel_name');
  63. txt2.setAttribute("style","color:#A7A7A7;font-size:14px;font-weight:bold;margin-left:10px;white-space: pre;");
  64. document.getElementById("player-container").appendChild(txt2);
  65. channel_name = document.getElementById('channel_name').textContent.replace("Verified","");
  66. }
  67.  
  68. if (channel_name !== '' && channel_name != document.getElementById('owner-container').textContent) {
  69. document.getElementById('channel_name').innerHTML = document.getElementById('owner-container').textContent.replace("Verified","");
  70. channel_name = document.getElementById('channel_name').textContent.replace("Verified","");
  71. }
  72. document.getElementById('upload_date').innerHTML = document.getElementsByClassName('date')[0].textContent.replace("Published on ","");
  73. }
  74. },1000);
  75.  
  76. function displayProgress(video){
  77. var playerwidth = document.getElementById("player-container").offsetWidth;
  78. var pcent = (video.currentTime / video.duration)*100;
  79. var progress_bar = document.getElementById("progress_bar");
  80. var progress_counter = document.getElementById("progress_counter");
  81. if (!progress_bar) {
  82. var bar = document.createElement("div"); bar.id = "progress_bar"; bar.setAttribute("style","height:3px;width:0px;background:#FF4646;position:relative;top:1px;margin-bottom:4px");
  83. document.getElementById("player-container").appendChild(bar);
  84. var counter = document.createElement("span"); counter.id = "progress_counter";
  85. counter.setAttribute("style","font-family: 'Courier';font-size:20px;font-weight:bold;color:#A7A7A7;position:relative;top:2px;margin-right:10px;");
  86. document.getElementById("player-container").appendChild(counter);
  87. } else {progress_bar.style.width = (playerwidth / 100) * pcent + "px";}
  88. if (video.duration) progress_counter.innerHTML = humanTime(Math.round(video.currentTime)) + "/" + humanTime(Math.round(video.duration));
  89. }
  90.  
  91. function humanTime(totalSec){
  92. var hours = parseInt( totalSec / 3600 ) % 24;
  93. var minutes = parseInt( totalSec / 60 ) % 60;
  94. var seconds = totalSec % 60;
  95. if (hours > 0) {
  96. return hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
  97. }else{
  98. return (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
  99. }
  100. }
  101.  
  102. var style = document.createElement("style");
  103. style.innerHTML = ".favlist_yt_rate_button{cursor:pointer;font-size:10px;margin-left:1px;height:24px;width:26px;text-align:center;color:#FAFAFA;border:none;border-left:1px solid #545454;border-right:1px solid #545454;background-color:#545454;}"+
  104. ".favlist_yt_rate_button[disabled]{color:#545454;border-left:1px solid transparent;border-right:1px solid transparent;background:transparent;cursor:default;} #eow-title{white-space:nowrap;font-size:18px}"+
  105. "#subitle-container-first{display:none}#watch7-sidebar{margin-top:-389px!important}"+
  106. ".ytd-iframe-companion-renderer{display:none}"+
  107. "#player-container {white-space:nowrap;position:relative;bottom:16px}ytd-video-primary-info-renderer{padding: 33px 0 28px 0;}";
  108. document.getElementsByTagName("head")[0].appendChild(style);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. var globalKey ='';
  117. var keyInterval = 0;
  118. var keyStop = 1;
  119. var docTitle = '';
  120. window.addEventListener("keydown", myFunction);
  121.  
  122. function myFunction(event) {
  123. if (event.keyCode == 83 && globalKey != event.keyCode) {
  124. keyInterval ++; keyStop = 0;docTitle = document.title;
  125. startKeyInterval();
  126. }
  127. globalKey = event.keyCode;
  128. }
  129. window.addEventListener("keyup", myFunction2);
  130.  
  131. function myFunction2(event) {
  132. if (keyInterval > 10){
  133. if (event.keyCode == 83) {
  134. document.location = 'https://www.youtube.com/feed/subscriptions';
  135. }
  136. }
  137. document.title = docTitle;
  138. keyInterval = 0; keyStop = 1;
  139. globalKey = '';
  140. }
  141.  
  142. function startKeyInterval(){
  143. if (keyInterval > 10 && document.title != "--- " + docTitle){
  144. document.title = "[☯] - " + docTitle;
  145. }
  146. if (keyInterval > 0&& keyStop === 0) window.setTimeout(function(){keyInterval ++;startKeyInterval();},100);
  147. }