FYTE /Fast YouTube Embedded/ Player

Hugely improves load speed of pages with lots of embedded Youtube videos by instantly showing clickable and immediately accessible placeholders, then the thumbnails are loaded in background. A fast simple HTML5 direct playback (720p max) can be selected if available for the video.

当前为 2016-08-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name FYTE /Fast YouTube Embedded/ Player
  3. // @description Hugely improves load speed of pages with lots of embedded Youtube videos by instantly showing clickable and immediately accessible placeholders, then the thumbnails are loaded in background. A fast simple HTML5 direct playback (720p max) can be selected if available for the video.
  4. // @description:ru На порядок ускоряет время загрузки страниц с большим количеством вставленных Youtube-видео. С первого момента загрузки страницы появляются заглушки для видео, которые можно щелкнуть для загрузки плеера, и почти сразу же появляются кавер-картинки с названием видео. В опциях можно включить режим использования упрощенного браузерного плеера (макс. 720p).
  5. // @version 2.6.0
  6. // @include *
  7. // @exclude https://www.youtube.com/*
  8. // @author wOxxOm
  9. // @namespace wOxxOm.scripts
  10. // @license MIT License
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_addStyle
  14. // @grant GM_xmlhttpRequest
  15. // @connect www.youtube.com
  16. // @connect youtube.com
  17. // @run-at document-start
  18. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACABAMAAAAxEHz4AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAwUExURUxpcQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJuxkb8AAAAPdFJOUwDvH0biMvjOZFW6pxJ6kh7r3iMAAAPDSURBVGje7ZlNaBNBFMeXhNDWpkKgFg9iYlBB6SGwiPQkftaCYATjTaRKiwi1xVaKXpqTpHhosR66p4pQhb209CQGbSweK/TiRYwfFy+NkWBM2pR2nHm73abJzuceRMj/kEzSvl92Z9689+atpjXUUEMN1WgpoRupbH41nTbNUaxzlkIhe0C+M810Ov8zmzL0RGeNeeDThUEkof72N/Fqe/8LJK07sR173yJS0EbEATxFSurZtm9DilqxAV9VAZuWfbPbLBOFqtSBP9f/WxIAV2Bc6H5owiKPG7p+IpFIRG11LsPbEfyVrhvTqeyX1dfmaBiM9gFgjgwrTzJSfncMFq7s3EExJuu5/rHte3hPBvfkff84sbuEBxPkUiLygCC5hDV7CvpUtt81axICZBN9UwHsxYalOMxhIaIC8IVhFlvJtlALIWQl57Um/LquBpjBpkOwin1qADKLB7RD9moqiPz2TcAMqQGa4OI9Av5op/DrMzXAHmz6mw4IxEQA67AW825/bhngAVoBMEHzZD+aFQCsQUCkAAor/M2wCYAVdwCqxJmANgD8cmJjPQDt5wK22AD0nAVoBsAiE1BMcgAbAJikAqoTYP1CA4BEtBgdgC6yARUuAC3QI7sDiLMAxUk2YAwiIwNAn4YAhGU+YKcOqAUMCgJQziugHGMALmNAhANAWxkaoEgABS4ADdMyiyiglPMIcJ0GKQAayDAAGQEAuu8VUB/gJAH1AS4IgLAwAA24AAoygAeuAPFbqHPHoNwc1HuCJCDncRl7NG8At7Ak48qugVEGsOBxO7snB58T0ngASlwWjomFpMegOusxrFOLBCexsFMbvUzxCyVXRqEkBpjlpXdOgcEqFlsEKpRynFviMIus0md+kcUEDAuUeaxCcysjUGgySt1yTKTUZRTbOaFim17unxUr92doBw4f9zTKObGInZl+//NTW592VP3g+Q4Onh6Ovjfgt5vsPoSCJuDuPRz/58CFmhEtKPIEvY8kZAd3VxRxRJJSyIXcUu0/VOz3okITJRC2ex9kGdB5ecBVZLtgCyt70fUB2nGTTjOu/HFZohsXXLoOrbQKfDps1ePtTj9wSter2oGWoBnYRZqB+bQ5OnLaShpnrNAz6N6R7OW1I1HJjnmPVFuit7eDV1jNvuAkpJNqgJ0DQPCHiv3dqmULfJe3P7hrB/oej3T0S/Tme7tf1Xp/MArPB/Ayp82X5OlAaJfI8wHsJ2/zWXg6EGV4XXB5CbuN3mUYxnQKNI6HU9i3op0y3tpQQw39b/oLfDt0HcsiqWsAAAAASUVORK5CYII=
  19. // @compatible chrome
  20. // @compatible firefox
  21. // @compatible opera
  22. // ==/UserScript==
  23.  
  24. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  25.  
  26. var resizeMode = GM_getValue('resize', 'Fit to width');
  27. if (typeof resizeMode != 'string')
  28. resizeMode = resizeMode ? 'Fit to width' : 'Original';
  29.  
  30. var resizeWidth = GM_getValue('width', 1280) |0;
  31. var resizeHeight = GM_getValue('height', 720) |0;
  32. updateCustomSize();
  33.  
  34. var playDirectly = !!GM_getValue('playHTML5', false);
  35. var skipCustom = !!GM_getValue('skipCustom', true);
  36. var showStoryboard = !!GM_getValue('showStoryboard', true);
  37. var pinnable = GM_getValue('pinnable', 'on');
  38. if (!/^(on|hide|off)$/.test(pinnable))
  39. pinnable = !!pinnable ? 'on' : 'hide';
  40.  
  41. var _ = initTL();
  42.  
  43. var imageLoader = document.createElement('img');
  44. var imageLoader2 = document.createElement('img');
  45.  
  46. var fytedom = document.getElementsByClassName('instant-youtube-container');
  47. var iframes = document.getElementsByTagName('iframe');
  48. var oembeds = document.getElementsByTagName('embed');
  49. var persite = (function() {
  50. var rules = [
  51. {host: /(^|\.)google\.\w{2,3}(\.\w{2,3})?$/, class:'g-blk', query: 'a[href*="youtube.com/watch"][data-ved]', eatparent: 1},
  52. {host: 'pikabu.ru', class:'b-video', match: '[data-url*="youtube.com/embed"]', attr: 'data-url'},
  53. {host: 'androidauthority.com', eatparent: '.video-container'},
  54. {host: 'reddit.com',
  55. match: '[data-url*="youtube.com/"] [src*="/mediaembed"], [data-url*="youtu.be/"] [src*="/mediaembed"]',
  56. src: function(e) { return e.closest('[data-url*="youtube.com/"], [data-url*="youtu.be/"]').dataset.url }},
  57. {host: '9gag.com', eatparent: 0},
  58. ];
  59. for (var i=0, rule; (i<rules.length) && (rule=rules[i]); i++) {
  60. var rx = rule.host instanceof RegExp ? rule.host : new RegExp('(^|\\.)' + rule.host.replace(/\./g, '\\.') + '$', 'i');
  61. if (rx.test(location.hostname)) {
  62. if (!rule.tag && !rule.class)
  63. rule.tag = 'iframe';
  64. if (!rule.match && !rule.query)
  65. rule.match = '[src*="youtube.com/embed"]';
  66. return {
  67. nodes: rule.class ? document.getElementsByClassName(rule.class) : document.getElementsByTagName(rule.tag),
  68. match: rule.match ? function(e) { return e.matches(rule.match) ? e : null }
  69. : function(e) { return e.querySelector(rule.query) },
  70. attr: rule.attr,
  71. src: rule.src,
  72. eatparent: rule.eatparent,
  73. };
  74. }
  75. }
  76. })();
  77.  
  78. findEmbeds();
  79. injectStylesIfNeeded();
  80. new MutationObserver(findEmbeds).observe(document, {subtree:true, childList:true});
  81.  
  82. document.addEventListener('DOMContentLoaded', function(e) {
  83. injectStylesIfNeeded();
  84. adjustNodesIfNeeded(e);
  85. });
  86. window.addEventListener('resize', adjustNodesIfNeeded, true);
  87. window.addEventListener('message', function(e) {
  88. if (e.data == 'iframe-allowfs') {
  89. $$('iframe:not([allowfullscreen])').some(function(iframe) {
  90. if (iframe.contentWindow == e.source) {
  91. iframe.allowFullscreen = true;
  92. return true;
  93. }
  94. });
  95. if (window != window.top)
  96. window.parent.postMessage('iframe-allowfs', '*');
  97. }
  98. });
  99.  
  100. function findEmbeds(mutations) {
  101. var i, len, e;
  102. if (mutations && mutations.length == 1 && !mutations[0].addedNodes.length)
  103. return;
  104. if (persite)
  105. for (i=0, len=persite.nodes.length; (i<len) && (e=persite.nodes[i]); i++)
  106. if (e = persite.match(e))
  107. processEmbed(e, persite.src && persite.src(e) || e.getAttribute(persite.attr));
  108. for (i=0, len=iframes.length; (i<len) && (e=iframes[i]); i++)
  109. if (/youtube\.com(\/|%2F)(embed|v)(\/|%2F)/i.test(e.src))
  110. processEmbed(e);
  111. for (i=0, len=oembeds.length; (i<len) && (e=oembeds[i]); i++)
  112. if (/youtube\.com(\/|%2F)(embed|v)\//i.test(e.src))
  113. processEmbed(e);
  114. }
  115.  
  116. function processEmbed(node, src) {
  117. function decodeEmbedUrl(url) {
  118. return url.indexOf('youtube.com%2Fembed') > 0
  119. ? decodeURIComponent(url.replace(/^.*?(http[^&?=]+?youtube.com%2Fembed[^&]+).*$/i, '$1'))
  120. : url;
  121. }
  122. src = src || node.src || node.href || '';
  123. var n = node;
  124. var np = n.parentNode, npw;
  125. var srcFixed = decodeEmbedUrl(src).replace(/\/(watch\?v=|v\/)/, '/embed/');
  126. if (src.indexOf('cdn.embedly.com/') > 0 ||
  127. resizeMode != 'Original' && np && np.children.length == 1 && !np.className && !np.id)
  128. {
  129. n = location.hostname == 'disqus.com' ? np.parentNode : np;
  130. np = n.parentElement;
  131. }
  132. if (!np ||
  133. !np.parentNode ||
  134. skipCustom && srcFixed.indexOf('enablejsapi=1') > 0 ||
  135. node.matches('.instant-youtube-embed, .YTLT-embed') ||
  136. node.onload // skip some retarded loaders
  137. )
  138. return;
  139.  
  140. var id = srcFixed.match(/(?:embed\/|v[=\/]|youtu\.be\/)([^\s,.()\[\]?]+?)(?:[&?\/].*|$)/);
  141. if (!id)
  142. return;
  143. id = id[1];
  144.  
  145. var autoplay = srcFixed.indexOf('autoplay=1') > 0;
  146.  
  147. if (np.localName == 'object')
  148. n = np, np = n.parentElement;
  149.  
  150. var eatparent = persite && persite.eatparent || 0;
  151. if (typeof eatparent == 'string')
  152. n = np.closest(eatparent) || n, np = n.parentElement;
  153. else
  154. while (eatparent--)
  155. n = np, np = n.parentElement;
  156.  
  157. injectStylesIfNeeded('force');
  158.  
  159. var div = document.createElement('div');
  160. div.className = 'instant-youtube-container';
  161. div.FYTE = {
  162. state: 'querying',
  163. srcEmbed: srcFixed.replace(/&$/, ''),
  164. videoID: id,
  165. originalWidth: /%/.test(node.width) ? 320 : node.width|0 || n.clientWidth|0,
  166. originalHeight: /%/.test(node.height) ? 200 : node.height|0 || n.clientHeight|0,
  167. };
  168. div.FYTE.srcEmbedFixed = div.FYTE.srcEmbed.replace(/^http:/, 'https:').replace(/&?wmode=\w+/, '').replace(/[?&]feature=oembed/, '');
  169. div.FYTE.srcWatchFixed = div.FYTE.srcEmbedFixed.replace(/\/embed\//, '/watch?v=');
  170.  
  171. var divSize = calcContainerSize(div, n);
  172. var origStyle = getComputedStyle(n);
  173. div.style.cssText = important('background-color:transparent; transition:background-color 2s;' +
  174. (origStyle.hasOwnProperty('position') ? Object.keys(origStyle) : Object.keys(origStyle.__proto__) /*FF*/)
  175. .filter(function(k) { return !!k.match(/^(position|left|right|top|bottom)$/) })
  176. .map(function(k) { return k + ':' + origStyle[k] })
  177. .join(';')
  178. .replace(/\b[^;:]+:\s*(auto|static|block)\s*(!\s*important)?;/g, '') +
  179. (origStyle.display == 'inline' ? ';display:inline-block;width:100%' : '') +
  180. ';min-width:' + Math.min(divSize.w, div.FYTE.originalWidth) + 'px' +
  181. ';min-height:' + Math.min(divSize.h, div.FYTE.originalHeight) + 'px' +
  182. (resizeMode == 'Fit to width' ? ';width:100%' : '') +
  183. ';max-width:' + divSize.w + 'px; height:' + (persite && persite.eatparent === 0 ? '100%' : divSize.h + 'px;'));
  184. if (!autoplay) {
  185. setTimeout(function() { div.style.backgroundColor = '' }, 0);
  186. setTimeout(function() { div.style.transition = '' }, 2000);
  187. }
  188.  
  189. // consume parents of retardedly positioned videos
  190. if (div.style.position.match('absolute|relative')) {
  191. if (np.children.length == 1 && floatPadding(np, getComputedStyle(np, ':after'), 'Top') >= div.FYTE.originalHeight)
  192. n = np, np = n.parentElement;
  193. div.style.cssText = div.style.cssText.replace(/\b(position|left|top|right|bottom):[^;]+/g, '');
  194. }
  195.  
  196. var wrapper = div.appendChild(document.createElement('div'));
  197. wrapper.className = 'instant-youtube-wrapper';
  198.  
  199. var img = wrapper.appendChild(document.createElement('img'));
  200. img.className = 'instant-youtube-thumbnail';
  201. img.src = 'https://i.ytimg.com/vi/' + id + '/maxresdefault.jpg';
  202. img.style.cssText = important('transition:opacity 0.1s ease-out; opacity:0; padding:0; margin:auto; position:absolute; left:0; right:0; top:0; bottom:0; max-width:none; max-height:none;');
  203.  
  204. img.title = _('Shift-click to use alternative player');
  205. img.onload = function(e) {
  206. if (img.naturalWidth <= 120)
  207. return img.onerror(e);
  208. var fitToWidth = true;
  209. if (img.naturalHeight) {
  210. var ratio = img.naturalWidth / img.naturalHeight;
  211. if (ratio > 4.1/3 && ratio < divSize.w/divSize.h) {
  212. img.style.cssText += important('width:auto; height:100%;');
  213. fitToWidth = false;
  214. }
  215. }
  216. if (fitToWidth) {
  217. img.style.cssText += important('width:100%; height:auto;');
  218. }
  219. if (div.FYTE.videoWidth)
  220. fixThumbnailAR(div);
  221. if (!autoplay)
  222. img.style.opacity = 1;
  223. };
  224. img.onerror = function(e) {
  225. if (img.src.indexOf('maxresdefault') > 0)
  226. img.src = img.src.replace('maxresdefault','hqdefault');
  227. };
  228.  
  229. GM_xmlhttpRequest({
  230. method: 'GET',
  231. url: 'https://www.youtube.com/get_video_info?video_id=' + div.FYTE.videoID + '&el=detailpage',
  232. headers: {'Accept-Encoding': 'gzip'},
  233. context: div,
  234. onload: parseVideoInfo
  235. });
  236.  
  237. translateHTML(wrapper, 'beforeend', '\
  238. <a class="instant-youtube-title" target="_blank" href="' + div.FYTE.srcWatchFixed + '">&nbsp;</a>\
  239. <svg class="instant-youtube-play-button"><path fill-rule="evenodd" clip-rule="evenodd" fill="#1F1F1F" class="ytp-large-play-button-svg" d="M84.15,26.4v6.35c0,2.833-0.15,5.967-0.45,9.4c-0.133,1.7-0.267,3.117-0.4,4.25l-0.15,0.95c-0.167,0.767-0.367,1.517-0.6,2.25c-0.667,2.367-1.533,4.083-2.6,5.15c-1.367,1.4-2.967,2.383-4.8,2.95c-0.633,0.2-1.316,0.333-2.05,0.4c-0.767,0.1-1.3,0.167-1.6,0.2c-4.9,0.367-11.283,0.617-19.15,0.75c-2.434,0.034-4.883,0.067-7.35,0.1h-2.95C38.417,59.117,34.5,59.067,30.3,59c-8.433-0.167-14.05-0.383-16.85-0.65c-0.067-0.033-0.667-0.117-1.8-0.25c-0.9-0.133-1.683-0.283-2.35-0.45c-2.066-0.533-3.783-1.5-5.15-2.9c-1.033-1.067-1.9-2.783-2.6-5.15C1.317,48.867,1.133,48.117,1,47.35L0.8,46.4c-0.133-1.133-0.267-2.55-0.4-4.25C0.133,38.717,0,35.583,0,32.75V26.4c0-2.833,0.133-5.95,0.4-9.35l0.4-4.25c0.167-0.966,0.417-2.05,0.75-3.25c0.7-2.333,1.567-4.033,2.6-5.1c1.367-1.434,2.967-2.434,4.8-3c0.633-0.167,1.333-0.3,2.1-0.4c0.4-0.066,0.917-0.133,1.55-0.2c4.9-0.333,11.283-0.567,19.15-0.7C35.65,0.05,39.083,0,42.05,0L45,0.05c2.467,0,4.933,0.034,7.4,0.1c7.833,0.133,14.2,0.367,19.1,0.7c0.3,0.033,0.833,0.1,1.6,0.2c0.733,0.1,1.417,0.233,2.05,0.4c1.833,0.566,3.434,1.566,4.8,3c1.066,1.066,1.933,2.767,2.6,5.1c0.367,1.2,0.617,2.284,0.75,3.25l0.4,4.25C84,20.45,84.15,23.567,84.15,26.4z M33.3,41.4L56,29.6L33.3,17.75V41.4z"></path><polygon fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" points="33.3,41.4 33.3,17.75 56,29.6"></polygon></svg>\
  240. <span tl class="instant-youtube-link">' + (playDirectly ? 'Play with Youtube player' : 'Play directly (up to 720p)') + '</span>\
  241. <div tl class="instant-youtube-options-button">Options</div>\
  242. ');
  243.  
  244. np.insertBefore(div, n);
  245. n.remove();
  246.  
  247. if (autoplay)
  248. return startPlaying(div);
  249.  
  250. div.addEventListener('click', clickHandler);
  251. }
  252.  
  253. function adjustNodesIfNeeded(e) {
  254. if (!fytedom[0])
  255. return;
  256. if (adjustNodesIfNeeded.scheduled)
  257. clearTimeout(adjustNodesIfNeeded.scheduled);
  258. adjustNodesIfNeeded.scheduled = setTimeout(function() {
  259. adjustNodes(e);
  260. adjustNodesIfNeeded.scheduled = 0;
  261. }, 16);
  262. }
  263.  
  264. function adjustNodes(e, clickedContainer) {
  265. var force = !!clickedContainer;
  266. var nearest = force ? clickedContainer : null;
  267.  
  268. var vids = $$('.instant-youtube-container:not([pinned])');
  269.  
  270. if (!nearest && e.type != 'DOMContentLoaded') {
  271. var minDistance = window.innerHeight*3/4 |0;
  272. var nearTargetY = window.innerHeight/2;
  273. vids.forEach(function(n) {
  274. var bounds = n.getBoundingClientRect();
  275. var distance = Math.abs((bounds.bottom + bounds.top)/2 - nearTargetY);
  276. if (distance < minDistance) {
  277. minDistance = distance;
  278. nearest = n;
  279. }
  280. });
  281. }
  282.  
  283. if (nearest) {
  284. var bounds = nearest.getBoundingClientRect();
  285. var nearestCenterYpct = (bounds.top + bounds.bottom)/2 / window.innerHeight;
  286. }
  287.  
  288. var resized = false;
  289.  
  290. vids.forEach(function(n) {
  291. var size = calcContainerSize(n);
  292. var w = size.w, h = size.h;
  293.  
  294. // prevent parent clipping
  295. for (var e=n.parentElement, style; e; e=e.parentElement)
  296. if (e.style.overflow != 'visible' && (style=getComputedStyle(e)))
  297. if ((style.overflow+style.overflowX+style.overflowY).match(/hidden|scroll/))
  298. if (n.offsetTop < e.clientHeight / 2 && n.offsetTop + n.clientHeight > e.clientHeight)
  299. e.style.cssText = e.style.cssText.replace(/\boverflow(-[xy])?:[^;]+/g, '') +
  300. important('overflow:visible;overflow-x:visible;overflow-y:visible;');
  301.  
  302. if (force && Math.abs(w - parseFloat(n.style.maxWidth)) <= 2)
  303. return;
  304.  
  305. if (n.style.maxWidth != w + 'px') n.style.maxWidth = w + 'px';
  306. if (n.style.height != h + 'px') n.style.height = h + 'px';
  307. if (parseFloat(n.style.minWidth) > w) n.style.minWidth = n.style.maxWidth;
  308. if (parseFloat(n.style.minHeight) > h) n.style.minHeight = n.style.height;
  309.  
  310. fixThumbnailAR(n);
  311. resized = true;
  312. });
  313.  
  314. if (resized && nearest)
  315. setTimeout(function() {
  316. var bounds = nearest.getBoundingClientRect();
  317. var h = bounds.bottom - bounds.top;
  318. var projectedCenterY = nearestCenterYpct * window.innerHeight;
  319. var projectedTop = projectedCenterY - h/2;
  320. var safeTop = Math.min(Math.max(0, projectedTop), window.innerHeight - h);
  321. window.scrollBy(0, bounds.top - safeTop);
  322. }, 16);
  323. }
  324.  
  325. function calcContainerSize(div, origNode) {
  326. var w, h;
  327. origNode = origNode || div;
  328. switch (resizeMode) {
  329. case 'Original':
  330. w = div.FYTE.originalWidth;
  331. h = div.FYTE.originalHeight;
  332. break;
  333. case 'Custom':
  334. w = resizeWidth;
  335. h = resizeHeight;
  336. break;
  337. case '1080p':
  338. case '720p':
  339. case '480p':
  340. case '360p':
  341. h = parseInt(resizeMode);
  342. w = h / 9 * 16;
  343. break;
  344. default: // fit-to-width mode
  345. var n = origNode;
  346. do {
  347. n = n.parentElement;
  348. // find parent node with nonzero width (i.e. independent of our video element)
  349. } while (n && !(w = n.clientWidth));
  350. if (w)
  351. h = w / 16 * 9;
  352. else {
  353. w = origNode.clientWidth;
  354. h = origNode.clientHeight;
  355. }
  356. }
  357. var np = origNode.parentElement;
  358. var style = getComputedStyle(np);
  359. var parentWidth = parseFloat(style.width) - floatPadding(np, style, 'Left') - floatPadding(np, style, 'Right');
  360. if (parentWidth > 0 && parentWidth < w) {
  361. h = parentWidth / w * h;
  362. w = parentWidth;
  363. }
  364. if (resizeMode == 'Fit to width' && h < div.FYTE.originalHeight*0.9)
  365. h = Math.min(div.FYTE.originalHeight, w / div.FYTE.originalWidth * div.FYTE.originalHeight);
  366.  
  367. return {w: window.chrome ? w : Math.round(w), h:h};
  368. }
  369.  
  370. function parseVideoInfo(response) {
  371. var div = response.context;
  372. var txt = response.responseText;
  373. var info = tryCatch(function() { return JSON.parse(txt.replace(/(\w+)=?(.*?)(&|$)/g, '"$1":"$2",').replace(/^(.+?),?$/, '{$1}')) }) || {};
  374. var videoSources = [];
  375.  
  376. // parse width & height to adjust the thumbnail
  377. var m = decodeURIComponent(info.adaptive_fmts || '').match(/size=(\d+)x(\d+)\b/) ||
  378. decodeURIComponent(txt).match(/\/(\d+)x(\d+)\//);
  379. if (m)
  380. fixThumbnailAR(div, m[1]|0, m[2]|0);
  381.  
  382. // parse video sources
  383. if (info.url_encoded_fmt_stream_map && info.fmt_list) {
  384. var streams = {};
  385. decodeURIComponent(info.url_encoded_fmt_stream_map).split(',').forEach(function(stream) {
  386. var params = {};
  387. stream.split('&').forEach(function(kv) {
  388. params[kv.split('=')[0]] = decodeURIComponent(kv.split('=')[1]);
  389. });
  390. streams[params.itag] = params;
  391. });
  392. decodeURIComponent(info.fmt_list).split(',').forEach(function(fmt) {
  393. var itag = fmt.split('/')[0];
  394. var dimensions = fmt.split('/')[1];
  395. var stream = streams[itag];
  396. if (stream) {
  397. videoSources.push({
  398. src: stream.url,
  399. title: stream.quality + ', ' + dimensions + ', ' + stream.type
  400. });
  401. }
  402. });
  403. } else {
  404. var rx = /url=([^=]+?mime%3Dvideo%252F(?:mp4|webm)[^=]+?)(?:,quality|,itag|.u0026)/g;
  405. var text = decodeURIComponent(txt).split('url_encoded_fmt_stream_map')[1];
  406. while (m = rx.exec(text)) {
  407. videoSources.push({
  408. src: decodeURIComponent(decodeURIComponent(m[1]))
  409. });
  410. }
  411. }
  412.  
  413. var duration = div.FYTE.duration = info.length_seconds|0 || '';
  414. if (duration) {
  415. var d = new Date(null);
  416. d.setSeconds(duration);
  417. duration = d.toISOString().replace(/^.+?T[0:]{0,4}(.+?)\..+$/, '<span>$1</span>');
  418. }
  419. var title = decodeURIComponent(info.title || info.reason || '').replace(/\+/g, ' ');
  420. if (title || duration)
  421. $(div, '.instant-youtube-title').innerHTML = (title ? '<strong>' + title + '</strong>' : '') + duration;
  422. if (pinnable != 'off' && info.title)
  423. makeDraggable(div);
  424. if (info.reason)
  425. div.setAttribute('disabled', '');
  426.  
  427. if (videoSources.length)
  428. div.FYTE.videoSources = videoSources;
  429.  
  430. if (info.storyboard_spec) {
  431. m = decodeURIComponent(decodeURIComponent(info.storyboard_spec)).split('|');
  432. div.FYTE.storyboard = JSON.parse(m[m.length-1].replace(/^(\d+)#(\d+)#(\d+)#(\d+)#(\d+)#.+$/, '{"w":$1, "h":$2, "len":$3, "rows":$4, "cols":$5}'));
  433. div.FYTE.storyboard.url = m[0].replace('$L/$N.jpg', (m.length-2) + '/M0.jpg?sigh=' + m[m.length-1].replace(/^.+?#([^#]+)$/, '$1'));
  434. $(div, '.instant-youtube-options-button').insertAdjacentHTML('beforebegin',
  435. '<div class="instant-youtube-storyboard"' + (showStoryboard ? '' : ' disabled') + '>' +
  436. important('<div style="width:' + (div.FYTE.storyboard.w-1) + 'px; height:' + div.FYTE.storyboard.h + 'px;') +
  437. '">&nbsp;</div>' +
  438. '</div>');
  439. if (showStoryboard)
  440. updateHoverHandler(div);
  441. }
  442.  
  443. injectStylesIfNeeded();
  444.  
  445. if (div.FYTE.state == 'scheduled play')
  446. setTimeout(function() { startPlayingDirectly(div) }, 0);
  447.  
  448. div.FYTE.state = '';
  449. }
  450.  
  451. function fixThumbnailAR(div, w, h) {
  452. var img = $(div, 'img');
  453. var thw = img.naturalWidth, thh = img.naturalHeight;
  454. if (w && h) { // means thumbnail is still loading
  455. div.FYTE.videoWidth = w;
  456. div.FYTE.videoHeight = h;
  457. } else {
  458. w = div.FYTE.videoWidth;
  459. h = div.FYTE.videoHeight;
  460. if (!w || !h)
  461. return;
  462. }
  463. var divw = div.clientWidth, divh = div.clientHeight;
  464. // if both video and thumbnail are 4:3, fit the image to height
  465. //console.log(div, divw, divh, thw, thh, w, h, h/w*divw / divh - 1, thh/thw*divw / divh - 1);
  466. if (Math.abs(h/w*divw / divh - 1) > 0.05 && Math.abs(thh/thw*divw / divh - 1) > 0.05) {
  467. img.style.maxHeight = img.clientHeight + 'px';
  468. if (!div.FYTE.videoWidth) // skip animation if thumbnail is already loaded
  469. img.style.transition = 'height 1s ease, margin-top 1s ease';
  470. setTimeout(function() {
  471. img.style.maxHeight = 'none';
  472. img.style.cssText += important(h/w >= divh/divw ? 'width:auto; height:100%;' : 'width:100%; height:auto;');
  473. setTimeout(function() {
  474. img.style.transition = '';
  475. }, 1000);
  476. }, 0);
  477. }
  478. }
  479.  
  480. function updateHoverHandler(div) {
  481. var sb = $(div, '.instant-youtube-storyboard');
  482. if (!showStoryboard) {
  483. if (!sb.getAttribute('disabled'))
  484. sb.setAttribute('disabled', '');
  485. return;
  486. }
  487. if (sb.hasAttribute('disabled'))
  488. sb.removeAttribute('disabled');
  489.  
  490. sb.addEventListener('click', storyboardClickHandler);
  491.  
  492. var oldIndex = null;
  493. var style = sb.firstElementChild.style;
  494. sb.addEventListener('mousemove', storyboardHoverHandler);
  495. sb.addEventListener('mouseout', storyboardHoverHandler);
  496.  
  497. div.addEventListener('mouseover', storyboardPreloader);
  498. div.addEventListener('mouseout', storyboardPreloader);
  499.  
  500. var spinner = document.createElement('span');
  501. spinner.className = 'instant-youtube-loading-button';
  502.  
  503. function storyboardClickHandler(e) {
  504. sb.removeEventListener('click', storyboardClickHandler);
  505. var offsetX = e.offsetX || e.clientX - this.getBoundingClientRect().left;
  506. div.FYTE.startAt = offsetX / this.clientWidth * div.FYTE.duration |0;
  507. div.FYTE.srcEmbedFixed = setUrlParams(div.FYTE.srcEmbedFixed, {start: div.FYTE.startAt});
  508. startPlaying(div, {alternateMode: e.shiftKey});
  509. }
  510.  
  511. function storyboardPreloader(e) {
  512. if (e.type == 'mouseout') {
  513. imageLoader.onload = null; imageLoader.src = '';
  514. spinner.remove();
  515. return;
  516. }
  517. if (!div.FYTE.storyboard || div.FYTE.storyboard.preloaded)
  518. return;
  519. var lastpart = (div.FYTE.storyboard.len-1)/(div.FYTE.storyboard.rows * div.FYTE.storyboard.cols) |0;
  520. if (lastpart <= 0)
  521. return;
  522. var part = 0;
  523. imageLoader.src = setStoryboardUrl(part++);
  524. imageLoader.onload = function() {
  525. if (part > lastpart) {
  526. div.FYTE.storyboard.preloaded = true;
  527. div.removeEventListener('mouseover', storyboardPreloader);
  528. div.removeEventListener('mouseout', storyboardPreloader);
  529. imageLoader.onload = null;
  530. imageLoader.src = '';
  531. spinner.remove();
  532. return;
  533. }
  534. imageLoader.src = setStoryboardUrl(part++);
  535. };
  536. }
  537.  
  538. function setStoryboardUrl(part) {
  539. return div.FYTE.storyboard.url.replace(/M\d+\.jpg\?/, 'M' + part + '.jpg?');
  540. }
  541.  
  542. function storyboardHoverHandler(e) {
  543. if (!showStoryboard || !div.FYTE.storyboard)
  544. return;
  545. if (e.type == 'mouseout')
  546. return imageLoader2.onload && imageLoader2.onload();
  547. var w = div.FYTE.storyboard.w;
  548. var h = div.FYTE.storyboard.h;
  549. var cols = div.FYTE.storyboard.cols;
  550. var rows = div.FYTE.storyboard.rows;
  551. var len = div.FYTE.storyboard.len;
  552. var partlen = rows * cols;
  553.  
  554. var offsetX = e.offsetX || e.clientX - this.getBoundingClientRect().left;
  555. var left = Math.min(this.clientWidth - w, Math.max(0, offsetX - w)) |0;
  556. if (!style.left || parseInt(style.left) != left) {
  557. style.left = left + 'px';
  558. if (spinner.parentElement)
  559. spinner.style.cssText = important('left:' + (left + w/2 - 10) + 'px; right:auto;');
  560. }
  561.  
  562. var index = Math.min(offsetX / this.clientWidth * (len+1) |0, len - 1);
  563. if (index == oldIndex)
  564. return;
  565.  
  566. var part = index/partlen|0;
  567. if (!oldIndex || part != (oldIndex/partlen|0)) {
  568. style.cssText = style.cssText.replace(/$|background-image[^;]+;/,
  569. 'background-image: url(' + setStoryboardUrl(part) + ')!important;');
  570. if (!div.FYTE.storyboard.preloaded) {
  571. if (spinner.timer)
  572. clearTimeout(spinner.timer);
  573. spinner.timer = setTimeout(function() {
  574. spinner.timer = 0;
  575. if (!imageLoader2.src)
  576. return;
  577. this.appendChild(spinner);
  578. spinner.style.cssText = important('left:' + (left + w/2 - 10) + 'px; right:auto;');
  579. }.bind(this), 50);
  580. imageLoader2.onload = function() {
  581. clearTimeout(spinner.timer);
  582. spinner.remove();
  583. spinner.timer = 0;
  584. imageLoader2.onload = null;
  585. imageLoader2.src = '';
  586. };
  587. imageLoader2.src = setStoryboardUrl(part);
  588. }
  589. }
  590.  
  591. oldIndex = index;
  592. index = index % partlen;
  593. style.backgroundPosition = '-' + (index % cols) * w + 'px -' + (index / cols |0) * h + 'px';
  594. }
  595. }
  596.  
  597. function clickHandler(e) {
  598. if (e.target.href || e.target.closest('a'))
  599. return;
  600. if (e.target.matches('.instant-youtube-options-button')) {
  601. showOptions(e);
  602. e.preventDefault();
  603. e.stopPropagation();
  604. return;
  605. }
  606. if (e.target.matches('.instant-youtube-options, .instant-youtube-options *'))
  607. return;
  608.  
  609. e.preventDefault();
  610. e.stopPropagation();
  611.  
  612. var alternateMode = e.shiftKey || e.target.className == 'instant-youtube-link';
  613. startPlaying(e.target.closest('.instant-youtube-container'), {alternateMode: alternateMode});
  614. }
  615.  
  616. function startPlaying(div, params) {
  617. div.removeEventListener('click', clickHandler);
  618.  
  619. $$(div, '.instant-youtube-wrapper > *:not(img):not(a)').forEach(function(e) { e.style.cssText = 'display:none!important' });
  620. $(div, 'svg').outerHTML = '<span class="instant-youtube-loading-button"></span>';
  621.  
  622. if (pinnable != 'off') {
  623. div.firstElementChild.insertAdjacentHTML('beforeend',
  624. '<div pin="top-left"></div><div pin="top-right"></div><div pin="bottom-right"></div><div pin="bottom-left"></div>');
  625. $$(div, '[pin]').forEach(function(pin) {
  626. if (pinnable == 'hide')
  627. pin.setAttribute('transparent', '');
  628. pin.onclick = function(e) {
  629. var pinIt = !div.hasAttribute('pinned') || !pin.hasAttribute('active');
  630. var corner = pin.getAttribute('pin');
  631. var video = $(div, 'video');
  632. if (pinIt) {
  633. $$(div, '[pin][active]').forEach(function(p) { p.removeAttribute('active') });
  634. pin.setAttribute('active', '');
  635. if (!div.FYTE.unpinnedStyle) {
  636. div.FYTE.unpinnedStyle = div.style.cssText;
  637. var stub = div.cloneNode();
  638. var img = $(div, 'img').cloneNode();
  639. img.style.opacity = 1;
  640. img.style.display = 'block';
  641. img.title = '';
  642. stub.appendChild(img);
  643. stub.onclick = function(e) { $(div, '[pin][active]').onclick(e) };
  644. stub.style.cssText += 'opacity:0.3!important;';
  645. stub.setAttribute('stub', '');
  646. div.FYTE.stub = stub;
  647. div.parentNode.insertBefore(stub, div);
  648. }
  649. div.style.cssText = important(
  650. 'position: fixed;' +
  651. 'contain: inherit;' +
  652. 'width: 400px;' +
  653. 'z-index: 999999999;' +
  654. 'height:' + (400 / div.FYTE.videoWidth * div.FYTE.videoHeight) + 'px;' +
  655. 'top:' + (corner.indexOf('top') >= 0 ? '0' : 'auto') + ';' +
  656. 'bottom:' + (corner.indexOf('bottom') >= 0 ? '0' : 'auto') + ';' +
  657. 'left:' + (corner.indexOf('left') >= 0 ? '0' : 'auto') + ';' +
  658. 'right:' + (corner.indexOf('right') >= 0 ? '0' : 'auto') + ';'
  659. );
  660. adjustPinnedOffset(div, div, corner);
  661. div.setAttribute('pinned', '');
  662. if (video && document.body)
  663. document.body.appendChild(div);
  664. } else {
  665. pin.removeAttribute('active');
  666. div.removeAttribute('pinned');
  667. div.style.cssText = div.FYTE.unpinnedStyle;
  668. div.FYTE.unpinnedStyle = '';
  669. if (div.FYTE.stub) {
  670. if (video && document.body)
  671. div.FYTE.stub.parentNode.replaceChild(div, div.FYTE.stub);
  672. div.FYTE.stub.remove();
  673. div.FYTE.stub = null;
  674. }
  675. }
  676. if (video && video.paused)
  677. video.play();
  678. };
  679. });
  680. if (params && params.pin)
  681. $(div, '[pin="' + params.pin + '"]').click();
  682. }
  683.  
  684. if (window != window.top)
  685. window.parent.postMessage('iframe-allowfs', '*');
  686.  
  687. if ((!!playDirectly + !!(params && params.alternateMode) == 1)
  688. && (div.FYTE.videoSources || div.FYTE.state == 'querying')) {
  689. if (div.FYTE.videoSources)
  690. startPlayingDirectly(div);
  691. else {
  692. // playback will start in parseVideoInfo
  693. div.FYTE.state = 'scheduled play';
  694. // fallback to iframe in 5s
  695. setTimeout(function() {
  696. if (div.FYTE.state) {
  697. div.FYTE.state = '';
  698. switchToIFrame.call(div);
  699. }
  700. }, 5000);
  701. }
  702. }
  703. else
  704. switchToIFrame.call(div);
  705. }
  706.  
  707. function startPlayingDirectly(div) {
  708. var video = document.createElement('video');
  709. video.controls = true;
  710. video.autoplay = true;
  711. video.style.cssText = important(
  712. 'position:absolute; left:0; top:0; right:0; padding:0; margin:auto; opacity:0; transition:opacity 2s;' +
  713. 'width:100%; height:100%;');
  714. video.className = 'instant-youtube-embed';
  715. video.volume = GM_getValue('volume', 0.5);
  716.  
  717. div.FYTE.videoSources.forEach(function(src) {
  718. var srcdom = video.appendChild(document.createElement('source'));
  719. Object.keys(src).forEach(function(k) { srcdom[k] = src[k] });
  720. srcdom.onerror = switchToIFrame.bind(div);
  721. });
  722.  
  723.  
  724. if (window.chrome) {
  725. video.addEventListener('click', function(e) {
  726. this.paused ? this.play() : this.pause();
  727. });
  728. }
  729. video.interval = (function() {
  730. return setInterval(function() {
  731. if (video.volume != GM_getValue('volume', 0.5))
  732. GM_setValue('volume', video.volume);
  733. }, 1000);
  734. })();
  735. var title = $(div, '.instant-youtube-title');
  736. if (title) {
  737. video.onpause = function() { title.removeAttribute('hidden') };
  738. video.onplay = function() { title.setAttribute('hidden', true) };
  739. }
  740. video.onloadedmetadata = div.FYTE.startAt && function(e) {
  741. video.currentTime = div.FYTE.startAt;
  742. };
  743. video.onloadeddata = function(e) {
  744. pauseOtherVideos(this);
  745. div.style.cssText += 'contain:inherit!important'; // allow fullscreen
  746. div.firstElementChild.appendChild(this);
  747. div.setAttribute('playing', '');
  748. this.style.opacity = 1;
  749. var img = $(div, 'img');
  750. img.style.transition = 'opacity 1s';
  751. img.style.opacity = 0;
  752. };
  753. }
  754.  
  755. function switchToIFrame(e) {
  756. var div = this;
  757. var wrapper = div.firstElementChild;
  758. if (e) {
  759. console.log('[FYTE] Direct linking canceled on %s, switching to IFRAME player', div.FYTE.srcEmbed);
  760. var video = e.target ? e.target.closest('video') : e.path && e.path[e.path.length-1];
  761. while (video.lastElementChild)
  762. video.lastElementChild.remove();
  763. }
  764.  
  765. ($(div, '[pin]') || wrapper).insertAdjacentHTML(pinnable != 'off' ? 'beforebegin' : 'beforeend',
  766. '<iframe class="instant-youtube-embed" allowtransparency="true" src="' +
  767. setUrlParams(div.FYTE.srcEmbedFixed, {
  768. html5: 1,
  769. autoplay: 1,
  770. autohide: 2,
  771. border: 0,
  772. controls: 1,
  773. fs: 1,
  774. showinfo: 1,
  775. ssl: 1,
  776. theme: 'dark',
  777. enablejsapi: 1,
  778. }) + '" frameborder="0" allowfullscreen width="100%" height="100%"></iframe>');
  779.  
  780. $(div, 'iframe').onload = function() {
  781. pauseOtherVideos(this);
  782. this.style.cssText = important(
  783. 'position:absolute; left:0; top:0; right:0; padding:0; margin:auto; opacity:1; transition:opacity 2s;');
  784.  
  785. div.setAttribute('iframe', '');
  786. div.setAttribute('playing', '');
  787. div.style.cssText += 'contain:none!important'; // allow fullscreen
  788. setTimeout(function() {
  789. $(div, 'img').style.display = 'none';
  790. var title = $(div, '.instant-youtube-title');
  791. if (title)
  792. title.remove();
  793. }, 2000);
  794. };
  795. }
  796.  
  797. function setUrlParams(url, params) {
  798. var names = Object.keys(params);
  799. url = url.replace(new RegExp('[?&](' + names.join('|') + ')(=[^?&]*)?', 'gi'), '');
  800. return url +
  801. (url.indexOf('?') > 0 ? '&' : '?') +
  802. names.map(function(n) { return n + '=' + params[n] }).join('&');
  803. }
  804.  
  805. function pauseOtherVideos(activePlayer) {
  806. $$(activePlayer.ownerDocument, '.instant-youtube-embed').forEach(function(v) {
  807. if (v == activePlayer)
  808. return;
  809. switch (v.localName) {
  810. case 'video':
  811. if (!v.paused)
  812. v.pause();
  813. break;
  814. case 'iframe':
  815. try { v.contentWindow.postMessage('{"event":"command", "func":"pauseVideo", "args":""}', '*') } catch(e) {}
  816. break;
  817. }
  818. });
  819. }
  820.  
  821. function makeDraggable(div) {
  822. div.draggable = true;
  823. div.addEventListener('dragstart', function(e) {
  824. e.dataTransfer.setData('text/plain', '');
  825.  
  826. var dropZone = document.createElement('div');
  827. var dropZoneHeight = 400 / div.FYTE.videoWidth * div.FYTE.videoHeight;
  828. dropZone.className = 'instant-youtube-dragndrop-placeholder';
  829.  
  830. document.body.addEventListener('dragenter', dragHandler);
  831. document.body.addEventListener('dragover', dragHandler);
  832. document.body.addEventListener('dragend', dragHandler);
  833. document.body.addEventListener('drop', dragHandler);
  834. function dragHandler(e) {
  835. e.stopImmediatePropagation();
  836. e.stopPropagation();
  837. e.preventDefault();
  838. switch (e.type) {
  839. case 'dragover':
  840. var playing = div.hasAttribute('playing');
  841. var stub = e.target.closest('.instant-youtube-container[stub]') == div.FYTE.stub && div.FYTE.stub;
  842. var gizmo = playing && !stub
  843. ? {left:0, top:0, right:innerWidth, bottom:innerHeight}
  844. : (stub || div).getBoundingClientRect();
  845. var x = e.clientX, y = e.clientY;
  846. var cx = (gizmo.left + gizmo.right) / 2;
  847. var cy = (gizmo.top + gizmo.bottom) / 2;
  848. var stay = !!stub || y >= cy-200 && y <= cy+200 && x >= cx-200 && x <= cx+200;
  849. overrideCSS(dropZone, {
  850. top: y < cy || stay ? '0' : 'auto',
  851. bottom: y > cy || stay ? '0' : 'auto',
  852. left: x < cx || stay ? '0' : 'auto',
  853. right: x > cx || stay ? '0' : 'auto',
  854. width: playing && stay && stub ? stub.clientWidth+'px' : '400px',
  855. height: playing && stay && stub ? stub.clientHeight+'px' : dropZoneHeight + 'px',
  856. margin: playing && stay ? 'auto' : '0',
  857. position: !playing && stay || stub ? 'absolute' : 'fixed',
  858. 'background-color': stub ? 'rgba(0,0,255,0.5)' : stay ? 'rgba(255,255,0,0.4)' : 'rgba(0,255,0,0.2)',
  859. });
  860. adjustPinnedOffset(dropZone, div);
  861. (stay && !playing || stub ? (stub || div) : document.body).appendChild(dropZone);
  862. break;
  863. case 'dragend':
  864. case 'drop':
  865. var corner = calcPinnedCorner(dropZone);
  866. dropZone.remove();
  867. dropZone = null;
  868. document.body.removeEventListener('dragenter', dragHandler);
  869. document.body.removeEventListener('dragover', dragHandler);
  870. document.body.removeEventListener('dragend', dragHandler);
  871. document.body.removeEventListener('drop', dragHandler);
  872. if (e.type == 'dragend')
  873. break;
  874. if (div.hasAttribute('playing'))
  875. (corner ? $(div, '[pin="' + corner + '"]') : div.FYTE.stub).click();
  876. else
  877. startPlaying(div, {pin: corner});
  878. }
  879. }
  880. });
  881. }
  882.  
  883. function adjustPinnedOffset(el, self, corner) {
  884. var offset = 0;
  885. $$('.instant-youtube-container[pinned] [pin="' + (corner || calcPinnedCorner(el)) + '"][active]').forEach(function(pin) {
  886. var container = pin.closest('[pinned]');
  887. if (container != el && container != self) {
  888. var bounds = container.getBoundingClientRect();
  889. offset = Math.max(offset, el.style.top == '0px' ? bounds.bottom : innerHeight - bounds.top);
  890. }
  891. });
  892. if (offset)
  893. el.style[el.style.top == '0px' ? 'top' : 'bottom'] = offset + 'px';
  894. }
  895.  
  896. function calcPinnedCorner(el) {
  897. var t = el.style.top != 'auto';
  898. var b = el.style.bottom != 'auto';
  899. var l = el.style.left != 'auto';
  900. var r = el.style.right != 'auto';
  901. return t && b && l && r ? '' : (t ? 'top' : 'bottom') + '-' + (l ? 'left' : 'right');
  902. }
  903.  
  904. function showOptions(e) {
  905. var optionsButton = e.target;
  906. translateHTML(optionsButton, 'afterend', '\
  907. <div class="instant-youtube-options">\
  908. <label tl style="width: 100% !important;">Size:<br>\
  909. <select data-action="size-mode" style="width:20ex!important">\
  910. <option tl value="Original">Original\
  911. <option tl value="Fit to width">Fit to width\
  912. <option>360p\
  913. <option>480p\
  914. <option>720p\
  915. <option>1080p\
  916. <option tl value="Custom">Custom...\
  917. </select>\
  918. </label>\
  919. <label data-action="size-custom" ' + (resizeMode != 'Custom' ? 'disabled' : '') + '>\
  920. <input type="number" min="320" max="9999" tl-placeholder="width" data-action="width" step="1" value="' + (resizeWidth||'') + '">\
  921. x\
  922. <input type="number" min="240" max="9999" tl-placeholder="height" data-action="height" step="1" value="' + (resizeHeight||'') + '">\
  923. </label>\
  924. <label tl="content,title" title="msgStoryboardTooltip">\
  925. <input data-action="storyboard" type="checkbox" ' + (showStoryboard ? 'checked' : '') + '>\
  926. Storyboard thumbs\
  927. </label>\
  928. <label tl="content,title" title="msgDirectTooltip">\
  929. <input data-action="direct" type="checkbox" ' + (playDirectly ? 'checked' : '') + '>\
  930. Play directly\
  931. </label>\
  932. <label tl="content,title" title="Do not process customized videos with enablejsapi=1 parameter (requires page reload)">\
  933. <input data-action="safe" type="checkbox" ' + (skipCustom ? 'checked' : '') + '>\
  934. Safe\
  935. </label>\
  936. <label tl="content,title" title="msgPinningTooltip">Pinning:\
  937. <select data-action="pinnable">\
  938. <option tl value="on">On\
  939. <option tl value="hide">On, hidden\
  940. <option tl value="off">Off\
  941. </select>\
  942. </label>\
  943. <span data-action="buttons">\
  944. <button tl data-action="ok">OK</button>\
  945. <button tl data-action="cancel">Cancel</button>\
  946. </span>\
  947. </div>\
  948. ');
  949. var options = optionsButton.nextElementSibling;
  950.  
  951. options.addEventListener('keydown', function(e) {
  952. if (e.target.localName == 'input' &&
  953. !e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey && e.key.match(/[.,]/))
  954. return false;
  955. });
  956.  
  957. $(options, '[data-action="size-mode"]').value = resizeMode;
  958. $(options, '[data-action="size-mode"]').addEventListener('change', function() {
  959. var v = this.value != 'Custom';
  960. var e = $(options, '[data-action="size-custom"]');
  961. e.children[0].disabled = e.children[1].disabled = v;
  962. v ? e.setAttribute('disabled', '') : e.removeAttribute('disabled');
  963. });
  964.  
  965. $(options, '[data-action="pinnable"]').value = pinnable;
  966.  
  967. $(options, '[data-action="buttons"]').addEventListener('click', function(e) {
  968. if (e.target.dataset.action != 'ok') {
  969. options.remove();
  970. return;
  971. }
  972. var v, shouldAdjust;
  973. if (resizeMode != (v = $(options, '[data-action="size-mode"]').value)) {
  974. GM_setValue('resize', resizeMode = v);
  975. shouldAdjust = true;
  976. }
  977. if (resizeMode == 'Custom') {
  978. var w = $(options, '[data-action="width"]').value |0;
  979. var h = $(options, '[data-action="height"]').value |0;
  980. if (resizeWidth != w || resizeHeight != h) {
  981. updateCustomSize(w, h);
  982. GM_setValue('width', resizeWidth);
  983. GM_setValue('height', resizeHeight);
  984. shouldAdjust = true;
  985. }
  986. }
  987. if (showStoryboard != (v = $(options, '[data-action="storyboard"]').checked)) {
  988. GM_setValue('showStoryboard', showStoryboard = v);
  989. $$('.instant-youtube-container').forEach(updateHoverHandler);
  990. }
  991. if (playDirectly != (v = $(options, '[data-action="direct"]').checked)) {
  992. GM_setValue('playHTML5', playDirectly = v);
  993. $$('.instant-youtube-container .instant-youtube-link').forEach(function(e) {
  994. e.textContent = playDirectly ? 'Play with Youtube player' : 'Play directly (up to 720p)';
  995. });
  996. }
  997. if (skipCustom != (v = $(options, '[data-action="safe"]').checked)) {
  998. GM_setValue('skipCustom', skipCustom = v);
  999. }
  1000. if (pinnable != (v = $(options, '[data-action="pinnable"]').value)) {
  1001. GM_setValue('pinnable', pinnable = v);
  1002. }
  1003.  
  1004. options.remove();
  1005.  
  1006. if (shouldAdjust)
  1007. adjustNodes(e, e.target.closest('.instant-youtube-container'));
  1008. });
  1009. }
  1010.  
  1011. function updateCustomSize(w, h) {
  1012. resizeWidth = Math.min(9999, Math.max(320, w|0 || resizeWidth|0));
  1013. resizeHeight = Math.min(9999, Math.max(240, h|0 || resizeHeight|0));
  1014. }
  1015.  
  1016. function important(cssText) {
  1017. return cssText.replace(/;/g, '!important;');
  1018. }
  1019.  
  1020. function tryCatch(func) {
  1021. try {
  1022. return func();
  1023. } catch(e) {
  1024. console.log(e);
  1025. }
  1026. }
  1027.  
  1028. function getFunctionComment(fn) {
  1029. return fn.toString().match(/\/\*([\s\S]*?)\*\/\s*\}$/)[1];
  1030. }
  1031.  
  1032. function $(selORnode, sel) {
  1033. return sel ? selORnode.querySelector(sel)
  1034. : document.querySelector(selORnode);
  1035. }
  1036.  
  1037. function $$(selORnode, sel) {
  1038. return Array.prototype.slice.call(
  1039. sel ? selORnode.querySelectorAll(sel)
  1040. : document.querySelectorAll(selORnode));
  1041. }
  1042.  
  1043. function overrideCSS(e, params) {
  1044. var names = Object.keys(params);
  1045. var style = e.style.cssText.replace(new RegExp('(^|\s|;)(' + names.join('|') + ')(:[^;]+)', 'gi'), '$1');
  1046. e.style.cssText = style.replace(/[^;]\s*$/, '$&;').replace(/^\s*;\s*/, '') +
  1047. names.map(function(n) { return n + ':' + params[n] + '!important' }).join(';') + ';';
  1048. }
  1049.  
  1050. // fix dumb Firefox bug
  1051. function floatPadding(node, style, dir) {
  1052. var padding = style['padding' + dir];
  1053. if (padding.indexOf('%') < 0)
  1054. return parseFloat(padding);
  1055. return parseFloat(padding) * (parseFloat(style.width) || node.clientWidth) / 100;
  1056. }
  1057.  
  1058. function translateHTML(baseElement, place, html) {
  1059. var tmp = document.createElement('div');
  1060. tmp.innerHTML = html;
  1061. $$(tmp, '[tl]').forEach(function(node) {
  1062. (node.getAttribute('tl') || 'content').split(',').forEach(function(what) {
  1063. var child, src, tl;
  1064. if (what == 'content') {
  1065. for (var i = node.childNodes.length-1, n; (i>=0) && (n=node.childNodes[i]); i--) {
  1066. if (n.nodeType == Node.TEXT_NODE && n.textContent.trim()) {
  1067. child = n;
  1068. break;
  1069. }
  1070. }
  1071. } else
  1072. child = node.getAttributeNode(what);
  1073. if (!child)
  1074. return;
  1075. src = child.textContent;
  1076. srcTrimmed = src.trim();
  1077. tl = src.replace(srcTrimmed, _(srcTrimmed));
  1078. if (src != tl)
  1079. child.textContent = tl;
  1080. });
  1081. });
  1082. baseElement.insertAdjacentHTML(place, tmp.innerHTML);
  1083. }
  1084.  
  1085. function initTL(src) {
  1086. var tlSource = {
  1087. 'watch on Youtube': {
  1088. 'ru': 'открыть на Youtube',
  1089. },
  1090. 'Play with Youtube player': {
  1091. 'ru': 'Включить плеер Youtube',
  1092. },
  1093. 'Play directly (up to 720p)': {
  1094. 'ru': 'Включить напрямую (макс. 720p)',
  1095. },
  1096. 'Shift-click to use alternative player': {
  1097. 'ru': 'Shift-клик для смены типа плеера',
  1098. },
  1099. 'Options': {
  1100. 'ru': 'Опции',
  1101. },
  1102. 'Size:': {
  1103. 'ru': 'Размер:',
  1104. },
  1105. 'Original': {
  1106. 'ru': 'Исходный',
  1107. },
  1108. 'Fit to width': {
  1109. 'ru': 'На всю ширину',
  1110. },
  1111. 'Custom...': {
  1112. 'ru': 'Настроить...',
  1113. },
  1114. 'width': {
  1115. 'ru': 'ширина',
  1116. },
  1117. 'height': {
  1118. 'ru': 'высота',
  1119. },
  1120. 'Storyboard thumbs': {
  1121. 'ru': 'Раскадровка',
  1122. },
  1123. 'msgStoryboardTooltip': {
  1124. 'en': 'Show storyboard preview on mouse hover at the bottom',
  1125. 'ru': 'Показывать миникадры при наведении мыши на низ кавер-картинки',
  1126. },
  1127. 'Play directly': {
  1128. 'ru': 'Плеер браузера',
  1129. },
  1130. 'msgDirectTooltip': {
  1131. 'en': 'Shift-clicking thumbnails will use alternative player',
  1132. 'ru': 'Удерживайте клавишу Shift при щелчке на картинке для альтернативного плеера',
  1133. },
  1134. 'Safe': {
  1135. 'ru': 'Консервативный режим',
  1136. },
  1137. 'Pinning': {
  1138. 'ru': 'Закрепление',
  1139. },
  1140. 'msgPinningTooltip': {
  1141. 'en': 'Enable corner pinning controls when a video is playing. \nTo restore the video click the active corner pin or the original video placeholder.',
  1142. 'ru': 'Включить шпильки по углам для закрепления видео во время просмотра. \nДля отмены можно нажать еще раз на активированный уголЪ или на заглушку, где исходно было видео',
  1143. },
  1144. 'On': {
  1145. 'ru': 'Да',
  1146. },
  1147. 'On, hide': {
  1148. 'ru': 'Да, невидимые',
  1149. },
  1150. 'Off': {
  1151. 'ru': 'Нет',
  1152. },
  1153. 'msgSafe': {
  1154. 'en': 'Do not process customized videos with enablejsapi=1 parameter (requires page reload)',
  1155. 'ru': 'Не обрабатывать нестандартные видео с параметром enablejsapi=1 (подействует после обновления страницы)',
  1156. },
  1157. 'OK': {
  1158. 'ru': 'ОК',
  1159. },
  1160. 'Cancel': {
  1161. 'ru': 'Оменить',
  1162. },
  1163. };
  1164. var browserLang = navigator.language || navigator.languages && navigator.languages[0] || '';
  1165. var browserLangMajor = browserLang.replace(/-.+/, '');
  1166. var tl = {};
  1167. Object.keys(tlSource).forEach(function(k) {
  1168. var langs = tlSource[k];
  1169. var text = langs[browserLang] || langs[browserLangMajor];
  1170. if (text)
  1171. tl[k] = text;
  1172. });
  1173. return function(src) { return tl[src] || src };
  1174. }
  1175.  
  1176. function injectStylesIfNeeded(force) {
  1177. if (!fytedom[0] && !force)
  1178. return;
  1179. var styledom = $('style#instant-youtube-styles');
  1180. if (styledom) {
  1181. // move our rules to the end of HEAD to increase CSS specificity
  1182. if (styledom.nextElementSibling && document.head)
  1183. document.head.insertBefore(styledom, null);
  1184. return;
  1185. }
  1186. styledom = (document.head || document.documentElement).appendChild(document.createElement('style'));
  1187. styledom.id = 'instant-youtube-styles';
  1188. styledom.textContent = important(getFunctionComment(function() { /*
  1189. .instant-youtube-container {
  1190. contain: strict;
  1191. position: relative;
  1192. overflow: hidden;
  1193. cursor: pointer;
  1194. padding: 0;
  1195. margin: 0;
  1196. font: normal 14px/1.0 sans-serif, Arial, Helvetica, Verdana;
  1197. text-align: center;
  1198. background: black;
  1199. }
  1200. .instant-youtube-container[disabled] {
  1201. background: #888;
  1202. }
  1203. .instant-youtube-container[disabled] .instant-youtube-storyboard {
  1204. display: none;
  1205. }
  1206. .instant-youtube-container[pinned] {
  1207. box-shadow: 0 0 30px black;
  1208. }
  1209. .instant-youtube-container .instant-youtube-wrapper {
  1210. width: 100%;
  1211. height: 100%;
  1212. }
  1213. .instant-youtube-container .instant-youtube-play-button {
  1214. display: block;
  1215. position: absolute;
  1216. width: 85px;
  1217. height: 60px;
  1218. left: 0;
  1219. right: 0;
  1220. top: 0;
  1221. bottom: 0;
  1222. margin: auto;
  1223. }
  1224. .instant-youtube-container .instant-youtube-loading-button {
  1225. display: block;
  1226. position: absolute;
  1227. width: 20px;
  1228. height: 20px;
  1229. left: 0;
  1230. right: 0;
  1231. top: 0;
  1232. bottom: 0;
  1233. padding: 0;
  1234. margin: auto;
  1235. pointer-events: none;
  1236. background: url("data:image/gif;base64,R0lGODlhFAAUAJEDAMzMzLOzs39/f////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgADACwAAAAAFAAUAAACPJyPqcuNItyCUJoQBo0ANIxpXOctYHaQpYkiHfM2cUrCNT0nqr4uudsz/IC5na/2Mh4Hu+HR6YBaplRDAQAh+QQFCgADACwEAAIADAAGAAACFpwdcYupC8BwSogR46xWZHl0l8ZYQwEAIfkEBQoAAwAsCAACAAoACgAAAhccMKl2uHxGCCvO+eTNmishcCCYjWEZFgAh+QQFCgADACwMAAQABgAMAAACFxwweaebhl4K4VE6r61DiOd5SfiN5VAAACH5BAUKAAMALAgACAAKAAoAAAIYnD8AeKqcHIwwhGntEWLkO3CcB4biNEIFACH5BAUKAAMALAQADAAMAAYAAAIWnDSpAHa4GHgohCHbGdbipnBdSHphAQAh+QQFCgADACwCAAgACgAKAAACF5w0qXa4fF6KUoVQ75UaA7Bs3yeNYAkWACH5BAUKAAMALAIABAAGAAwAAAIXnCU2iMfaRghqTmMp1moAoHyfIYIkWAAAOw==");
  1237. }
  1238. .instant-youtube-container:hover .ytp-large-play-button-svg {
  1239. fill: #CC181E;
  1240. }
  1241. .instant-youtube-container .instant-youtube-link {
  1242. display: block;
  1243. position: absolute;
  1244. width: 20em;
  1245. height: 20px;
  1246. top: 50%;
  1247. left: 0;
  1248. right: 0;
  1249. margin: 60px auto;
  1250. padding: 0;
  1251. border: none;
  1252. text-align: center;
  1253. text-decoration: none;
  1254. text-shadow: 1px 1px 3px black;
  1255. font-weight: bold;
  1256. color: white;
  1257. }
  1258. .instant-youtube-container span.instant-youtube-link {
  1259. z-index: 8;
  1260. font-weight: normal;
  1261. font-size: 12px;
  1262. }
  1263. .instant-youtube-container .instant-youtube-link:hover {
  1264. text-decoration: underline;
  1265. color: white;
  1266. background: transparent;
  1267. }
  1268. .instant-youtube-container iframe {
  1269. z-index: 10;
  1270. }
  1271. .instant-youtube-container .instant-youtube-title {
  1272. z-index: 9;
  1273. display: block;
  1274. position: absolute;
  1275. width: auto;
  1276. top: 0;
  1277. left: 0;
  1278. right: 0;
  1279. margin: 0;
  1280. padding: 7px;
  1281. border: none;
  1282. text-shadow: 1px 1px 2px black;
  1283. text-align: center;
  1284. text-decoration: none;
  1285. color: white;
  1286. background-color: rgba(0, 0, 0, 0.5);
  1287. }
  1288. .instant-youtube-container .instant-youtube-title strong {
  1289. font: bold 14px/1.0 sans-serif, Arial, Helvetica, Verdana;
  1290. }
  1291. .instant-youtube-container .instant-youtube-title strong:after {
  1292. content: " - $tl:'watch on Youtube'";
  1293. font-weight: normal;
  1294. margin-right: 1ex;
  1295. }
  1296. .instant-youtube-container .instant-youtube-title span {
  1297. color: inherit;
  1298. }
  1299. .instant-youtube-container .instant-youtube-title span:before {
  1300. content: "(";
  1301. }
  1302. .instant-youtube-container .instant-youtube-title span:after {
  1303. content: ")";
  1304. }
  1305. @-webkit-keyframes instant-youtube-fadein {
  1306. from { opacity: 0 }
  1307. to { opacity: 1 }
  1308. }
  1309. @-moz-keyframes instant-youtube-fadein {
  1310. from { opacity: 0 }
  1311. to { opacity: 1 }
  1312. }
  1313. @keyframes instant-youtube-fadein {
  1314. from { opacity: 0 }
  1315. to { opacity: 1 }
  1316. }
  1317. .instant-youtube-container:not(:hover) .instant-youtube-title[hidden] {
  1318. display: none;
  1319. margin: 0;
  1320. }
  1321. .instant-youtube-container .instant-youtube-title:hover {
  1322. text-decoration: underline;
  1323. }
  1324. .instant-youtube-container .instant-youtube-title strong {
  1325. color: white;
  1326. }
  1327. .instant-youtube-container .instant-youtube-options-button {
  1328. opacity: 0.6;
  1329. position: absolute;
  1330. right: 0;
  1331. bottom: 0;
  1332. margin: 0;
  1333. padding: 1.5ex 2ex;
  1334. font-size: 11px;
  1335. text-shadow: 1px 1px 2px black;
  1336. color: white;
  1337. }
  1338. .instant-youtube-container .instant-youtube-options-button:hover {
  1339. opacity: 1;
  1340. background: rgba(0, 0, 0, 0.5);
  1341. }
  1342. .instant-youtube-container .instant-youtube-options {
  1343. display: flex;
  1344. position: absolute;
  1345. right: 0;
  1346. bottom: 0;
  1347. margin: 0;
  1348. padding: 2ex 1ex 2ex 2ex;
  1349. flex-direction: column;
  1350. align-items: flex-start;
  1351. line-height: 1.5;
  1352. text-align: left;
  1353. opacity: 1;
  1354. color: white;
  1355. background: black;
  1356. }
  1357. .instant-youtube-container .instant-youtube-options * {
  1358. width: auto;
  1359. height: auto;
  1360. margin: 0;
  1361. padding: 0;
  1362. font: inherit;
  1363. font-size: 13px;
  1364. vertical-align: middle;
  1365. text-transform: none;
  1366. text-align: left;
  1367. border-radius: 0;
  1368. text-decoration: none;
  1369. color: white;
  1370. background: black;
  1371. }
  1372. .instant-youtube-container .instant-youtube-options > label {
  1373. margin-top: 1ex;
  1374. }
  1375. .instant-youtube-container .instant-youtube-options > label > * {
  1376. display: inline;
  1377. }
  1378. .instant-youtube-container .instant-youtube-options select {
  1379. padding: .5ex .25ex;
  1380. border: 1px solid #444;
  1381. -webkit-appearance: menulist;
  1382. }
  1383. .instant-youtube-container .instant-youtube-options [data-action="size-custom"] input {
  1384. width: 9ex;
  1385. padding: .5ex .5ex .4ex;
  1386. border: 1px solid #666;
  1387. }
  1388. .instant-youtube-container .instant-youtube-options [data-action="buttons"] {
  1389. margin-top: 1em;
  1390. }
  1391. .instant-youtube-container .instant-youtube-options button {
  1392. margin: 0 1ex 0 0;
  1393. padding: .5ex 2ex;
  1394. border: 2px solid gray;
  1395. font-weight: bold;
  1396. }
  1397. .instant-youtube-container .instant-youtube-options button:hover {
  1398. border-color: white;
  1399. }
  1400. .instant-youtube-container .instant-youtube-options > [disabled] {
  1401. opacity: 0.25;
  1402. }
  1403. .instant-youtube-container .instant-youtube-storyboard {
  1404. height: 33%;
  1405. max-height: 90px;
  1406. display: block;
  1407. position: absolute;
  1408. left: 0;
  1409. right: 0;
  1410. bottom: 0;
  1411. overflow: visible;
  1412. overflow-x: visible;
  1413. overflow-y: visible;
  1414. }
  1415. .instant-youtube-container .instant-youtube-storyboard:hover {
  1416. background-color: rgba(0,0,0,0.3);
  1417. }
  1418. .instant-youtube-container .instant-youtube-storyboard[disabled] {
  1419. display:none;
  1420. }
  1421. .instant-youtube-container .instant-youtube-storyboard div {
  1422. display: block;
  1423. position: absolute;
  1424. bottom: 0px;
  1425. pointer-events: none;
  1426. border: 3px solid #888;
  1427. box-shadow: 2px 2px 10px black;
  1428. transition: opacity .25s ease;
  1429. background-color: transparent;
  1430. background-origin: content-box;
  1431. opacity: 0;
  1432. }
  1433. .instant-youtube-container .instant-youtube-storyboard:hover div {
  1434. opacity: 1;
  1435. }
  1436. .instant-youtube-container [pin] {
  1437. position: absolute;
  1438. width: 0;
  1439. height: 0;
  1440. margin: 0;
  1441. padding: 0;
  1442. top: auto; bottom: auto; left: auto; right: auto;
  1443. border-style: solid;
  1444. transition: opacity 2.5s ease-in, opacity 0.4s ease-out;
  1445. opacity: 0;
  1446. z-index: 100;
  1447. }
  1448. .instant-youtube-container[playing]:hover [pin]:not([transparent]) {
  1449. opacity: 1;
  1450. }
  1451. .instant-youtube-container[playing] [pin]:hover {
  1452. cursor: alias;
  1453. opacity: 1;
  1454. transition: opacity 0s;
  1455. }
  1456. .instant-youtube-container [pin=top-left][active] { border-top-color: green; }
  1457. .instant-youtube-container [pin=top-left]:hover { border-top-color: #fc0; }
  1458. .instant-youtube-container [pin=top-left] {
  1459. top: 0; left: 0;
  1460. border-width: 10px 10px 0 0;
  1461. border-color: red transparent transparent transparent;
  1462. }
  1463. .instant-youtube-container [pin=top-right][active] { border-right-color: green; }
  1464. .instant-youtube-container [pin=top-right]:hover { border-right-color: #fc0; }
  1465. .instant-youtube-container [pin=top-right] {
  1466. top: 0; right: 0;
  1467. border-width: 0 10px 10px 0;
  1468. border-color: transparent red transparent transparent;
  1469. }
  1470. .instant-youtube-container [pin=bottom-right][active] { border-bottom-color: green; }
  1471. .instant-youtube-container [pin=bottom-right]:hover { border-bottom-color: #fc0; }
  1472. .instant-youtube-container [pin=bottom-right] {
  1473. bottom: 0; right: 0;
  1474. border-width: 0 0 10px 10px;
  1475. border-color: transparent transparent red transparent;
  1476. }
  1477. .instant-youtube-container [pin=bottom-left][active] { border-left-color: green; }
  1478. .instant-youtube-container [pin=bottom-left]:hover { border-left-color: #fc0; }
  1479. .instant-youtube-container [pin=bottom-left] {
  1480. bottom: 0; left: 0;
  1481. border-width: 10px 0 0 10px;
  1482. border-color: transparent transparent transparent red;
  1483. }
  1484. .instant-youtube-dragndrop-placeholder {
  1485. z-index: 999999999;
  1486. margin: 0;
  1487. padding: 0;
  1488. background: rgba(0, 255, 0, 0.1);
  1489. border: 2px dotted green;
  1490. box-sizing: border-box;
  1491. pointer-events: none;
  1492. }
  1493. */}).replace(/\$tl:'(.+?)'/g, function(m, m1) { return _(m1) })
  1494. );
  1495. }