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.

当前为 2017-05-22 提交的版本,查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址