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.

当前为 2020-05-04 提交的版本,查看 最新版本

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