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. Optionally a fast simple HTML5 direct playback (720p max) can be selected if available for the video.

当前为 2019-07-27 提交的版本,查看 最新版本

  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. Optionally a fast simple HTML5 direct playback (720p max) can be selected if available for the video.
  4. // @description:ru На порядок ускоряет время загрузки страниц с большим количеством вставленных Youtube-видео. С первого момента загрузки страницы появляются заглушки для видео, которые можно щелкнуть для загрузки плеера, и почти сразу же появляются кавер-картинки с названием видео. В опциях можно включить режим использования упрощенного браузерного плеера (макс. 720p).
  5. // @version 2.9.15
  6. // @include *
  7. // @exclude /^https:\/\/(www\.)?youtube\.com\/(?!embed)/
  8. // @exclude https://accounts.google.*/o/oauth2/postmessageRelay*
  9. // @exclude https://clients*.google.*/youtubei/*
  10. // @exclude https://clients*.google.*/static/proxy*
  11. // @author wOxxOm
  12. // @namespace wOxxOm.scripts
  13. // @license MIT License
  14. // @grant GM_getValue
  15. // @grant GM_listValues
  16. // @grant GM_deleteValue
  17. // @grant GM_setValue
  18. // @grant GM_addStyle
  19. // @grant GM_xmlhttpRequest
  20. // @connect www.youtube.com
  21. // @connect youtube.com
  22. // @run-at document-start
  23. // @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=
  24. // @compatible chrome
  25. // @compatible firefox
  26. // @compatible opera
  27. // ==/UserScript==
  28.  
  29. 'use strict';
  30.  
  31. // keep video info cache for a month since last time it's shown
  32. var CACHE_STALE_DURATION = 30 * 24 * 3600e3;
  33.  
  34. var playQuality = GM_getValue('quality', 'auto') || 'auto';
  35.  
  36. if (/^https:\/\/(www\.)?youtube\.com\//.test(location)) {
  37. if (playQuality !== 'auto') {
  38. window.addEventListener('load', function _() {
  39. window.removeEventListener('load', _);
  40. var player = $('.html5-video-player');
  41. if (player && typeof player.setPlaybackQuality === 'function')
  42. player.setPlaybackQuality(playQuality);
  43. });
  44. }
  45. if (!window.chrome || window === window.parent)
  46. return;
  47. parent.postMessage('FYTE-toggle-fullscreen-init', '*');
  48. addEventListener('message', function onMessage(e) {
  49. if (e.source !== parent || e.data !== 'FYTE-toggle-fullscreen-init-confirmed')
  50. return;
  51. removeEventListener('message', onMessage);
  52. var fsbtn = document.getElementsByClassName('ytp-fullscreen-button');
  53. new MutationObserver(function() {
  54. if (fsbtn[0]) {
  55. this.disconnect();
  56. // noinspection SillyAssignmentJS
  57. fsbtn[0].outerHTML = fsbtn[0].outerHTML.replace('aria-disabled="true"', '');
  58. fsbtn[0].addEventListener('click', function() {
  59. window.parent.postMessage('FYTE-toggle-fullscreen', '*');
  60. });
  61. }
  62. }).observe(document, {subtree:true, childList:true});
  63. });
  64. return;
  65. }
  66.  
  67. var resizeMode = GM_getValue('resize', 'Fit to width');
  68. if (typeof resizeMode !== 'string')
  69. resizeMode = resizeMode ? 'Fit to width' : 'Original';
  70.  
  71. var resizeWidth = GM_getValue('width', 1280) |0;
  72. var resizeHeight = GM_getValue('height', 720) |0;
  73. updateCustomSize();
  74.  
  75. var pinnedWidth = GM_getValue('pinnedWidth', 400) |0;
  76.  
  77. var playDirectly = !!GM_getValue('playHTML5', false);
  78. var playDirectlyShown = !!GM_getValue('playHTML5shown', false);
  79. var skipCustom = !!GM_getValue('skipCustom', true);
  80. var showStoryboard = !!GM_getValue('showStoryboard', true);
  81. var pinnable = GM_getValue('pinnable', 'on');
  82. if (!/^(on|hide|off)$/.test(pinnable))
  83. pinnable = !!pinnable ? 'on' : 'hide';
  84.  
  85. var _ = initTL();
  86.  
  87. var imageLoader = document.createElement('img');
  88. var imageLoader2 = document.createElement('img');
  89.  
  90. var fytedom = document.getElementsByClassName('instant-youtube-container');
  91. var iframes = document.getElementsByTagName('iframe');
  92. var objects = document.getElementsByTagName('object');
  93. var checked = [];
  94. var persite = (function() {
  95. var rules = [
  96. {host: 'developers.google.com',
  97. match: '[data-video-id]',
  98. src: function(e) { return '//youtube.com/embed/' + e.dataset.videoId }},
  99. {host: 'play.google.com', eatparent: 0},
  100. {host: /(^|\.)google\.\w{2,3}(\.\w{2,3})?$/, class:'g-blk', query: 'a[href*="youtube.com/watch"][data-ved]', eatparent: 1},
  101. {host: 'pikabu.ru', class:'b-video', match: '[data-url*="youtube.com/embed"]', attr: 'data-url'},
  102. {host: 'androidauthority.com', eatparent: '.video-container'},
  103. {host: 'reddit.com',
  104. match: '[data-url*="youtube.com/"] [src*="/mediaembed"], [data-url*="youtu.be/"] [src*="/mediaembed"]',
  105. src: function(e) { return e.closest('[data-url*="youtube.com/"], [data-url*="youtu.be/"]').dataset.url }},
  106. {host: /(www\.)?theverge\.com$/, eatparent: '.p-scalable-video'},
  107. {host: '9gag.com', eatparent: 0},
  108. {host: 'reddit.com', match: '[data-url*="youtube.com"] iframe[src*="redditmedia.com/mediaembed"]',
  109. src: function(e) { return e.closest('[data-url*="youtube.com"]').dataset.url }},
  110. {host: 'anilist.co', eatparent: '.youtube'},
  111. ];
  112. for (var i=0, rule; (i<rules.length) && (rule=rules[i]); i++) {
  113. var rx = rule.host instanceof RegExp ? rule.host : new RegExp('(^|\\.)' + rule.host.replace(/\./g, '\\.') + '$', 'i');
  114. if (rx.test(location.hostname)) {
  115. if (!rule.tag && !rule.class)
  116. rule.tag = 'iframe';
  117. if (!rule.match && !rule.query)
  118. rule.match = '[src*="youtube.com/embed"]';
  119. return {
  120. nodes: rule.class ? document.getElementsByClassName(rule.class) : document.getElementsByTagName(rule.tag),
  121. match: rule.match ?
  122. function(e) {
  123. // noinspection JSReferencingMutableVariableFromClosure
  124. return e.matches(rule.match) ? e : null;
  125. } :
  126. function(e) {
  127. // noinspection JSReferencingMutableVariableFromClosure
  128. return e.querySelector(rule.query);
  129. },
  130. attr: rule.attr,
  131. src: rule.src,
  132. eatparent: rule.eatparent,
  133. };
  134. }
  135. }
  136. })();
  137.  
  138. findEmbeds([]);
  139. injectStylesIfNeeded();
  140. new MutationObserver(findEmbeds).observe(document, {subtree:true, childList:true});
  141.  
  142. document.addEventListener('DOMContentLoaded', function(e) {
  143. injectStylesIfNeeded();
  144. adjustNodesIfNeeded(e);
  145. setTimeout(cleanupCache, 60e3);
  146. });
  147.  
  148. window.addEventListener('resize', adjustNodesIfNeeded, true);
  149.  
  150. window.addEventListener('message', function(e) {
  151. switch (e.data) {
  152. case 'FYTE-toggle-fullscreen-init':
  153. if (e.source)
  154. e.source.postMessage('FYTE-toggle-fullscreen-init-confirmed', '*');
  155. break;
  156. case 'FYTE-toggle-fullscreen':
  157. $$('iframe[allowfullscreen]').some(function (iframe) {
  158. if (iframe.contentWindow === e.source) {
  159. goFullscreen(iframe, !(document.webkitIsFullScreen || document.mozIsFullScreen || document.isFullScreen));
  160. return true;
  161. }
  162. });
  163. break;
  164. case 'iframe-allowfs':
  165. $$('iframe:not([allowfullscreen])').some(function (iframe) {
  166. if (iframe.contentWindow === e.source) {
  167. iframe.allowFullscreen = true;
  168. return true;
  169. }
  170. });
  171. if (window !== window.top)
  172. window.parent.postMessage('iframe-allowfs', '*');
  173. break;
  174. }
  175. });
  176.  
  177. function findEmbeds(mutations) {
  178. var i, len, e, m;
  179. if (mutations.length === 1) {
  180. var added = mutations[0].addedNodes;
  181. if (!added[0] || !added[1] && added[0].nodeType === 3)
  182. return;
  183. }
  184. if (persite)
  185. for (i=0, len=persite.nodes.length; (i<len) && (e=persite.nodes[i]); i++)
  186. if ((e = persite.match(e)))
  187. processEmbed(e, persite.src && persite.src(e) || e.getAttribute(persite.attr));
  188. for (i=0, len=iframes.length; (i<len) && (e=iframes[i]); i++) {
  189. if (checked.includes(e)) continue;
  190. checked.push(e);
  191. if (/youtube\.com|youtu\.be/i.test(e.src || e.dataset.src))
  192. processEmbed(e, e.src || e.dataset.src);
  193. }
  194. for (i=0, len=objects.length; (i<len) && (e=objects[i]); i++) {
  195. if (checked.includes(e)) continue;
  196. checked.push(e);
  197. if ((m = e.querySelector('embed, [value*="youtu.be"], [value*="youtube.com"]')))
  198. processEmbed(e, m.src || e.dataset.src || 'https://' + m.value.match(/youtu\.be.*|youtube\.com.*/)[0]);
  199. }
  200. }
  201.  
  202. function processEmbed(node, src) {
  203. function decodeEmbedUrl(url) {
  204. return url.indexOf('youtube.com%2Fembed') > 0
  205. ? decodeURIComponent(url.replace(/^.*?(http[^&?=]+?youtube.com%2Fembed[^&]+).*$/i, '$1'))
  206. : url;
  207. }
  208. src = src || node.src || node.href || '';
  209. var n = node;
  210. var np = n.parentNode;
  211. var srcFixed = decodeEmbedUrl(src).replace(/\/(watch\?v=|v\/)/, '/embed/').replace(/^([^?&]+)&/, '$1?');
  212. if (src.indexOf('cdn.embedly.com/') > 0 ||
  213. resizeMode !== 'Original' && np && np.children.length === 1 && !np.className && !np.id)
  214. {
  215. n = location.hostname === 'disqus.com' ? np.parentNode : np;
  216. np = n.parentElement;
  217. }
  218. if (!np ||
  219. !np.parentNode ||
  220. skipCustom && srcFixed.indexOf('enablejsapi=1') > 0 ||
  221. srcFixed.indexOf('/embed/videoseries') > 0 ||
  222. node.matches('.instant-youtube-embed, .YTLT-embed, .ihvyoutube') ||
  223. node.style.position === 'fixed' ||
  224. node.onload // skip some retarded loaders
  225. )
  226. return;
  227.  
  228. var id = srcFixed.match(/(?:^(?:https?:)?\/\/)(?:www\.)?(?:youtube\.com\/(?:embed\/(?:v=)?|\/.*?[&?\/]v[=\/])|youtu\.be\/)([^\s,.()\[\]?]+?)(?:[&?\/].*|$)/);
  229. if (!id)
  230. return;
  231. id = id[1];
  232.  
  233. var autoplay = srcFixed.indexOf('autoplay=1') > 0;
  234.  
  235. if (np.localName === 'object')
  236. n = np, np = n.parentElement;
  237.  
  238. var eatparent = persite && persite.eatparent || 0;
  239. if (typeof eatparent === 'string')
  240. n = np.closest(eatparent) || n, np = n.parentElement;
  241. else
  242. while (eatparent--)
  243. n = np, np = n.parentElement;
  244.  
  245. injectStylesIfNeeded('force');
  246.  
  247. var div = document.createElement('div');
  248. div.className = 'instant-youtube-container';
  249. div.FYTE = {
  250. state: 'querying',
  251. srcEmbed: srcFixed.replace(/&$/, ''),
  252. originalWidth: /%/.test(node.width) ? 320 : node.width|0 || n.clientWidth|0,
  253. originalHeight: /%/.test(node.height) ? 200 : node.height|0 || n.clientHeight|0,
  254. cache: tryJSONparse(localStorage['FYTE-cache-' + id]) || {
  255. id: id,
  256. }
  257. };
  258. div.FYTE.srcEmbedFixed = div.FYTE.srcEmbed.replace(/^http:/, 'https:').replace(/([&?])(wmode=\w+|feature=oembed)&?/, '$1').replace(/[&?]$/, '');
  259. div.FYTE.srcWatchFixed = div.FYTE.srcEmbedFixed.replace(/\/embed\//, '/watch?v=').replace(/(\?.*?)\?/, '$1&');
  260.  
  261. var cache = div.FYTE.cache;
  262. cache.lastUsed = Date.now();
  263. localStorage['FYTE-cache-' + id] = JSON.stringify(cache);
  264.  
  265. if (cache.reason)
  266. div.setAttribute('disabled', '');
  267.  
  268. var divSize = calcContainerSize(div, n);
  269. var origStyle = getComputedStyle(n);
  270. div.style.cssText = important(
  271. (autoplay ? '' : 'background-color:transparent; transition:background-color 2s;') +
  272. (origStyle.hasOwnProperty('position') ? Object.keys(origStyle) : Object.keys(origStyle.__proto__) /*FF*/)
  273. .filter(function(k) { return !!k.match(/^(position|left|right|top|bottom)$/) })
  274. .map(function(k) { return k + ':' + origStyle[k] })
  275. .join(';')
  276. .replace(/\b[^;:]+:\s*(auto|static|block)\s*(!\s*important)?;/g, '') +
  277. (origStyle.display === 'inline' ? ';display:inline-block;width:100%' : '') +
  278. ';min-width:' + Math.min(divSize.w, div.FYTE.originalWidth) + 'px' +
  279. ';min-height:' + Math.min(divSize.h, div.FYTE.originalHeight) + 'px' +
  280. (resizeMode === 'Fit to width' ? ';width:100%' : '') +
  281. ';max-width:' + divSize.w + 'px; height:' + (persite && persite.eatparent === 0 ? '100%;' : divSize.h + 'px;'));
  282. if (!autoplay) {
  283. setTimeout(function() { div.style.backgroundColor = '' }, 0);
  284. setTimeout(function() { div.style.transition = '' }, 2000);
  285. }
  286.  
  287. // consume parents of retardedly positioned videos
  288. var parentStyle = getComputedStyle(np);
  289. if (div.style.position.match('absolute|relative')
  290. && parseFloat(parentStyle.paddingTop) + parseFloat(parentStyle.paddingBottom) < parseFloat(parentStyle.height)) {
  291. if (np.children.length === 1 &&
  292. floatPadding(np, parentStyle, 'Top') >= div.FYTE.originalHeight-1 ||
  293. floatPadding(np, getComputedStyle(np, ':after'), 'Top') >= div.FYTE.originalHeight-1
  294. ) {
  295. n = np, np = n.parentElement;
  296. }
  297. div.style.cssText = div.style.cssText.replace(/\b(position|left|top|right|bottom):[^;]+/g, '');
  298. }
  299.  
  300. var wrapper = div.appendChild(document.createElement('div'));
  301. wrapper.className = 'instant-youtube-wrapper';
  302.  
  303. var img = wrapper.appendChild(document.createElement('img'));
  304. if (!autoplay)
  305. img.src = 'https://i.ytimg.com/vi/' + id + '/' + (cache.cover || 'maxresdefault.jpg');
  306. img.className = 'instant-youtube-thumbnail';
  307. img.style.cssText = important((cache.cover ? '' : 'transition:opacity 0.1s ease-out; opacity:0; ') +
  308. 'padding:0; margin:auto; position:absolute; left:0; right:0; top:0; bottom:0; max-width:none; max-height:none;');
  309.  
  310. img.onload = function(e) {
  311. if (img.naturalWidth <= 120 && !cache.cover)
  312. return img.onerror(e);
  313. var fitToWidth = true;
  314. if (img.naturalHeight || cache.coverHeight) {
  315. if (!cache.coverHeight) {
  316. cache.coverWidth = img.naturalWidth;
  317. cache.coverHeight = img.naturalHeight;
  318. localStorage['FYTE-cache-' + id] = JSON.stringify(cache);
  319. }
  320. var ratio = cache.coverWidth / cache.coverHeight;
  321. if (ratio > 4.1/3 && ratio < divSize.w/divSize.h) {
  322. img.style.cssText += important('width:auto; height:100%;');
  323. fitToWidth = false;
  324. }
  325. }
  326. if (fitToWidth) {
  327. img.style.cssText += important('width:100%; height:auto;');
  328. }
  329. if (cache.videoWidth)
  330. fixThumbnailAR(div);
  331. if (!autoplay)
  332. img.style.opacity = 1;
  333. };
  334. img.onerror = function() {
  335. if (img.src.indexOf('maxresdefault') > 0)
  336. img.src = img.src.replace('maxresdefault','sddefault');
  337. else if (img.src.indexOf('sddefault') > 0)
  338. img.src = img.src.replace('sddefault','hqdefault');
  339. };
  340. if (cache.coverWidth)
  341. img.onload();
  342.  
  343. translateHTML(wrapper, 'beforeend', '\
  344. <a class="instant-youtube-title" target="_blank" href="' + div.FYTE.srcWatchFixed + '">' +
  345. (cache.title || cache.reason ? '<strong>' + (cache.title || cache.reason || '') + '</strong>' +
  346. (cache.duration ? '<span>' + cache.duration + '</span>' : '') +
  347. (cache.fps ? '<i>, ' + cache.fps + 'fps</i>' : '')
  348. : '&nbsp;') + '</a>\
  349. <svg class="instant-youtube-play-button">\
  350. <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">\
  351. <title tl>msgAltPlayerHint</title>\
  352. </path>\
  353. <polygon fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" points="33.3,41.4 33.3,17.75 56,29.6"></polygon>\
  354. </svg>\
  355. <span tl class="instant-youtube-alternative">' + (playDirectly ? 'Play with Youtube player' : 'Play directly (up to 720p)') + '</span>\
  356. <div tl class="instant-youtube-options-button">Options</div>\
  357. ');
  358.  
  359. np.insertBefore(div, n);
  360. n.remove();
  361.  
  362. if (!cache.title && !cache.reason || autoplay && playDirectly)
  363. fetchInfo();
  364.  
  365. if (autoplay)
  366. return startPlaying(div);
  367.  
  368. div.addEventListener('click', clickHandler);
  369. div.addEventListener('mousedown', clickHandler);
  370. div.addEventListener('mouseenter', fetchInfo);
  371.  
  372. function fetchInfo() {
  373. div.removeEventListener('mouseenter', fetchInfo);
  374. if (!div.FYTE.storyboard) {
  375. GM_xmlhttpRequest({
  376. method: 'GET',
  377. url: 'https://www.youtube.com/get_video_info?video_id=' + id +
  378. '&hl=en_US&html5=1&el=embedded&eurl=' + encodeURIComponent(location.href),
  379. context: div,
  380. onload: parseVideoInfo
  381. });
  382. }
  383. }
  384. }
  385.  
  386. function adjustNodesIfNeeded(e) {
  387. if (!fytedom[0])
  388. return;
  389. if (adjustNodesIfNeeded.scheduled)
  390. clearTimeout(adjustNodesIfNeeded.scheduled);
  391. adjustNodesIfNeeded.scheduled = setTimeout(function() {
  392. adjustNodes(e);
  393. adjustNodesIfNeeded.scheduled = 0;
  394. }, 16);
  395. }
  396.  
  397. function adjustNodes(e, clickedContainer) {
  398. var force = !!clickedContainer;
  399. var nearest = force ? clickedContainer : null;
  400.  
  401. var vids = $$('.instant-youtube-container:not([pinned]):not([stub])');
  402.  
  403. if (!nearest && e.type !== 'DOMContentLoaded') {
  404. var minDistance = window.innerHeight*3/4 |0;
  405. var nearTargetY = window.innerHeight/2;
  406. vids.forEach(function(n) {
  407. var bounds = n.getBoundingClientRect();
  408. var distance = Math.abs((bounds.bottom + bounds.top)/2 - nearTargetY);
  409. if (distance < minDistance) {
  410. minDistance = distance;
  411. nearest = n;
  412. }
  413. });
  414. }
  415.  
  416. if (nearest) {
  417. var bounds = nearest.getBoundingClientRect();
  418. var nearestCenterYpct = (bounds.top + bounds.bottom)/2 / window.innerHeight;
  419. }
  420.  
  421. var resized = false;
  422.  
  423. vids.forEach(function(n) {
  424. var size = calcContainerSize(n);
  425. var w = size.w, h = size.h;
  426.  
  427. // prevent parent clipping
  428. for (var e=n.parentElement, style; e; e=e.parentElement)
  429. if (e.style.overflow !== 'visible' && (style=getComputedStyle(e)))
  430. if ((style.overflow+style.overflowX+style.overflowY).match(/hidden|scroll/))
  431. if (n.offsetTop < e.clientHeight / 2 && n.offsetTop + n.clientHeight > e.clientHeight)
  432. e.style.cssText = e.style.cssText.replace(/\boverflow(-[xy])?:[^;]+/g, '') +
  433. important('overflow:visible;overflow-x:visible;overflow-y:visible;');
  434.  
  435. if (force && Math.abs(w - parseFloat(n.style.maxWidth)) <= 2)
  436. return;
  437.  
  438. if (n.style.maxWidth !== w + 'px') overrideCSS(n, {'max-width': w + 'px'});
  439. if (n.style.height !== h + 'px') overrideCSS(n, {'height': h + 'px'});
  440. if (parseFloat(n.style.minWidth) > w) overrideCSS(n, {'min-width': n.style.maxWidth});
  441. if (parseFloat(n.style.minHeight) > h) overrideCSS(n, {'min-height': n.style.height});
  442.  
  443. fixThumbnailAR(n);
  444. resized = true;
  445. });
  446.  
  447. if (resized && nearest)
  448. setTimeout(function() {
  449. var bounds = nearest.getBoundingClientRect();
  450. var h = bounds.bottom - bounds.top;
  451. var projectedCenterY = nearestCenterYpct * window.innerHeight;
  452. var projectedTop = projectedCenterY - h/2;
  453. var safeTop = Math.min(Math.max(0, projectedTop), window.innerHeight - h);
  454. window.scrollBy(0, bounds.top - safeTop);
  455. }, 16);
  456. }
  457.  
  458. function calcContainerSize(div, origNode) {
  459. origNode = origNode || div;
  460. var w, h;
  461. var np = origNode.parentElement;
  462. var style = getComputedStyle(np);
  463. var parentWidth = parseFloat(style.width) - floatPadding(np, style, 'Left') - floatPadding(np, style, 'Right');
  464. if (+style.columnCount > 1)
  465. parentWidth = (parentWidth + parseFloat(style.columnGap)) / style.columnCount - parseFloat(style.columnGap);
  466. switch (resizeMode) {
  467. case 'Original':
  468. if (div.FYTE.originalWidth === 320 && div.FYTE.originalHeight === 200) {
  469. w = parentWidth;
  470. h = parentWidth / 16 * 9;
  471. } else {
  472. w = div.FYTE.originalWidth;
  473. h = div.FYTE.originalHeight;
  474. }
  475. break;
  476. case 'Custom':
  477. w = resizeWidth;
  478. h = resizeHeight;
  479. break;
  480. case '1080p':
  481. case '720p':
  482. case '480p':
  483. case '360p':
  484. h = parseInt(resizeMode);
  485. w = h / 9 * 16;
  486. break;
  487. default: // fit-to-width mode
  488. var n = origNode;
  489. do {
  490. n = n.parentElement;
  491. // find parent node with nonzero width (i.e. independent of our video element)
  492. } while (n && !(w = n.clientWidth));
  493. if (w)
  494. h = w / 16 * 9;
  495. else {
  496. w = origNode.clientWidth;
  497. h = origNode.clientHeight;
  498. }
  499. }
  500. if (parentWidth > 0 && parentWidth < w) {
  501. h = parentWidth / w * h;
  502. w = parentWidth;
  503. }
  504. if (resizeMode === 'Fit to width' && h < div.FYTE.originalHeight*0.9)
  505. h = Math.min(div.FYTE.originalHeight, w / div.FYTE.originalWidth * div.FYTE.originalHeight);
  506.  
  507. return {w: window.chrome ? w : Math.round(w), h:h};
  508. }
  509.  
  510. function parseVideoInfo(response) {
  511. var div = response.context;
  512. var txt = response.responseText;
  513. var info = tryCatch(function() {
  514. var json = txt.replace(/(\w+)=?(.*?)(&|$)/g, '"$1":"$2",').replace(/^(.+?),?$/, '{$1}');
  515. return tryJSONparse(json);
  516. }) || {};
  517. var cache = div.FYTE.cache;
  518. var shouldUpdateCache = false;
  519. var videoSources = [];
  520.  
  521. // parse width & height to adjust the thumbnail
  522. var m = decodeURIComponent(info.adaptive_fmts || '').match(/size=(\d+)x(\d+)\b/) ||
  523. decodeURIComponent(txt).match(/\/(\d+)x(\d+)\//);
  524.  
  525. if (m && (cache.videoWidth !== (m[1]|0) || cache.videoHeight !== (m[2]|0))) {
  526. fixThumbnailAR(div, m[1]|0, m[2]|0);
  527. cache.videoWidth = m[1]|0;
  528. cache.videoHeight = m[2]|0;
  529. shouldUpdateCache = true;
  530. }
  531.  
  532. // parse video sources
  533. if (info.url_encoded_fmt_stream_map && info.fmt_list) {
  534. var streams = {};
  535. decodeURIComponent(info.url_encoded_fmt_stream_map).split(',').forEach(function(stream) {
  536. var params = {};
  537. stream.split('&').forEach(function(kv) {
  538. params[kv.split('=')[0]] = decodeURIComponent(kv.split('=')[1]);
  539. });
  540. streams[params.itag] = params;
  541. });
  542. decodeURIComponent(info.fmt_list).split(',').forEach(function(fmt) {
  543. var itag = fmt.split('/')[0];
  544. var dimensions = fmt.split('/')[1];
  545. var stream = streams[itag];
  546. if (stream) {
  547. videoSources.push({
  548. src: stream.url + (stream.s ? '&signature=' + stream.s : ''),
  549. title: stream.quality + ', ' + dimensions + ', ' + stream.type
  550. });
  551. }
  552. });
  553. } else {
  554. var rx = /url=([^=]+?mime%3Dvideo%252F(?:mp4|webm)[^=]+?)(?:,quality|,itag|.u0026)/g;
  555. var text = decodeURIComponent(txt).split('url_encoded_fmt_stream_map')[1];
  556. while ((m = rx.exec(text))) {
  557. videoSources.push({
  558. src: decodeURIComponent(decodeURIComponent(m[1]))
  559. });
  560. }
  561. }
  562.  
  563. var fps = {};
  564. (decodeURIComponent(info.adaptive_fmts || ''))
  565. .split(',')
  566. .filter(function(s) { return /(^|&)type=video/.test(s) && /\b(?:fps=)([\d.]+)/.test(s) })
  567. .forEach(function(s) { fps[s.match(/\b(?:fps=)([\d.]+)/)[1]] = true });
  568. fps = Object.keys(fps).join('/');
  569. if (fps && cache.fps !== fps) {
  570. cache.fps = fps;
  571. shouldUpdateCache = true;
  572. }
  573.  
  574. var duration = div.FYTE.duration = info.length_seconds|0 || '';
  575. if (duration) {
  576. var d = new Date(null);
  577. d.setSeconds(duration);
  578. duration = d.toISOString().replace(/^.+?T[0:]{0,4}(.+?)\..+$/, '$1');
  579. if (cache.duration !== duration) {
  580. cache.duration = duration;
  581. shouldUpdateCache = true;
  582. }
  583. }
  584. if (duration || fps)
  585. duration = '<span>' + duration + '</span>' +
  586. (fps ? '<i>, ' + fps + 'fps</i>' : '');
  587.  
  588. var title = decodeURIComponent(info.title || info.reason || '').replace(/\+/g, ' ');
  589. if (title) {
  590. $(div, '.instant-youtube-title').innerHTML = (title ? '<strong>' + title + '</strong>' : '') + duration;
  591. if (cache.title !== title) {
  592. cache.title = title;
  593. shouldUpdateCache = true;
  594. }
  595. }
  596. if (pinnable !== 'off' && info.title)
  597. makeDraggable(div);
  598.  
  599. if (info.reason) {
  600. div.setAttribute('disabled', '');
  601. if (cache.reason !== info.reason) {
  602. cache.reason = info.reason;
  603. shouldUpdateCache = true;
  604. }
  605. }
  606.  
  607. if (videoSources.length)
  608. div.FYTE.videoSources = videoSources;
  609.  
  610. if (info.storyboard_spec && div.FYTE.state !== 'scheduled play') {
  611. m = decodeURIComponent(decodeURIComponent(info.storyboard_spec)).split('|');
  612. div.FYTE.storyboard = tryJSONparse(
  613. m[m.length-1].replace(
  614. /^(\d+)#(\d+)#(\d+)#(\d+)#(\d+)#.+$/,
  615. '{"w":$1, "h":$2, "len":$3, "rows":$4, "cols":$5}'
  616. ));
  617. if (div.FYTE.storyboard.w * div.FYTE.storyboard.h > 2000) {
  618. div.FYTE.storyboard.url = m[0].replace('?', '&').replace('$L/$N.jpg',
  619. (m.length-2) + '/M0.jpg?sigh=' + m[m.length-1].replace(/^.+?#([^#]+)$/, '$1'));
  620. $(div, '.instant-youtube-options-button').insertAdjacentHTML('beforebegin',
  621. '<div class="instant-youtube-storyboard"' + (showStoryboard ? '' : ' disabled') + '>' +
  622. important('<div style="width:' + (div.FYTE.storyboard.w-1) + 'px; height:' + div.FYTE.storyboard.h + 'px;') +
  623. '">&nbsp;</div>' +
  624. '</div>');
  625. if (showStoryboard)
  626. updateHoverHandler(div);
  627. }
  628. }
  629.  
  630. injectStylesIfNeeded();
  631.  
  632. if (div.FYTE.state === 'scheduled play')
  633. setTimeout(function() { startPlayingDirectly(div) }, 0);
  634.  
  635. div.FYTE.state = '';
  636.  
  637. var cover = decodeURIComponent(info.iurlmaxres || info.iurlhq || info.iurl || '').match(/[^\/]+$/);
  638. if (cover && cache.cover !== cover[0]) {
  639. cache.cover = cover[0];
  640. shouldUpdateCache = true;
  641. }
  642. if (shouldUpdateCache)
  643. localStorage['FYTE-cache-' + info.video_id] = JSON.stringify(cache);
  644. }
  645.  
  646. function fixThumbnailAR(div, w, h) {
  647. var img = $(div, 'img');
  648. if (!img)
  649. return;
  650. var thw = img.naturalWidth, thh = img.naturalHeight;
  651. if (w && h) { // means thumbnail is still loading
  652. div.FYTE.cache.videoWidth = w;
  653. div.FYTE.cache.videoHeight = h;
  654. } else {
  655. w = div.FYTE.cache.videoWidth;
  656. h = div.FYTE.cache.videoHeight;
  657. if (!w || !h)
  658. return;
  659. }
  660. var divw = div.clientWidth, divh = div.clientHeight;
  661. // if both video and thumbnail are 4:3, fit the image to height
  662. //console.log(div, divw, divh, thw, thh, w, h, h/w*divw / divh - 1, thh/thw*divw / divh - 1);
  663. if (Math.abs(h/w*divw / divh - 1) > 0.05 && Math.abs(thh/thw*divw / divh - 1) > 0.05) {
  664. img.style.maxHeight = img.clientHeight + 'px';
  665. if (!div.FYTE.cache.videoWidth) // skip animation if thumbnail is already loaded
  666. img.style.transition = 'height 1s ease, margin-top 1s ease';
  667. setTimeout(function() {
  668. img.style.maxHeight = 'none';
  669. img.style.cssText += important(h/w >= divh/divw ? 'width:auto; height:100%;' : 'width:100%; height:auto;');
  670. setTimeout(function() {
  671. img.style.transition = '';
  672. }, 1000);
  673. }, 0);
  674. }
  675. }
  676.  
  677. function updateHoverHandler(div) {
  678. var sb = $(div, '.instant-youtube-storyboard');
  679. if (!showStoryboard) {
  680. if (!sb.getAttribute('disabled'))
  681. sb.setAttribute('disabled', '');
  682. return;
  683. }
  684. if (sb.hasAttribute('disabled'))
  685. sb.removeAttribute('disabled');
  686.  
  687. sb.addEventListener('click', storyboardClickHandler);
  688.  
  689. var oldIndex = null;
  690. var style = sb.firstElementChild.style;
  691. sb.addEventListener('mousemove', storyboardHoverHandler);
  692. sb.addEventListener('mouseout', storyboardHoverHandler);
  693.  
  694. div.addEventListener('mouseover', storyboardPreloader);
  695. div.addEventListener('mouseout', storyboardPreloader);
  696.  
  697. var spinner = document.createElement('span');
  698. spinner.className = 'instant-youtube-loading-spinner';
  699.  
  700. function storyboardClickHandler(e) {
  701. sb.removeEventListener('click', storyboardClickHandler);
  702. var offsetX = e.offsetX || e.clientX - this.getBoundingClientRect().left;
  703. div.FYTE.startAt = offsetX / this.clientWidth * div.FYTE.duration |0;
  704. div.FYTE.srcEmbedFixed = setUrlParams(div.FYTE.srcEmbedFixed, {start: div.FYTE.startAt});
  705. startPlaying(div, {alternateMode: e.shiftKey});
  706. }
  707.  
  708. function storyboardPreloader(e) {
  709. if (e.type === 'mouseout') {
  710. imageLoader.onload = null; imageLoader.src = '';
  711. spinner.remove();
  712. return;
  713. }
  714. if (!div.FYTE.storyboard || div.FYTE.storyboard.preloaded)
  715. return;
  716. var lastpart = (div.FYTE.storyboard.len-1)/(div.FYTE.storyboard.rows * div.FYTE.storyboard.cols) |0;
  717. if (lastpart <= 0)
  718. return;
  719. var part = 0;
  720. imageLoader.src = setStoryboardUrl(part++);
  721. imageLoader.onload = function() {
  722. if (part > lastpart) {
  723. div.FYTE.storyboard.preloaded = true;
  724. div.removeEventListener('mouseover', storyboardPreloader);
  725. div.removeEventListener('mouseout', storyboardPreloader);
  726. imageLoader.onload = null;
  727. imageLoader.src = '';
  728. spinner.remove();
  729. return;
  730. }
  731. imageLoader.src = setStoryboardUrl(part++);
  732. };
  733. }
  734.  
  735. function setStoryboardUrl(part) {
  736. return div.FYTE.storyboard.url.replace(/M\d+\.jpg\?/, 'M' + part + '.jpg?');
  737. }
  738.  
  739. function storyboardHoverHandler(e) {
  740. if (!showStoryboard || !div.FYTE.storyboard)
  741. return;
  742. if (e.type === 'mouseout')
  743. return imageLoader2.onload && imageLoader2.onload();
  744. var w = div.FYTE.storyboard.w;
  745. var h = div.FYTE.storyboard.h;
  746. var cols = div.FYTE.storyboard.cols;
  747. var rows = div.FYTE.storyboard.rows;
  748. var len = div.FYTE.storyboard.len;
  749. var partlen = rows * cols;
  750.  
  751. var offsetX = e.offsetX || e.clientX - this.getBoundingClientRect().left;
  752. var left = Math.min(this.clientWidth - w, Math.max(0, offsetX - w)) |0;
  753. if (!style.left || parseInt(style.left) !== left) {
  754. style.left = left + 'px';
  755. if (spinner.parentElement)
  756. spinner.style.cssText = important('left:' + (left + w/2 - 10) + 'px; right:auto;');
  757. }
  758.  
  759. var index = Math.min(offsetX / this.clientWidth * (len+1) |0, len - 1);
  760. if (index === oldIndex)
  761. return;
  762.  
  763. var part = index/partlen|0;
  764. if (!oldIndex || part !== (oldIndex/partlen|0)) {
  765. style.cssText = style.cssText.replace(/$|background-image[^;]+;/,
  766. 'background-image: url(' + setStoryboardUrl(part) + ')!important;');
  767. if (!div.FYTE.storyboard.preloaded) {
  768. if (spinner.timer)
  769. clearTimeout(spinner.timer);
  770. spinner.timer = setTimeout(function() {
  771. spinner.timer = 0;
  772. if (!imageLoader2.src)
  773. return;
  774. this.appendChild(spinner);
  775. spinner.style.cssText = important('left:' + (left + w/2 - 10) + 'px; right:auto;');
  776. }.bind(this), 50);
  777. imageLoader2.onload = function() {
  778. clearTimeout(spinner.timer);
  779. spinner.remove();
  780. spinner.timer = 0;
  781. imageLoader2.onload = null;
  782. imageLoader2.src = '';
  783. };
  784. imageLoader2.src = setStoryboardUrl(part);
  785. }
  786. }
  787.  
  788. oldIndex = index;
  789. index = index % partlen;
  790. style.backgroundPosition = '-' + (index % cols) * w + 'px -' + (index / cols |0) * h + 'px';
  791. }
  792. }
  793.  
  794. function clickHandler(e) {
  795. if (e.target.closest('a')
  796. || e.type === 'mousedown' && e.button !== 1
  797. || e.type === 'click' && e.target.matches('.instant-youtube-options, .instant-youtube-options *'))
  798. return;
  799. if (e.type === 'click' && e.target.matches('.instant-youtube-options-button')) {
  800. showOptions(e);
  801. e.preventDefault();
  802. e.stopPropagation();
  803. return;
  804. }
  805.  
  806. e.preventDefault();
  807. e.stopPropagation();
  808. e.stopImmediatePropagation();
  809.  
  810. startPlaying(e.target.closest('.instant-youtube-container'), {
  811. alternateMode: e.shiftKey || e.target.matches('.instant-youtube-alternative'),
  812. fullscreen: e.button === 1,
  813. });
  814. }
  815.  
  816. function startPlaying(div, params) {
  817. div.removeEventListener('click', clickHandler);
  818. div.removeEventListener('mousedown', clickHandler);
  819.  
  820. $$remove(div, '.instant-youtube-alternative, .instant-youtube-storyboard, .instant-youtube-options-button, .instant-youtube-options');
  821. $(div, 'svg').outerHTML = '<span class="instant-youtube-loading-spinner"></span>';
  822.  
  823. if (pinnable !== 'off') {
  824. makePinnable(div);
  825. if (params && params.pin)
  826. $(div, '[pin="' + params.pin + '"]').click();
  827. }
  828.  
  829. if (window !== window.top)
  830. window.parent.postMessage('iframe-allowfs', '*');
  831.  
  832. if ((!!playDirectly + !!(params && params.alternateMode) === 1)
  833. && (div.FYTE.videoSources || div.FYTE.state === 'querying')) {
  834. if (div.FYTE.videoSources)
  835. startPlayingDirectly(div, params);
  836. else {
  837. // playback will start in parseVideoInfo
  838. div.FYTE.state = 'scheduled play';
  839. // fallback to iframe in 5s
  840. setTimeout(function() {
  841. if (div.FYTE.state) {
  842. div.FYTE.state = '';
  843. switchToIFrame.call(div, params);
  844. }
  845. }, 5000);
  846. }
  847. }
  848. else
  849. switchToIFrame.call(div, params);
  850. }
  851.  
  852. function startPlayingDirectly(div, params) {
  853. var video = document.createElement('video');
  854. video.controls = true;
  855. video.autoplay = true;
  856. video.style.cssText = important('position:absolute; left:0; top:0; right:0; bottom:0; padding:0; margin:auto; opacity:0; width:100%; height:100%;');
  857. video.className = 'instant-youtube-embed';
  858. video.volume = GM_getValue('volume', 0.5);
  859.  
  860. div.FYTE.videoSources.forEach(function(src) {
  861. var srcdom = video.appendChild(document.createElement('source'));
  862. Object.keys(src).forEach(function(k) { srcdom[k] = src[k] });
  863. srcdom.onerror = switchToIFrame.bind(div, params);
  864. });
  865.  
  866. overrideCSS($(div, 'img'), {transition: 'opacity 1s', opacity: '0'});
  867.  
  868. if (params && params.fullscreen) {
  869. div.firstElementChild.appendChild(video);
  870. div.setAttribute('playing', '');
  871. video.style.opacity = 1;
  872. goFullscreen(video);
  873. }
  874.  
  875. if (window.chrome && Number(navigator.userAgent.match(/Chrom\D+(\d+)|$/)[1]) < 74) {
  876. video.addEventListener('click', function onClick(debounced) {
  877. if (debounced === true)
  878. video.paused ? video.play() : video.pause();
  879. else
  880. setTimeout(onClick, 0, true);
  881. });
  882. }
  883.  
  884. var title = $(div, '.instant-youtube-title');
  885. if (title) {
  886. video.onpause = function() { title.removeAttribute('hidden') };
  887. video.onplay = function() { title.setAttribute('hidden', true) };
  888. }
  889.  
  890. var switchTimer = setTimeout(switchToIFrame.bind(div, params), 5000);
  891.  
  892. video.onloadedmetadata = div.FYTE.startAt && function() {
  893. clearTimeout(switchTimer);
  894. video.currentTime = div.FYTE.startAt;
  895. };
  896.  
  897. video.onloadeddata = function() {
  898. clearTimeout(switchTimer);
  899. pauseOtherVideos(video);
  900. video.interval = setInterval(function() {
  901. if (video.volume !== GM_getValue('volume', 0.5))
  902. GM_setValue('volume', video.volume);
  903. }, 1000);
  904. if (params && params.fullscreen)
  905. return;
  906. div.setAttribute('playing', '');
  907. div.firstElementChild.appendChild(video);
  908. video.style.opacity = 1;
  909. };
  910. }
  911.  
  912. function switchToIFrame(params, e) {
  913. if (this.querySelector('iframe'))
  914. return;
  915. var div = this;
  916. var wrapper = div.firstElementChild;
  917. var fullscreen = params && params.fullscreen && !e;
  918. if (e instanceof Event) {
  919. console.log('[FYTE] Direct linking canceled on %s, switching to IFRAME player', div.FYTE.srcEmbed);
  920. var video = e.target ? e.target.closest('video') : e.path && e.path[e.path.length-1];
  921. while (video.lastElementChild)
  922. video.lastElementChild.remove();
  923. goFullscreen(video, false);
  924. video.remove();
  925. }
  926.  
  927. var url = setUrlParams(div.FYTE.srcEmbedFixed, {
  928. html5: 1,
  929. autoplay: 1,
  930. autohide: 2,
  931. border: 0,
  932. controls: 1,
  933. fs: 1,
  934. showinfo: 1,
  935. ssl: 1,
  936. theme: 'dark',
  937. enablejsapi: 1,
  938. FYTEfullscreen: fullscreen|0,
  939. });
  940.  
  941. var iframe = document.createElement('iframe');
  942. iframe.src = url;
  943. iframe.className = 'instant-youtube-embed';
  944. iframe.style = important('position:absolute; left:0; top:0; right:0; padding:0; margin:auto; opacity:0;');
  945. iframe.frameBorder = 0;
  946. iframe.allow = 'autoplay; fullscreen';
  947. iframe.setAttribute('allowtransparency', 'true');
  948. iframe.setAttribute('allowfullscreen', 'true');
  949. iframe.setAttribute('width', '100%');
  950. iframe.setAttribute('height', '100%');
  951.  
  952. if (pinnable !== 'off') {
  953. var pin = $(div, '[pin]');
  954. pin.parentNode.insertBefore(iframe, pin);
  955. } else {
  956. wrapper.appendChild(iframe);
  957. }
  958.  
  959. div.setAttribute('iframe', '');
  960. div.setAttribute('playing', '');
  961.  
  962. iframe = $(div, 'iframe');
  963. if (fullscreen) {
  964. goFullscreen(iframe);
  965. iframe.style.opacity = 1;
  966. }
  967.  
  968. iframe.onload = function() {
  969. window.addEventListener('message', YTlistener);
  970. iframe.contentWindow.postMessage('{"event":"listening"}', '*');
  971. };
  972. setTimeout(function() {
  973. iframe.style.opacity = 1;
  974. window.removeEventListener('message', YTlistener);
  975. }, 5000);
  976.  
  977. function YTlistener(e) {
  978. if (e.source !== iframe.contentWindow || !e.data)
  979. return;
  980. var data = tryJSONparse(e.data);
  981. if (!data.info || data.info.playerState !== 1)
  982. return;
  983. window.removeEventListener('message', YTlistener);
  984. pauseOtherVideos(iframe);
  985. iframe.style.opacity = 1;
  986. $$remove(div, 'span, a');
  987. $(div, 'img').style.display = 'none';
  988. }
  989. }
  990.  
  991. function setUrlParams(url, params) {
  992. var names = Object.keys(params);
  993. var parts = url.split('?', 2)
  994. var query = (parts[1] || '').replace(new RegExp('(?:[?&]|^)(?:' + names.join('|') + ')(?:=[^?&]*)?', 'gi'), '');
  995. return parts[0] + '?' + query + (query ? '&' : '?') +
  996. names.map(function(n) { return n + '=' + params[n] }).join('&');
  997. }
  998.  
  999. function pauseOtherVideos(activePlayer) {
  1000. $$(activePlayer.ownerDocument, '.instant-youtube-embed').forEach(function(v) {
  1001. if (v === activePlayer)
  1002. return;
  1003. switch (v.localName) {
  1004. case 'video':
  1005. if (!v.paused)
  1006. v.pause();
  1007. break;
  1008. case 'iframe':
  1009. try { v.contentWindow.postMessage('{"event":"command", "func":"pauseVideo", "args":""}', '*') } catch(e) {}
  1010. break;
  1011. }
  1012. });
  1013. }
  1014.  
  1015. function goFullscreen(el, enable) {
  1016. if (enable !== false)
  1017. el.webkitRequestFullScreen && el.webkitRequestFullScreen()
  1018. || el.mozRequestFullScreen && el.mozRequestFullScreen()
  1019. || el.requestFullScreen && el.requestFullScreen();
  1020. else
  1021. document.webkitCancelFullScreen && document.webkitCancelFullScreen()
  1022. || document.mozCancelFullScreen && document.mozCancelFullScreen()
  1023. || document.cancelFullScreen && document.cancelFullScreen();
  1024. }
  1025.  
  1026. function makePinnable(div) {
  1027. div.firstElementChild.insertAdjacentHTML('beforeend',
  1028. '<div size-gripper></div>' +
  1029. '<div pin="top-left"></div><div pin="top-right"></div><div pin="bottom-right"></div><div pin="bottom-left"></div>');
  1030.  
  1031. $$(div, '[pin]').forEach(function(pin) {
  1032. if (pinnable === 'hide')
  1033. pin.setAttribute('transparent', '');
  1034. pin.onclick = pinClicked;
  1035. });
  1036. $(div, '[size-gripper]').addEventListener('mousedown', startResize, true);
  1037. $(div, '[size-gripper]').addEventListener('mousedown', function() { return false });
  1038.  
  1039. function pinClicked() {
  1040. var pin = this;
  1041. var pinIt = !div.hasAttribute('pinned') || !pin.hasAttribute('active');
  1042. var corner = pin.getAttribute('pin');
  1043. var video = $(div, 'video');
  1044. if (pinIt) {
  1045. $$(div, '[pin][active]').forEach(function(p) { p.removeAttribute('active') });
  1046. pin.setAttribute('active', '');
  1047. if (!div.FYTE.unpinnedStyle) {
  1048. div.FYTE.unpinnedStyle = div.style.cssText;
  1049. var stub = div.cloneNode();
  1050. var img = $(div, 'img').cloneNode();
  1051. img.style.opacity = 1;
  1052. img.style.display = 'block';
  1053. img.title = '';
  1054. stub.appendChild(img);
  1055. stub.onclick = function(e) { $(div, '[pin][active]').onclick(e) };
  1056. stub.style.cssText += 'opacity:0.3!important;';
  1057. stub.setAttribute('stub', '');
  1058. div.FYTE.stub = stub;
  1059. div.parentNode.insertBefore(stub, div);
  1060. }
  1061. var size = constrainPinnedSize(div, localStorage['width#' + location.hostname] || pinnedWidth);
  1062. div.style.cssText = important(
  1063. 'position: fixed;' +
  1064. 'width: ' + size.w + 'px;' +
  1065. 'z-index: 999999999;' +
  1066. 'height:' + size.h + 'px;' +
  1067. 'top:' + (corner.indexOf('top') >= 0 ? '0' : 'auto') + ';' +
  1068. 'bottom:' + (corner.indexOf('bottom') >= 0 ? '0' : 'auto') + ';' +
  1069. 'left:' + (corner.indexOf('left') >= 0 ? '0' : 'auto') + ';' +
  1070. 'right:' + (corner.indexOf('right') >= 0 ? '0' : 'auto') + ';'
  1071. );
  1072. adjustPinnedOffset(div, div, corner);
  1073. div.setAttribute('pinned', corner);
  1074. if (video && document.body)
  1075. document.body.appendChild(div);
  1076. }
  1077. else { // unpin
  1078. pin.removeAttribute('active');
  1079. div.removeAttribute('pinned');
  1080. div.style.cssText = div.FYTE.unpinnedStyle;
  1081. div.FYTE.unpinnedStyle = '';
  1082. if (div.FYTE.stub) {
  1083. if (video && document.body)
  1084. div.FYTE.stub.parentNode.replaceChild(div, div.FYTE.stub);
  1085. div.FYTE.stub.remove();
  1086. div.FYTE.stub = null;
  1087. }
  1088. }
  1089. if (video && video.paused)
  1090. video.play();
  1091. }
  1092.  
  1093. function startResize(e) {
  1094. var siteSaved = ('width#' + location.hostname) in localStorage;
  1095. var saveAs = siteSaved ? 'site' : 'global';
  1096. var oldSizeCSS = {w: div.style.width, h: div.style.height};
  1097. var oldDraggable = div.draggable;
  1098. div.draggable = false;
  1099.  
  1100. var gripper = this;
  1101. gripper.removeAttribute('tried-exceeding');
  1102. gripper.innerHTML = '<div>' +
  1103. '<div save-as="' + saveAs + '"><b>S</b> = Site mode: <span>' + getSiteOnlyText() + '</span></div>' +
  1104. (!siteSaved ? '' : '<div><b>R</b> = Reset to global size</div>') +
  1105. '<div><b>Esc</b> = Cancel</div>' +
  1106. '</div>';
  1107.  
  1108. document.addEventListener('mousemove', resize);
  1109. document.addEventListener('mouseup', resizeDone);
  1110. document.addEventListener('keydown', resizeKeyDown);
  1111. e.stopImmediatePropagation();
  1112. return false;
  1113.  
  1114. function getSiteOnlyText() {
  1115. return saveAs === 'site' ? 'only ' + location.hostname : 'global';
  1116. }
  1117.  
  1118. function resize(e) {
  1119. var deltaX = e.movementX || e.webkitMovementX || e.mozMovementX || 0;
  1120. if (/right/.test(div.getAttribute('pinned')))
  1121. deltaX = -deltaX;
  1122. var newSize = constrainPinnedSize(div, div.clientWidth + deltaX);
  1123. if (newSize.w !== div.clientWidth) {
  1124. div.style.setProperty('width', newSize.w + 'px', 'important');
  1125. div.style.setProperty('height', newSize.h + 'px', 'important');
  1126. gripper.removeAttribute('tried-exceeding');
  1127. } else if (newSize.triedExceeding) {
  1128. gripper.setAttribute('tried-exceeding', '');
  1129. }
  1130. window.getSelection().removeAllRanges();
  1131. return false;
  1132. }
  1133.  
  1134. function resizeDone() {
  1135. div.draggable = oldDraggable;
  1136. gripper.removeAttribute('tried-exceeding');
  1137. gripper.innerHTML = '';
  1138. document.removeEventListener('mousemove', resize);
  1139. document.removeEventListener('mouseup', resizeDone);
  1140. document.removeEventListener('keydown', resizeKeyDown);
  1141. switch (saveAs) {
  1142. case 'site':
  1143. localStorage['width#' + location.hostname] = div.clientWidth;
  1144. break;
  1145. case 'global':
  1146. pinnedWidth = div.clientWidth;
  1147. GM_setValue('pinnedWidth', pinnedWidth);
  1148. // fallthrough to remove the locally saved value
  1149. case 'reset':
  1150. localStorage.removeItem('width#' + location.hostname);
  1151. break;
  1152. case '':
  1153. return false;
  1154. }
  1155. gripper.setAttribute('saveAs', saveAs);
  1156. setTimeout(function() { gripper.removeAttribute('saveAs'); }, 250);
  1157. return false;
  1158. }
  1159.  
  1160. function resizeKeyDown(e) {
  1161. switch (e.keyCode) {
  1162. case 27: // Esc
  1163. saveAs = 'cancel';
  1164. div.style.width = oldSizeCSS.w;
  1165. div.style.height = oldSizeCSS.h;
  1166. break;
  1167. case 83: // S
  1168. saveAs = saveAs === 'site' ? 'global' : 'site';
  1169. $(gripper, '[save-as]').setAttribute('save-as', saveAs);
  1170. $(gripper, '[save-as] span').textContent = getSiteOnlyText();
  1171. return false;
  1172. case 82: // R
  1173. if (!siteSaved)
  1174. return;
  1175. saveAs = 'reset';
  1176. var size = constrainPinnedSize(div, pinnedWidth);
  1177. div.style.width = size.w;
  1178. div.style.height = size.h;
  1179. break;
  1180. default:
  1181. return;
  1182. }
  1183. document.dispatchEvent(new MouseEvent('mouseup'));
  1184. return false;
  1185. }
  1186. }
  1187. }
  1188.  
  1189. function makeDraggable(div) {
  1190. div.draggable = true;
  1191. div.addEventListener('dragstart', function(e) {
  1192. var offsetY = e.offsetY || e.clientY - div.getBoundingClientRect().top;
  1193. if (offsetY > div.clientHeight - 30)
  1194. return e.preventDefault();
  1195.  
  1196. e.dataTransfer.setData('text/plain', '');
  1197.  
  1198. var dropZone = document.createElement('div');
  1199. var dropZoneHeight = 400 / div.FYTE.cache.videoWidth * div.FYTE.cache.videoHeight;
  1200. dropZone.className = 'instant-youtube-dragndrop-placeholder';
  1201.  
  1202. document.body.addEventListener('dragenter', dragHandler);
  1203. document.body.addEventListener('dragover', dragHandler);
  1204. document.body.addEventListener('dragend', dragHandler);
  1205. document.body.addEventListener('drop', dragHandler);
  1206. function dragHandler(e) {
  1207. e.stopImmediatePropagation();
  1208. e.stopPropagation();
  1209. e.preventDefault();
  1210. switch (e.type) {
  1211. case 'dragover':
  1212. var playing = div.hasAttribute('playing');
  1213. var stub = e.target.closest('.instant-youtube-container[stub]') === div.FYTE.stub && div.FYTE.stub;
  1214. var gizmo = playing && !stub
  1215. ? {left:0, top:0, right:innerWidth, bottom:innerHeight}
  1216. : (stub || div).getBoundingClientRect();
  1217. var x = e.clientX, y = e.clientY;
  1218. var cx = (gizmo.left + gizmo.right) / 2;
  1219. var cy = (gizmo.top + gizmo.bottom) / 2;
  1220. var stay = !!stub || y >= cy-200 && y <= cy+200 && x >= cx-200 && x <= cx+200;
  1221. overrideCSS(dropZone, {
  1222. top: y < cy || stay ? '0' : 'auto',
  1223. bottom: y > cy || stay ? '0' : 'auto',
  1224. left: x < cx || stay ? '0' : 'auto',
  1225. right: x > cx || stay ? '0' : 'auto',
  1226. width: playing && stay && stub ? stub.clientWidth+'px' : '400px',
  1227. height: playing && stay && stub ? stub.clientHeight+'px' : dropZoneHeight + 'px',
  1228. margin: playing && stay ? 'auto' : '0',
  1229. position: !playing && stay || stub ? 'absolute' : 'fixed',
  1230. 'background-color': stub ? 'rgba(0,0,255,0.5)' : stay ? 'rgba(255,255,0,0.4)' : 'rgba(0,255,0,0.2)',
  1231. });
  1232. adjustPinnedOffset(dropZone, div);
  1233. (stay && !playing || stub ? (stub || div) : document.body).appendChild(dropZone);
  1234. break;
  1235. case 'dragend':
  1236. case 'drop':
  1237. var corner = calcPinnedCorner(dropZone);
  1238. dropZone.remove();
  1239. dropZone = null;
  1240. document.body.removeEventListener('dragenter', dragHandler);
  1241. document.body.removeEventListener('dragover', dragHandler);
  1242. document.body.removeEventListener('dragend', dragHandler);
  1243. document.body.removeEventListener('drop', dragHandler);
  1244. if (e.type === 'dragend')
  1245. break;
  1246. if (div.hasAttribute('playing'))
  1247. (corner ? $(div, '[pin="' + corner + '"]') : div.FYTE.stub).click();
  1248. else
  1249. startPlaying(div, {pin: corner});
  1250. }
  1251. }
  1252. });
  1253. }
  1254.  
  1255. function adjustPinnedOffset(el, self, corner) {
  1256. var offset = 0;
  1257. $$('.instant-youtube-container[pinned] [pin="' + (corner || calcPinnedCorner(el)) + '"][active]').forEach(function(pin) {
  1258. var container = pin.closest('[pinned]');
  1259. if (container !== el && container !== self) {
  1260. var bounds = container.getBoundingClientRect();
  1261. offset = Math.max(offset, el.style.top === '0px' ? bounds.bottom : innerHeight - bounds.top);
  1262. }
  1263. });
  1264. if (offset)
  1265. el.style[el.style.top === '0px' ? 'top' : 'bottom'] = offset + 'px';
  1266. }
  1267.  
  1268. function calcPinnedCorner(el) {
  1269. var t = el.style.top !== 'auto';
  1270. var b = el.style.bottom !== 'auto';
  1271. var l = el.style.left !== 'auto';
  1272. var r = el.style.right !== 'auto';
  1273. return t && b && l && r ? '' : (t ? 'top' : 'bottom') + '-' + (l ? 'left' : 'right');
  1274. }
  1275.  
  1276. function constrainPinnedSize(div, width) {
  1277. var maxWidth = window.innerWidth - 100 |0;
  1278. var triedExceeding = (width|0) > maxWidth;
  1279. width = Math.max(200, Math.min(maxWidth, width|0));
  1280. return {
  1281. w: width,
  1282. h: width / div.FYTE.cache.videoWidth * div.FYTE.cache.videoHeight,
  1283. triedExceeding: triedExceeding,
  1284. };
  1285. }
  1286.  
  1287. function showOptions(e) {
  1288. var optionsButton = e.target;
  1289. translateHTML(optionsButton, 'afterend', '\
  1290. <div class="instant-youtube-options">\
  1291. <span>\
  1292. <label tl style="width: 100% !important;">Size:&nbsp;\
  1293. <select data-action="size-mode">\
  1294. <option tl value="Original">Original\
  1295. <option tl value="Fit to width">Fit to width\
  1296. <option>360p\
  1297. <option>480p\
  1298. <option>720p\
  1299. <option>1080p\
  1300. <option tl value="Custom">Custom...\
  1301. </select>\
  1302. </label>&nbsp;\
  1303. <label data-action="size-custom" ' + (resizeMode !== 'Custom' ? 'disabled' : '') + '>\
  1304. <input type="number" min="320" max="9999" tl-placeholder="width" data-action="width" step="1" value="' + (resizeWidth||'') + '">\
  1305. x\
  1306. <input type="number" min="240" max="9999" tl-placeholder="height" data-action="height" step="1" value="' + (resizeHeight||'') + '">\
  1307. </label>\
  1308. </span>\
  1309. <label tl="content,title" title="msgStoryboardTip">\
  1310. <input data-action="storyboard" type="checkbox" ' + (showStoryboard ? 'checked' : '') + '>\
  1311. msgStoryboard\
  1312. </label>\
  1313. <span>\
  1314. <label tl="content,title" title="msgDirectTip">\
  1315. <input data-action="direct" type="checkbox" ' + (playDirectly ? 'checked' : '') + '>\
  1316. msgDirect\
  1317. </label>\
  1318. &nbsp;\
  1319. <label tl="content,title" title="msgDirectTip">\
  1320. <input data-action="direct-shown" type="checkbox" ' + (playDirectlyShown ? 'checked' : '') + '>\
  1321. msgDirectShown\
  1322. </label>\
  1323. </span>\
  1324. <label tl="content,title" title="msgSafeTip">\
  1325. <input data-action="safe" type="checkbox" ' + (skipCustom ? 'checked' : '') + '>\
  1326. msgSafe\
  1327. </label>\
  1328. <table>\
  1329. <tr>\
  1330. <td><label tl="content,title" title="msgPinningTip">msgPinning</label></td>\
  1331. <td>\
  1332. <select data-action="pinnable">\
  1333. <option tl value="on">msgPinningOn\
  1334. <option tl value="hide">msgPinningHover\
  1335. <option tl value="off">msgPinningOff\
  1336. </select>\
  1337. </td>\
  1338. </tr>\
  1339. <tr tl="title" title="msgQualityTip">\
  1340. <td><label tl="content">msgQuality</td>\
  1341. <td>\
  1342. <select data-action="quality">\
  1343. <option value="auto" tl>msgQualityAuto\
  1344. <option value="tiny">144p\
  1345. <option value="small">240p\
  1346. <option value="medium">360p\
  1347. <option value="large">480p\
  1348. <option value="hd720">720p\
  1349. <option value="hd1080">1080p\
  1350. <option value="hd1440">1440p\
  1351. <option value="hd2160">2160p (4K)\
  1352. <option value="hd2880">2880p (5K)\
  1353. <option value="highres">4320p (8K)\
  1354. </select>\
  1355. </td>\
  1356. </tr>\
  1357. </table>\
  1358. <span data-action="buttons">\
  1359. <button tl data-action="ok">OK</button>\
  1360. <button tl data-action="cancel">Cancel</button>\
  1361. </span>\
  1362. </div>\
  1363. ');
  1364. var options = optionsButton.nextElementSibling;
  1365.  
  1366. options.addEventListener('keydown', function(e) {
  1367. if (e.target.localName === 'input' &&
  1368. !e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey && e.key.match(/[.,]/))
  1369. return false;
  1370. });
  1371.  
  1372. $(options, '[data-action="size-mode"]').value = resizeMode;
  1373. $(options, '[data-action="size-mode"]').addEventListener('change', function() {
  1374. var v = this.value !== 'Custom';
  1375. var e = $(options, '[data-action="size-custom"]');
  1376. e.children[0].disabled = e.children[1].disabled = v;
  1377. v ? e.setAttribute('disabled', '') : e.removeAttribute('disabled');
  1378. });
  1379.  
  1380. $(options, '[data-action="pinnable"]').value = pinnable;
  1381.  
  1382. $(options, '[data-action="quality"]').value = playQuality;
  1383.  
  1384. $(options, '[data-action="buttons"]').addEventListener('click', function(e) {
  1385. if (e.target.dataset.action !== 'ok') {
  1386. options.remove();
  1387. return;
  1388. }
  1389. var v, shouldAdjust;
  1390. if (resizeMode !== (v = $(options, '[data-action="size-mode"]').value)) {
  1391. GM_setValue('resize', resizeMode = v);
  1392. shouldAdjust = true;
  1393. }
  1394. if (resizeMode === 'Custom') {
  1395. var w = $(options, '[data-action="width"]').value |0;
  1396. var h = $(options, '[data-action="height"]').value |0;
  1397. if (resizeWidth !== w || resizeHeight !== h) {
  1398. updateCustomSize(w, h);
  1399. GM_setValue('width', resizeWidth);
  1400. GM_setValue('height', resizeHeight);
  1401. shouldAdjust = true;
  1402. }
  1403. }
  1404. if (showStoryboard !== (v = $(options, '[data-action="storyboard"]').checked)) {
  1405. GM_setValue('showStoryboard', showStoryboard = v);
  1406. $$('.instant-youtube-container').forEach(updateHoverHandler);
  1407. }
  1408. if (playDirectly !== (v = $(options, '[data-action="direct"]').checked)) {
  1409. GM_setValue('playHTML5', playDirectly = v);
  1410. if (playDirectlyShown) {
  1411. var newAltText = _(playDirectly ? 'Play with Youtube player' : 'Play directly (up to 720p)');
  1412. $$('.instant-youtube-alternative').forEach(function(e) {
  1413. e.textContent = newAltText;
  1414. });
  1415. }
  1416. }
  1417. if (playDirectlyShown !== (v = $(options, '[data-action="direct-shown"]').checked)) {
  1418. GM_setValue('playHTML5', playDirectlyShown = v);
  1419. updateAltPlayerCSS();
  1420. }
  1421. if (skipCustom !== (v = $(options, '[data-action="safe"]').checked)) {
  1422. GM_setValue('skipCustom', skipCustom = v);
  1423. }
  1424. if (pinnable !== (v = $(options, '[data-action="pinnable"]').value)) {
  1425. GM_setValue('pinnable', pinnable = v);
  1426. }
  1427. if (playQuality !== (v = $(options, '[data-action="quality"]').value)) {
  1428. GM_setValue('quality', playQuality = v);
  1429. }
  1430.  
  1431. options.remove();
  1432.  
  1433. if (shouldAdjust)
  1434. adjustNodes(e, e.target.closest('.instant-youtube-container'));
  1435. });
  1436. }
  1437.  
  1438. function updateCustomSize(w, h) {
  1439. resizeWidth = Math.min(9999, Math.max(320, w|0 || resizeWidth|0));
  1440. resizeHeight = Math.min(9999, Math.max(240, h|0 || resizeHeight|0));
  1441. }
  1442.  
  1443. function updateAltPlayerCSS() {
  1444. var s = '.instant-youtube-alternative { display:' + (playDirectlyShown ? 'block' : 'none') + '!important}';
  1445. $('style#instant-youtube-styles').textContent += s;
  1446. return s;
  1447. }
  1448.  
  1449. function important(cssText) {
  1450. return cssText.replace(/;/g, '!important;');
  1451. }
  1452.  
  1453. function tryCatch(func) {
  1454. try {
  1455. return func();
  1456. } catch(e) {
  1457. console.log(e);
  1458. }
  1459. }
  1460.  
  1461. function getFunctionComment(fn) {
  1462. return fn.toString().match(/\/\*([\s\S]*?)\*\/\s*}$/)[1];
  1463. }
  1464.  
  1465. function $(selORnode, sel) {
  1466. return sel ? selORnode.querySelector(sel)
  1467. : document.querySelector(selORnode);
  1468. }
  1469.  
  1470. function $$(selORnode, sel) {
  1471. return Array.prototype.slice.call(
  1472. sel ? selORnode.querySelectorAll(sel)
  1473. : document.querySelectorAll(selORnode));
  1474. }
  1475.  
  1476. function $$remove(selORnode, sel) {
  1477. Array.prototype.forEach.call(
  1478. sel ? selORnode.querySelectorAll(sel)
  1479. : document.querySelectorAll(selORnode),
  1480. function(e) { e.remove() }
  1481. );
  1482. }
  1483.  
  1484. function overrideCSS(e, params) {
  1485. var names = Object.keys(params);
  1486. var style = e.style.cssText.replace(new RegExp('(^|\s|;)(' + names.join('|') + ')(:[^;]+)', 'gi'), '$1');
  1487. e.style.cssText = style.replace(/[^;]\s*$/, '$&;').replace(/^\s*;\s*/, '') +
  1488. names.map(function(n) { return n + ':' + params[n] + '!important' }).join(';') + ';';
  1489. }
  1490.  
  1491. // fix dumb Firefox bug
  1492. function floatPadding(node, style, dir) {
  1493. var padding = style['padding' + dir];
  1494. if (padding.indexOf('%') < 0)
  1495. return parseFloat(padding);
  1496. return parseFloat(padding) * (parseFloat(style.width) || node.clientWidth) / 100;
  1497. }
  1498.  
  1499. function cleanupCache() {
  1500. for (var k in localStorage) {
  1501. if (k.lastIndexOf('FYTE-cache-', 0) === 0
  1502. && Date.now() - Number((tryJSONparse(localStorage[k]) || {}).lastUsed || 0) > CACHE_STALE_DURATION) {
  1503. delete localStorage[k];
  1504. }
  1505. }
  1506. GM_listValues().forEach(function(k) {
  1507. if (k.lastIndexOf('cache-', 0) === 0) {
  1508. GM_deleteValue(k);
  1509. }
  1510. });
  1511. }
  1512.  
  1513. function tryJSONparse(s) {
  1514. try { return JSON.parse(s) }
  1515. catch (e) {}
  1516. }
  1517.  
  1518. function translateHTML(baseElement, place, html) {
  1519. var tmp = document.createElement('div');
  1520. tmp.innerHTML = html;
  1521. $$(tmp, '[tl]').forEach(function(node) {
  1522. (node.getAttribute('tl') || 'content').split(',').forEach(function(what) {
  1523. var child, src, tl;
  1524. if (what === 'content') {
  1525. for (var i = node.childNodes.length-1, n; (i>=0) && (n=node.childNodes[i]); i--) {
  1526. if (n.nodeType === Node.TEXT_NODE && n.textContent.trim()) {
  1527. child = n;
  1528. break;
  1529. }
  1530. }
  1531. } else
  1532. child = node.getAttributeNode(what);
  1533. if (!child)
  1534. return;
  1535. src = child.textContent;
  1536. var srcTrimmed = src.trim();
  1537. tl = src.replace(srcTrimmed, _(srcTrimmed));
  1538. if (src !== tl)
  1539. child.textContent = tl;
  1540. });
  1541. });
  1542. baseElement.insertAdjacentHTML(place, tmp.innerHTML);
  1543. }
  1544.  
  1545. function initTL() {
  1546. var tlSource = {
  1547. 'watch on Youtube': {
  1548. 'ru': 'открыть на Youtube',
  1549. },
  1550. 'Play with Youtube player': {
  1551. 'ru': 'Включить плеер Youtube',
  1552. },
  1553. 'Play directly (up to 720p)': {
  1554. 'ru': 'Включить напрямую (макс. 720p)',
  1555. },
  1556. msgAltPlayerHint: {
  1557. 'en': 'Shift-click to use alternative player',
  1558. 'ru': 'Shift-клик для смены типа плеера',
  1559. },
  1560. 'Options': {
  1561. 'ru': 'Опции',
  1562. },
  1563. 'Size:': {
  1564. 'ru': 'Размер:',
  1565. },
  1566. 'Original': {
  1567. 'ru': 'Исходный',
  1568. },
  1569. 'Fit to width': {
  1570. 'ru': 'На всю ширину',
  1571. },
  1572. 'Custom...': {
  1573. 'ru': 'Настроить...',
  1574. },
  1575. 'width': {
  1576. 'ru': 'ширина',
  1577. },
  1578. 'height': {
  1579. 'ru': 'высота',
  1580. },
  1581. msgStoryboard: {
  1582. 'en': 'Storyboard thumbnails on hover',
  1583. 'ru': 'Раскадровка при наведении курсора',
  1584. },
  1585. msgStoryboardTip: {
  1586. 'en': 'Show storyboard preview on mouse hover at the bottom',
  1587. 'ru': 'Показывать миникадры при наведении мыши на низ кавер-картинки',
  1588. },
  1589. msgDirect: {
  1590. 'en': 'Play directly',
  1591. 'ru': 'Встроенный плеер браузера',
  1592. },
  1593. msgDirectTip: {
  1594. 'en': 'Note: Shift-click a thumbnail to use alternative player',
  1595. 'ru': 'Напоминание: удерживайте клавишу Shift при щелчке на картинке для альтернативного плеера',
  1596. },
  1597. msgDirectShown: {
  1598. 'en': 'Show under play button',
  1599. 'ru': 'Показывать под кнопкой ►',
  1600. },
  1601. msgSafe: {
  1602. 'en': 'Safe (skip videos with enablejsapi=1)',
  1603. 'ru': 'Консервативный режим',
  1604. },
  1605. msgSafeTip: {
  1606. 'en': 'Do not process customized videos with enablejsapi=1 parameter (requires page reload)',
  1607. 'ru': 'Не обрабатывать нестандартные видео с параметром enablejsapi=1 (подействует после обновления страницы)',
  1608. },
  1609. msgPinning: {
  1610. 'en': 'Corner pinning',
  1611. 'ru': 'Закрепление по углам',
  1612. },
  1613. msgPinningTip: {
  1614. 'en': 'Enable corner pinning controls when a video is playing. \nTo restore the video click the active corner pin or the original video placeholder.',
  1615. 'ru': 'Включить шпильки по углам для закрепления видео во время просмотра. \nДля отмены можно нажать еще раз на активированный уголЪ или на заглушку, где исходно было видео',
  1616. },
  1617. msgPinningOn: {
  1618. 'en': 'On',
  1619. 'ru': 'Да',
  1620. },
  1621. msgPinningHover: {
  1622. 'en': 'On, hover a corner to show',
  1623. 'ru': 'Да, при наведении курсора',
  1624. },
  1625. msgPinningOff: {
  1626. 'en': 'Off',
  1627. 'ru': 'Нет',
  1628. },
  1629. msgQuality: {
  1630. 'en': 'Youtube quality',
  1631. 'ru': 'Качество youtube',
  1632. },
  1633. msgQualityTip: {
  1634. 'en': 'Youtube player video quality',
  1635. 'ru': 'Качество видео в youtube-плеере',
  1636. },
  1637. msgQualityAuto: {
  1638. 'en': 'auto',
  1639. 'ru': 'не выбрано',
  1640. },
  1641. 'OK': {
  1642. 'ru': 'ОК',
  1643. },
  1644. 'Cancel': {
  1645. 'ru': 'Оменить',
  1646. },
  1647. };
  1648. var browserLang = navigator.language || navigator.languages && navigator.languages[0] || '';
  1649. var browserLangMajor = browserLang.replace(/-.+/, '');
  1650. var tl = {};
  1651. Object.keys(tlSource).forEach(function(k) {
  1652. var langs = tlSource[k];
  1653. var text = langs[browserLang] || langs[browserLangMajor];
  1654. if (text)
  1655. tl[k] = text;
  1656. });
  1657. return function(src) { return tl[src] || src };
  1658. }
  1659.  
  1660. function injectStylesIfNeeded(force) {
  1661. if (!fytedom[0] && !force)
  1662. return;
  1663. var styledom = $('style#instant-youtube-styles');
  1664. if (styledom) {
  1665. // move our rules to the end of HEAD to increase CSS specificity
  1666. if (styledom.nextElementSibling && document.head)
  1667. document.head.insertBefore(styledom, null);
  1668. return;
  1669. }
  1670. styledom = (document.head || document.documentElement).appendChild(document.createElement('style'));
  1671. styledom.id = 'instant-youtube-styles';
  1672. styledom.textContent = important(getFunctionComment(function() { /*
  1673. .instant-youtube-container,
  1674. .instant-youtube-wrapper * {
  1675. transform: translate3D(0,0,0);
  1676. }
  1677. .instant-youtube-container {
  1678. all: unset;
  1679. contain: strict;
  1680. display: block;
  1681. position: relative;
  1682. overflow: hidden;
  1683. cursor: pointer;
  1684. padding: 0;
  1685. margin: auto;
  1686. font: normal 14px/1.0 sans-serif, Arial, Helvetica, Verdana;
  1687. text-align: center;
  1688. background: black;
  1689. break-inside: avoid-column;
  1690. }
  1691. .instant-youtube-container[disabled] {
  1692. background: #888;
  1693. }
  1694. .instant-youtube-container[disabled] .instant-youtube-storyboard {
  1695. display: none;
  1696. }
  1697. .instant-youtube-container[pinned] {
  1698. box-shadow: 0 0 30px black;
  1699. }
  1700. .instant-youtube-container[playing] {
  1701. contain: none;
  1702. }
  1703. .instant-youtube-wrapper {
  1704. width: 100%;
  1705. height: 100%;
  1706. }
  1707. .instant-youtube-play-button {
  1708. display: block;
  1709. position: absolute;
  1710. width: 85px;
  1711. height: 60px;
  1712. left: 0;
  1713. right: 0;
  1714. top: 0;
  1715. bottom: 0;
  1716. margin: auto;
  1717. }
  1718. .instant-youtube-loading-spinner {
  1719. display: block;
  1720. position: absolute;
  1721. width: 20px;
  1722. height: 20px;
  1723. left: 0;
  1724. right: 0;
  1725. top: 0;
  1726. bottom: 0;
  1727. padding: 0;
  1728. margin: auto;
  1729. pointer-events: none;
  1730. background: url("data:image/gif;base64,R0lGODlhFAAUAJEDAMzMzLOzs39/f////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgADACwAAAAAFAAUAAACPJyPqcuNItyCUJoQBo0ANIxpXOctYHaQpYkiHfM2cUrCNT0nqr4uudsz/IC5na/2Mh4Hu+HR6YBaplRDAQAh+QQFCgADACwEAAIADAAGAAACFpwdcYupC8BwSogR46xWZHl0l8ZYQwEAIfkEBQoAAwAsCAACAAoACgAAAhccMKl2uHxGCCvO+eTNmishcCCYjWEZFgAh+QQFCgADACwMAAQABgAMAAACFxwweaebhl4K4VE6r61DiOd5SfiN5VAAACH5BAUKAAMALAgACAAKAAoAAAIYnD8AeKqcHIwwhGntEWLkO3CcB4biNEIFACH5BAUKAAMALAQADAAMAAYAAAIWnDSpAHa4GHgohCHbGdbipnBdSHphAQAh+QQFCgADACwCAAgACgAKAAACF5w0qXa4fF6KUoVQ75UaA7Bs3yeNYAkWACH5BAUKAAMALAIABAAGAAwAAAIXnCU2iMfaRghqTmMp1moAoHyfIYIkWAAAOw==");
  1731. }
  1732. .instant-youtube-container:hover .ytp-large-play-button-svg {
  1733. fill: #CC181E;
  1734. }
  1735. .instant-youtube-alternative {
  1736. display: block;
  1737. position: absolute;
  1738. width: 20em;
  1739. height: 20px;
  1740. top: 50%;
  1741. left: 0;
  1742. right: 0;
  1743. margin: 60px auto;
  1744. padding: 0;
  1745. border: none;
  1746. text-align: center;
  1747. text-decoration: none;
  1748. text-shadow: 1px 1px 3px black;
  1749. font-weight: bold;
  1750. color: white;
  1751. z-index: 8;
  1752. font-weight: normal;
  1753. font-size: 12px;
  1754. }
  1755. .instant-youtube-alternative:hover {
  1756. text-decoration: underline;
  1757. color: white;
  1758. background: transparent;
  1759. }
  1760. .instant-youtube-embed {
  1761. z-index: 10;
  1762. background: transparent;
  1763. }
  1764. .instant-youtube-title {
  1765. z-index: 20;
  1766. display: block;
  1767. position: absolute;
  1768. width: auto;
  1769. top: 0;
  1770. left: 0;
  1771. right: 0;
  1772. margin: 0;
  1773. padding: 7px;
  1774. border: none;
  1775. text-shadow: 1px 1px 2px black;
  1776. text-align: center;
  1777. text-decoration: none;
  1778. color: white;
  1779. background-color: rgba(0, 0, 0, 0.5);
  1780. }
  1781. .instant-youtube-title strong {
  1782. font: bold 14px/1.0 sans-serif, Arial, Helvetica, Verdana;
  1783. }
  1784. .instant-youtube-title strong:after {
  1785. content: " - $tl:'watch on Youtube'";
  1786. font-weight: normal;
  1787. margin-right: 1ex;
  1788. }
  1789. .instant-youtube-title span {
  1790. color: white;
  1791. }
  1792. .instant-youtube-title span:before {
  1793. content: "(";
  1794. }
  1795. .instant-youtube-title span:after {
  1796. content: ")";
  1797. }
  1798. .instant-youtube-container .instant-youtube-title i {
  1799. all: unset;
  1800. opacity: .5;
  1801. font-style: normal;
  1802. color: white;
  1803. }
  1804. @-webkit-keyframes instant-youtube-fadein {
  1805. from { opacity: 0 }
  1806. to { opacity: 1 }
  1807. }
  1808. @-moz-keyframes instant-youtube-fadein {
  1809. from { opacity: 0 }
  1810. to { opacity: 1 }
  1811. }
  1812. @keyframes instant-youtube-fadein {
  1813. from { opacity: 0 }
  1814. to { opacity: 1 }
  1815. }
  1816. .instant-youtube-container:not(:hover) .instant-youtube-title[hidden] {
  1817. display: none;
  1818. margin: 0;
  1819. }
  1820. .instant-youtube-title:hover {
  1821. text-decoration: underline;
  1822. }
  1823. .instant-youtube-title strong {
  1824. color: white;
  1825. }
  1826. .instant-youtube-options-button {
  1827. opacity: 0.6;
  1828. position: absolute;
  1829. right: 0;
  1830. bottom: 0;
  1831. margin: 0;
  1832. padding: 1.5ex 2ex;
  1833. font-size: 11px;
  1834. text-shadow: 1px 1px 2px black;
  1835. color: white;
  1836. }
  1837. .instant-youtube-options-button:hover {
  1838. opacity: 1;
  1839. background: rgba(0, 0, 0, 0.5);
  1840. }
  1841. .instant-youtube-options {
  1842. display: flex;
  1843. position: absolute;
  1844. right: 0;
  1845. bottom: 0;
  1846. margin: 0;
  1847. padding: 1ex 1ex 2ex 2ex;
  1848. flex-direction: column;
  1849. align-items: flex-start;
  1850. line-height: 1.5;
  1851. text-align: left;
  1852. opacity: 1;
  1853. color: white;
  1854. background: black;
  1855. z-index: 999;
  1856. }
  1857. .instant-youtube-options * {
  1858. width: auto;
  1859. height: auto;
  1860. margin: 0;
  1861. padding: 0;
  1862. font: inherit;
  1863. font-size: 13px;
  1864. vertical-align: middle;
  1865. text-transform: none;
  1866. text-align: left;
  1867. border-radius: 0;
  1868. text-decoration: none;
  1869. color: white;
  1870. background: black;
  1871. }
  1872. .instant-youtube-options > * {
  1873. margin-top: 1ex;
  1874. }
  1875. .instant-youtube-options table {
  1876. all: unset;
  1877. display: table;
  1878. }
  1879. .instant-youtube-options tr {
  1880. all: unset;
  1881. display: table-row;
  1882. }
  1883. .instant-youtube-options td {
  1884. all: unset;
  1885. display: table-cell;
  1886. padding: 2px;
  1887. }
  1888. .instant-youtube-options label > * {
  1889. display: inline;
  1890. }
  1891. .instant-youtube-options select {
  1892. padding: .5ex .25ex;
  1893. border: 1px solid #444;
  1894. -webkit-appearance: menulist;
  1895. }
  1896. .instant-youtube-options [data-action="size-custom"] input {
  1897. width: 9ex;
  1898. padding: .5ex .5ex .4ex;
  1899. border: 1px solid #666;
  1900. }
  1901. .instant-youtube-options [data-action="buttons"] {
  1902. margin-top: 1em;
  1903. }
  1904. .instant-youtube-options button {
  1905. margin: 0 1ex 0 0;
  1906. padding: .5ex 2ex;
  1907. border: 2px solid gray;
  1908. font-weight: bold;
  1909. }
  1910. .instant-youtube-options button:hover {
  1911. border-color: white;
  1912. }
  1913. .instant-youtube-options label[disabled] {
  1914. opacity: 0.25;
  1915. }
  1916. .instant-youtube-storyboard {
  1917. height: 33%;
  1918. max-height: 90px;
  1919. display: block;
  1920. position: absolute;
  1921. left: 0;
  1922. right: 0;
  1923. bottom: 0;
  1924. overflow: visible;
  1925. overflow-x: visible;
  1926. overflow-y: visible;
  1927. }
  1928. .instant-youtube-storyboard:hover {
  1929. background-color: rgba(0,0,0,0.3);
  1930. }
  1931. .instant-youtube-storyboard[disabled] {
  1932. display:none;
  1933. }
  1934. .instant-youtube-storyboard div {
  1935. display: block;
  1936. position: absolute;
  1937. bottom: 0px;
  1938. pointer-events: none;
  1939. border: 3px solid #888;
  1940. box-shadow: 2px 2px 10px black;
  1941. transition: opacity .25s ease;
  1942. background-color: transparent;
  1943. background-origin: content-box;
  1944. opacity: 0;
  1945. }
  1946. .instant-youtube-storyboard:hover div {
  1947. opacity: 1;
  1948. }
  1949. .instant-youtube-container [pin] {
  1950. position: absolute;
  1951. width: 0;
  1952. height: 0;
  1953. margin: 0;
  1954. padding: 0;
  1955. top: auto; bottom: auto; left: auto; right: auto;
  1956. border-style: solid;
  1957. transition: opacity 2.5s ease-in, opacity 0.4s ease-out;
  1958. opacity: 0;
  1959. z-index: 100;
  1960. }
  1961. .instant-youtube-container[playing]:hover [pin]:not([transparent]) {
  1962. opacity: 1;
  1963. }
  1964. .instant-youtube-container[playing] [pin]:hover {
  1965. cursor: alias;
  1966. opacity: 1;
  1967. transition: opacity 0s;
  1968. }
  1969. .instant-youtube-container [pin=top-left][active] { border-top-color: green; }
  1970. .instant-youtube-container [pin=top-left]:hover { border-top-color: #fc0; }
  1971. .instant-youtube-container [pin=top-left] {
  1972. top: 0; left: 0;
  1973. border-width: 10px 10px 0 0;
  1974. border-color: red transparent transparent transparent;
  1975. }
  1976. .instant-youtube-container [pin=top-left][transparent] {
  1977. border-width: 10px 10px 0 0;
  1978. }
  1979. .instant-youtube-container [pin=top-right][active] { border-right-color: green; }
  1980. .instant-youtube-container [pin=top-right]:hover { border-right-color: #fc0; }
  1981. .instant-youtube-container [pin=top-right] {
  1982. top: 0; right: 0;
  1983. border-width: 0 10px 10px 0;
  1984. border-color: transparent red transparent transparent;
  1985. }
  1986. .instant-youtube-container [pin=top-right][transparent] {
  1987. border-width: 0 10px 10px 0;
  1988. }
  1989. .instant-youtube-container [pin=bottom-right][active] { border-bottom-color: green; }
  1990. .instant-youtube-container [pin=bottom-right]:hover { border-bottom-color: #fc0; }
  1991. .instant-youtube-container [pin=bottom-right] {
  1992. bottom: 0; right: 0;
  1993. border-width: 0 0 10px 10px;
  1994. border-color: transparent transparent red transparent;
  1995. }
  1996. .instant-youtube-container [pin=bottom-right][transparent] {
  1997. border-width: 0 0 10px 10px;
  1998. }
  1999. .instant-youtube-container [pin=bottom-left][active] { border-left-color: green; }
  2000. .instant-youtube-container [pin=bottom-left]:hover { border-left-color: #fc0; }
  2001. .instant-youtube-container [pin=bottom-left] {
  2002. bottom: 0; left: 0;
  2003. border-width: 10px 0 0 10px;
  2004. border-color: transparent transparent transparent red;
  2005. }
  2006. .instant-youtube-container [pin=bottom-left][transparent] {
  2007. border-width: 10px 0 0 10px;
  2008. }
  2009. .instant-youtube-dragndrop-placeholder {
  2010. z-index: 999999999;
  2011. margin: 0;
  2012. padding: 0;
  2013. background: rgba(0, 255, 0, 0.1);
  2014. border: 2px dotted green;
  2015. box-sizing: border-box;
  2016. pointer-events: none;
  2017. }
  2018. .instant-youtube-container [size-gripper] {
  2019. width: 0;
  2020. position: absolute;
  2021. top: 0;
  2022. bottom: 0;
  2023. cursor: e-resize;
  2024. border-color: rgba(50,100,255,0.5);
  2025. border-width: 12px;
  2026. background: rgba(50,100,255,0.2);
  2027. z-index: 99;
  2028. opacity: 0;
  2029. transition: opacity .1s ease-in-out, border-color .1s ease-in-out;
  2030. }
  2031. .instant-youtube-container[pinned*="right"] [size-gripper] {
  2032. border-style: none none none solid;
  2033. left: -4px;
  2034. }
  2035. .instant-youtube-container[pinned*="left"] [size-gripper] {
  2036. border-style: none solid none none;
  2037. right: -4px;
  2038. }
  2039. .instant-youtube-container [size-gripper]:hover {
  2040. opacity: 1;
  2041. }
  2042. .instant-youtube-container [size-gripper]:active {
  2043. opacity: 1;
  2044. width: auto;
  2045. left: -4px;
  2046. right: -4px;
  2047. }
  2048. .instant-youtube-container [size-gripper][tried-exceeding] {
  2049. border-color: rgba(255,0,0,0.5);
  2050. }
  2051. .instant-youtube-container [size-gripper][saveAs="global"] {
  2052. border-color: rgba(0,255,0,0.5);
  2053. }
  2054. .instant-youtube-container [size-gripper][saveAs="site"] {
  2055. border-color: rgba(0,255,255,0.5);
  2056. }
  2057. .instant-youtube-container [size-gripper][saveAs="reset"] {
  2058. border-color: rgba(255,255,0,0.5);
  2059. }
  2060. .instant-youtube-container [size-gripper][saveAs="cancel"] {
  2061. border-color: rgba(255,0,255,0.25);
  2062. }
  2063. .instant-youtube-container [size-gripper] > div {
  2064. white-space: nowrap;
  2065. color: white;
  2066. font-weight: normal;
  2067. line-height: 1.25;
  2068. text-align: left;
  2069. position: absolute;
  2070. top: 50%;
  2071. padding: 1ex 1em 1ex;
  2072. background-color: rgba(80,150,255,0.5);
  2073. }
  2074. .instant-youtube-container [size-gripper] [save-as="site"] {
  2075. font-weight: bold;
  2076. color: yellow;
  2077. }
  2078. .instant-youtube-container[pinned*="left"] [size-gripper] > div {
  2079. right: 0;
  2080. }
  2081. */}).replace(/\$tl:'(.+?)'/g, function(m, m1) { return _(m1) })
  2082. ) +
  2083. updateAltPlayerCSS();
  2084. }