Title Youtube Locations

Puts the video title in the location bar of all YouTube video pages. Now with extra features add scrollbars, animate thumbnails, reduce font sizes and un-float the header.

  1. // ==UserScript==
  2. // @name Title Youtube Locations
  3. // @namespace TYTLs
  4. // @description Puts the video title in the location bar of all YouTube video pages. Now with extra features add scrollbars, animate thumbnails, reduce font sizes and un-float the header.
  5. // @version 1.3.0
  6. // @downstreamURL http://userscripts.org/scripts/source/87416.user.js
  7. // @include http://*.youtube.*/*
  8. // @include http://youtube.*/*
  9. // @include https://*.youtube.*/*
  10. // @include https://youtube.*/*
  11. //// YouTube thumbnails almost certainly appear if you do a video search:
  12. // @include https://www.google.co.uk/search?*&tbm=vid&*
  13. //// YouTube thumbnails occasionally appear in normal Google search results:
  14. // @include https://www.google.co.uk/search*
  15. // @grant GM_addStyle
  16. // @grant GM_log
  17. // ==/UserScript==
  18.  
  19. // TODO: In case for some reason YouTube or another script redirects us to
  20. // another URL which has lost the title param we added, we will re-run forever.
  21. // To avoid this we should have a fallback. E.g. if the title we want to set
  22. // is the same as the last one we did set (via GM_set/getValue) then do not try
  23. // again.
  24. // This has never happened so far. :)
  25.  
  26. // If you are interested in YouTube images:
  27. // Thumbnails url looks like http://img.youtube.com/vi/8aYQ_wjmriQ/2.jpg
  28. // Youtube generates 4 thumbnails: 0.jpg at 320x240, and 1.jpg, 2.jpg, 3.jpg
  29. // Also large: hqdefault.jpg (480x360) and sometimes but not always hq1.jpg, hq2.jpg, hq3.jpg
  30.  
  31.  
  32.  
  33. var addTitleToLocation = false; // Oh dear, the primary feature of this plugin has started causing annoying reloads. TODO Perhaps we can do it with pushState instead...
  34. var reduceFontSizes = true;
  35. var addScrollbars = true;
  36. var scrollDownToVideo = false; // YouTube's header ("masthead") is now floating. Setting this true will un-float it, then scroll down to hide it. But scrolling containers should then be enlarged.
  37. var animateThumbnails = true;
  38. var unfloatTheHeader = true;
  39.  
  40.  
  41.  
  42. if (addTitleToLocation) {
  43.  
  44. // NOTE: This code is deprecated, as I now use the userscripts URLs_Need_Titles
  45.  
  46. setTimeout(function(){
  47. if (document.location.pathname == "/watch") {
  48.  
  49. var title = document.title.replace(/ - YouTube$/,'')
  50. || null;
  51.  
  52. if (title)
  53. title = title.replace(/ /g,'_').replace(/^[\r\n_]*/,'').replace(/[\r\n_]*$/,''); // "_"s paste better into IRC, since " "s become "%20"s which are hard to read. The second and third parts trim "_"s and newlines from the start and end of the string.
  54.  
  55. if (title) {
  56. if (!document.location.hash) {
  57. document.location.replace(document.location.href + '#' + title); // Does not alter browser history
  58. // document.location.hash = title; // Crashes Chrome less often
  59. }
  60. }
  61. }
  62. },5000); // This is what really stops the crashing!
  63.  
  64. }
  65.  
  66.  
  67.  
  68. if (reduceFontSizes) {
  69. // == Reduce font size of thumbnail titles ==
  70. GM_addStyle(".yt-tile-default.video-list-item a .title, #watch-sidebar .video-list-item .title { font-size: 11px; line-height: 10px; }");
  71. // Defaults are font-size: 13px; and line-height: 15px; which show only two lines in my browser.
  72. }
  73.  
  74.  
  75.  
  76. if (addScrollbars) {
  77.  
  78. // == Scrollbars on comments and related vids, to keep the video in view. ==
  79. setTimeout(function(){
  80. // We could alternatively act on watch-panel but that includes the video navigation buttons!
  81. // BUG: Interferes with YouTube's lazy-loading of thumbnails for related video.
  82. if (document.location.href.indexOf("/all_comments?") >= 0) {
  83. return; // Leave the full comments page alone.
  84. }
  85.  
  86. // The top of watch7-content has become very large, so put a scrollbar on the whole lot.
  87. var watchDiscussion = /* document.getElementById("watch-discussion") || */ document.getElementById("watch7-content");
  88. if (watchDiscussion) {
  89. var toSubtract = 464; // Small video screen
  90. //var toSubtract = 544; // Medium size video screen
  91. var roomForComments = window.innerHeight - toSubtract;
  92. if (roomForComments < 200) {
  93. roomForComments = 200;
  94. }
  95. watchDiscussion.style.overflow = "auto";
  96. watchDiscussion.style.maxHeight = roomForComments+"px"; /* For a video height 360p */
  97. GM_addStyle(" #watch7-content { border: 1px solid; border-color: #c8c8c8 #dddddd #dddddd #c8c8c8; margin-top: 5px; } #watch-header { margin-top: 0px; margin-bottom: 0px; } ");
  98. }
  99.  
  100. var watchSidebar = document.getElementById("watch-sidebar") || document.getElementById("watch7-sidebar");
  101. if (watchSidebar) {
  102. watchSidebar.style.overflow = "auto";
  103. watchSidebar.style.maxHeight = (window.innerHeight - 69)+"px";
  104. // May 2012 - fixes below no longer needed.
  105. // Now the text wraps because of the scrollbar, so we widen the element:
  106. // watchSidebar.style.width = (320+24)+"px";
  107. // watchSidebar.style.width = '300px';
  108. // And we must widen its container also:
  109. // TODO BUG: Why does this work in the console, but not from the userscript?
  110. // document.getElementById("watch-main").style.width = (960+24)+"px";
  111. GM_addStyle(" #watch-sidebar, #watch7-sidebar { border: 1px solid; border-color: #c8c8c8 #dddddd #dddddd #c8c8c8; } ");
  112. // Without this, the Google notifications dropdown will appear behind our sidebar.
  113. // But with it, the sidebar is not clickable! :-(
  114. //GM_addStyle(" #watch-sidebar, #watch7-sidebar { z-index: -20; } ");
  115. }
  116.  
  117. if (scrollDownToVideo) {
  118. // Un-float the header:
  119. GM_addStyle(" #masthead-positioner { position: initial; } #masthead-positioner-height-offset { height: 0px; } ");
  120. //// Title text
  121. // document.getElementById("eow-title").scrollIntoView();
  122. //// Uploader info and videolist popdown.
  123. // document.getElementById("watch-headline-user-info").scrollIntoView();
  124. //// The author's video list (was supposed to be a small gap above the video when collapsed, but it's not)
  125. // document.getElementById("watch-more-from-user").scrollIntoView();
  126. //// The video
  127. // document.getElementsByTagName("embed")[0].scrollIntoView();
  128. //// The video
  129. var watchVideo = document.getElementById("movie_player") || document.getElementById("player-api");
  130. if (watchVideo) {
  131. watchVideo.scrollIntoView();
  132. }
  133. //// Slight gap above the video (I prefer that)
  134. // var watchContainer = document.getElementById("watch7-container");
  135. // if (watchContainer) {
  136. // watchContainer.scrollIntoView();
  137. // }
  138. }
  139.  
  140. // ~ October 2014
  141. // This feels like a bug in Chrome 38. The element is given position:absolute but it does not move up when its parent does! We can fix it anyway:
  142. GM_addStyle(" #watch8-secondary-actions { position: initial; } ");
  143. },1000);
  144.  
  145. }
  146.  
  147.  
  148.  
  149. if (animateThumbnails) {
  150.  
  151. // == Thumbnail animation ==
  152. // TODO: This is working fine on "related videos" thumbnails, but not on queue
  153. // thumbnails, even if I have the queue open when I load the page.
  154. // Perhaps we are responding to a mouseout event from a child element, because
  155. // we are not checking the event target like we should do.
  156. function initThumbnailAnimator() {
  157. // function createThumbnailAnimatorEvent(thumbImage) {
  158. var filenameRE = /\/([^/]*)\.(jpg|webp)(\?.*|$)/;
  159. var thumbImage = null;
  160. var originalHref = null;
  161. var timer = null;
  162. //var frames = ["1.jpg","2.jpg","3.jpg"]; // "default.jpg",
  163. var frames = ["1","2","3"];
  164. var frameI = 0;
  165. function changeFrame() {
  166. frameI = (frameI + 1) % frames.length;
  167. var match = originalHref.match(filenameRE);
  168. var extension = match[2];
  169. var filename = frames[frameI] + '.' + extension;
  170. thumbImage.src = originalHref.replace(filenameRE, '/' + filename);
  171. }
  172. function startAnimation() {
  173. // Because there was a bug that the running animation would not stop!
  174. if (timer) {
  175. clearInterval(timer);
  176. }
  177. originalHref = thumbImage.src;
  178. if (originalHref.match(/^data:/)) {
  179. return;
  180. }
  181. // We make this check quite late, due to lazy loading
  182. if (originalHref.match(filenameRE)) {
  183. // logElem("Starting animation",thumbImage);
  184. timer = setInterval(changeFrame,600);
  185. }
  186. }
  187. function stopAnimation() {
  188. if (timer) {
  189. // logElem("Stopping animation",thumbImage);
  190. clearInterval(timer);
  191. timer = null;
  192. // This isn't really neccessary, except to ensure the check for default\.jpg above works next time!
  193. //thumbImage.src = thumbImage.src.replace(/\/[^/]*$/,'') + '/' + "default.jpg";
  194. thumbImage.src = originalHref;
  195. }
  196. }
  197. function logElem(name,elem) {
  198. report = "<"+elem.tagName+" id="+elem.id+" class="+elem.className+" src="+elem.src+" />";
  199. GM_log(name+" = "+report);
  200. }
  201. function check(fn) {
  202. return function(evt) {
  203. // logElem("["+evt.type+"] evt.target",evt.target);
  204. var elemToCheck = evt.target || evt.srcElement;
  205. if (elemToCheck.tagName == "IMG") {
  206. thumbImage = elemToCheck;
  207. return fn();
  208. } else if (elemToCheck.className=='screen') {
  209. var seekImg = elemToCheck.parentNode.getElementsByTagName("img")[0];
  210. if (seekImg) {
  211. thumbImage = seekImg;
  212. return fn();
  213. }
  214. // } else {
  215. // var imgCount = elemToCheck.getElementsByTagName("img").length;
  216. // if (imgCount == 1) {
  217. // thumbImage = elemToCheck.getElementsByTagName("img")[0];
  218. // // logElem("["+evt.type+"] checking sub-image",thumbImage);
  219. // logElem("Whilst checking",elemToCheck);
  220. // logElem(" Animating elem",thumbImage);
  221. // logElem(" with parent",thumbImage.parentNode);
  222. // logElem(" whilst currentTarget",evt.currentTarget);
  223. // logElem(" and srcElement",evt.srcElement);
  224. // return fn();
  225. // }
  226. }
  227. };
  228. }
  229. //// Unfortunately these do not fire on any HTMLImageElements when browsing the queue.
  230. document.body.addEventListener("mouseover",check(startAnimation),false);
  231. document.body.addEventListener("mouseout",check(stopAnimation),false);
  232. // var videoList = document.getElementById("watch-sidebar"); // or watch-module or watch-module-body or watch-related or watch-more-related
  233. // var videoList = document.getElementsByClassName("video-list")[0]; // can be 4 of these!
  234. // var thumbs = document.getElementsByTagName("img");
  235. // for (var i=0;i<thumbs.length;i++) {
  236. // createThumbnailAnimatorEvent(thumbs[i]);
  237. // }
  238. }
  239. setTimeout(initThumbnailAnimator,1000);
  240.  
  241. }
  242.  
  243.  
  244.  
  245. if (unfloatTheHeader) {
  246. GM_addStyle("#masthead-positioner { position: static; }");
  247. var gapElement = document.getElementById("masthead-positioner-height-offset");
  248. if (gapElement) {
  249. gapElement.parentNode.removeChild(gapElement);
  250. }
  251. }
  252.