YouTube Me Again!

ytma! automatically converts YouTube(TM), Vimeo, Vine, Soundcloud, WebM, and MP4 links into real embedded videos.

目前为 2017-10-03 提交的版本。查看 最新版本

  1. /*jslint maxerr: 500, browser: true, devel: true, bitwise: true, white: true */
  2. // ==UserScript==
  3. // Do not modify and re-release this script!
  4. // If you would like to add support for other sites, please tell me and I'll put it in the includes.
  5.  
  6. // @id youtube-me-again
  7. // @name YouTube Me Again!
  8. // @namespace hateradio)))
  9. // @author hateradio
  10. // @version 7.2.3
  11. // @description ytma! automatically converts YouTube(TM), Vimeo, Vine, Soundcloud, WebM, and MP4 links into real embedded videos.
  12. // @homepage https://greasyfork.org/en/scripts/1023-youtube-me-again
  13. // @icon https://www.dropbox.com/s/b85qmq0bsim407s/ytma32.png?dl=1
  14. // @icon64 https://www.dropbox.com/s/5zw3al38yf39wxb/ytma64.png?dl=1
  15. // @screenshot https://www.dropbox.com/s/syy9916b1prygl9/ytmascreen5.png?dl=1
  16.  
  17. // @include https://vine.co/v/*/embed/simple
  18. // @match https://vine.co/v/*/embed/simple
  19.  
  20. // @include http*://*youtube-nocookie.com/embed/*
  21. // @match *://*.youtube-nocookie.com/embed/*
  22.  
  23. // @include http*://*youtube.com/embed/*
  24. // @match *://*.youtube.com/embed/*
  25.  
  26. // @include https://gfycat.com/iframe/*
  27. // @match https://gfycat.com/iframe/*
  28.  
  29. // @include http*://*.neogaf.com/forum/showthread.php*
  30. // @include http*://*.neogaf.com/forum/showpost.php?p*
  31. // @include http*://*.neogaf.com/forum/newreply.php*
  32. // @include http*://*.neogaf.com/forum/editpost.php*
  33. // @include http*://*.neogaf.com/forum/private.php*
  34.  
  35. // @match *://*.neogaf.com/forum/showthread.php*
  36. // @match *://*.neogaf.com/forum/showpost.php?p*
  37. // @match *://*.neogaf.com/forum/newreply.php*
  38. // @match *://*.neogaf.com/forum/editpost.php*
  39. // @match *://*.neogaf.com/forum/private.php*
  40.  
  41. // @updated 02 Oct 2018
  42.  
  43. // @grant GM_xmlhttpRequest
  44. // @grant unsafeWindow
  45.  
  46. // @run-at document-end
  47. // ==/UserScript==
  48.  
  49. /*
  50.  
  51. ## Updates
  52.  
  53. #### 7.2.3
  54.  
  55. * Fix: Parses hours from YouTube URLs
  56.  
  57. #### 7.2.2
  58.  
  59. * Updates YouTube iFrame to hide related video feature when pausing
  60.  
  61. #### 7.2.1
  62.  
  63. * New: Extension info
  64. * Updates JSHint options
  65. * Removes outdated @include links
  66.  
  67. #### 7.2
  68.  
  69. * New: CSS rule to make videos fit within smaller windows
  70. * New: GitHub repository and update links
  71. * New: Streamable favicon
  72. * Fix: Vimeo favicon
  73.  
  74. #### 7.1
  75.  
  76. * HTTPS links for Vimeo and Gfycat
  77. * Fix: Safari bug
  78.  
  79. #### 7
  80.  
  81. * New: NeoGAF HTTPS Support
  82. * New: Streamable.com added
  83. * New: Soundcloud playlist support
  84. * Improved time parser
  85. * Upon scrolling, cached descriptions are shown
  86. * Code reorganization makes adding new media sites easier
  87.  
  88. ### 6
  89.  
  90. * New: Imgur GIFV (WEBM/MP4) support
  91. * New: Button to remove cache (descriptions/thumbnail links/etc)
  92. * New: SoundCloud playlist support
  93. * Default video quality is now 720p/HD
  94. * Soundcloud now uses HTML5 player
  95. * Players that open on scroll will no longer trigger the opening of players higher on the page
  96. * Adds HTML5, Gfycat, Imgur icons on links
  97. * Improved Soundcloud and GfyCat URL matchers
  98. * Restructured code base to simplify creation of media controls
  99. * Restructured CSS
  100. * Patched back Gfycat iFrame setting for Safari (it is incompatible with new settings)
  101. * Updates YouTube data API
  102. * Removes:
  103. * Object tag for YouTube for Flash (Deprecated)
  104. * "Batch" loading of descriptions (Only manual and scroll methods are supported)
  105.  
  106. // #Updates
  107.  
  108. Whitelist these sites on NoScript/NotScript/etc.
  109. ------------------------------------------------
  110.  
  111. * neogaf.com
  112. * youtube.com
  113. * youtube-nocookie.com
  114. * googlevideo.com (HTML5 player sends videos from this domain)
  115. * googleapis.com (YT video descriptions)
  116. * vimeo.com
  117. * vimeocdn.com
  118. * soundcloud.com
  119. * sndcdn.com
  120. * vineco.com
  121. * vine.com
  122. * vine.co
  123. * gfycat.com
  124. * github.io
  125.  
  126.  
  127. Whitelist these on Ghostery
  128. ---------------------------
  129.  
  130. * SoundCloud (Widgets, Audio / Music Player)
  131.  
  132. */
  133.  
  134. (function () {
  135. 'use strict';
  136.  
  137. var $$, strg, update;
  138.  
  139. if (!Function.prototype.bind) {
  140. Function.prototype.bind = function (self) {
  141. var args = [].slice.call(arguments, 1), fn = this;
  142. return function () {
  143. return fn.apply(self, args.concat([].slice.call(arguments)));
  144. };
  145. };
  146. }
  147.  
  148. function isNumber(n) {
  149. return !isNaN(parseFloat(n)) && isFinite(n);
  150. }
  151.  
  152. function inject(func) {
  153. var script = document.createElement('script');
  154. script.type = 'text/javascript';
  155. script.textContent = '(' + func + ')();';
  156. document.body.appendChild(script);
  157. document.body.removeChild(script);
  158. }
  159.  
  160. function removeSearch(uri, keepHash) { // removes search query from a uri
  161. var s = uri.indexOf('?'), h = uri.indexOf('#'), hash = '';
  162. if (s > -1) {
  163. if (keepHash && h > -1) {
  164. hash = uri.substr(h);
  165. }
  166. uri = uri.substr(0, s) + hash;
  167. }
  168. return uri;
  169. }
  170.  
  171. // D O M Handle
  172. $$ = {
  173. s: function (selector, cb) { var s = document.querySelectorAll(selector), i = -1; while (++i < s.length) { if (cb(s[i], i, s) === false) { break; } } },
  174. o: function (object, cb) { var i; for (i in object) { if (object.hasOwnProperty(i)) { if (cb(i, object[i], object) === false) { break; } } } },
  175. a: function (e) { var i = 1, j = arguments.length, f = document.createDocumentFragment(); for (i; i < j; i++) { if (arguments[i]) { f.appendChild(arguments[i]); } } e.appendChild(f); return e; },
  176. e: function (t, o, e, p) {
  177. var c = document.createElement(t);
  178. $$.o(o, function (k, v) {
  179. var b = k.charAt(0);
  180. switch (b) {
  181. case '_':
  182. c.dataset[k.substring(1)] = v;
  183. break;
  184. case '$':
  185. c.setAttribute(k.substring(1), v);
  186. break;
  187. default:
  188. c[k] = v;
  189. }
  190. });
  191.  
  192. if (e && p) {
  193. c.appendChild(e);
  194. } else if (e) {
  195. e.appendChild(c);
  196. }
  197. return c;
  198. },
  199. x: function (selector) { return this.ary(document.querySelectorAll(selector)); },
  200. ary: function (ary) { return Array.from ? Array.from(ary) : Array.prototype.slice.call(ary); },
  201. top: document.head || document.body,
  202. css: function (t) {
  203. if (!this.style) {
  204. this.style = document.createElement('style');
  205. this.style.type = 'text/css';
  206. this.top.appendChild(this.style);
  207. }
  208. this.style.appendChild(document.createTextNode(t + '\n'));
  209. },
  210. js: function (t) {
  211. var j = document.createElement('script');
  212. j.type = 'text/javascript';
  213. j[/^https?\:\/\//i.test(t) ? 'src' : 'textContent'] = t;
  214. this.top.appendChild(j);
  215. }
  216. };
  217.  
  218. // S T O R A G E HANDLE
  219. strg = {
  220. MAX: 5012,
  221. on: false,
  222. test: function () { try { var a, b = localStorage, c = Math.random().toString(16).substr(2, 8); b.setItem(c, c); a = b.getItem(c); return a === c ? !b.removeItem(c) : false; } catch (e) { return false; } },
  223. read: function (key) {
  224. try {
  225. return JSON.parse(localStorage.getItem(key));
  226. } catch (e) {
  227. console.error(e.lineNumber + ':' + e.message);
  228. return undefined;
  229. }
  230. },
  231. save: function (key, val) { return this.on ? !localStorage.setItem(key, JSON.stringify(val)) : false; },
  232. wipe: function (key) { return this.on ? !localStorage.removeItem(key) : false; },
  233. zero: function (o) { var k; for (k in o) { if (o.hasOwnProperty(k)) { return false; } } return true; },
  234. grab: function (key, def) { var s = strg.read(key); return strg.zero(s) ? def : s; },
  235. size: function () {
  236. var length = 0, key;
  237. try {
  238. for (key in window.localStorage) {
  239. if (window.localStorage.hasOwnProperty(key)) {
  240. length += window.localStorage[key].length;
  241. }
  242. }
  243. } catch (e) {}
  244. return 3 + ((length * 16) / (8 * 1024));
  245. },
  246. full: function () {
  247. try {
  248. var date = +(new Date());
  249. localStorage.setItem(date, date);
  250. localStorage.removeItem(date);
  251. return false;
  252. } catch (e) {
  253. if (e.name === 'QuotaExceededError' ||
  254. e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
  255. return true;
  256. }
  257. }
  258. },
  259. init: function () { this.on = this.test(); }
  260. };
  261. strg.init();
  262.  
  263. // U P D A T E HANDLE
  264. update = {
  265. name: 'ytma!',
  266. version: 7230,
  267. key: 'ujs_YTMA_UPDT_HR',
  268. callback: 'ytmaupdater',
  269. page: 'https://greasyfork.org/scripts/1023-youtube-me-again',
  270. urij: 'https://hateradio.github.io/ytma/update.json',
  271. interval: 5,
  272. day: (new Date()).getTime(),
  273. time: function () { return new Date(this.day + (1000 * 60 * 60 * 24 * this.interval)).getTime(); },
  274. notification: function (j) {
  275. if (this.version < j.version) {
  276. strg.save(this.key, { date: this.time(), version: j.version, page: j.page });
  277. this.link();
  278. }
  279. },
  280. link: function () {
  281. this.csstxt();
  282.  
  283. var a = document.createElement('a'), b = strg.read(this.key);
  284. a.href = b.page || this.page;
  285. a.id = 'userscriptupdater2';
  286. a.title = 'Update now.';
  287. a.target = '_blank';
  288. a.textContent = 'An update for ' + this.name + ' is available.';
  289. a.addEventListener('click', function () { this.style.display = 'none'; }, false);
  290. document.body.appendChild(a);
  291. },
  292. xhr: function () {
  293. var x = new XMLHttpRequest();
  294. x.addEventListener('load', function () { update.notification(JSON.parse(this.responseText)); }, false);
  295. x.open('get', update.urij, true);
  296. x.send();
  297. },
  298. check: function (opt) {
  299. if (!strg.on) { return; }
  300. if (window.chrome && window.chrome.extension) { return; }
  301. var stored = strg.read(this.key), page;
  302.  
  303. if (opt || !stored || stored.date < this.day) {
  304. page = (stored && stored.page) || this.page;
  305. strg.save(this.key, {date: this.time(), version: this.version, page: page});
  306. this.xhr();
  307. } else if (this.version < stored.version) {
  308. this.link();
  309. }
  310. },
  311. csstxt: function () {
  312. if (!this.pop) { this.pop = true; $$.css('#userscriptupdater2,#userscriptupdater2:visited{box-shadow:1px 1px 6px #7776;border-bottom:3px solid #d65e55;cursor:pointer;color:#555;font-family:sans-serif;font-size:12px;font-weight:700;text-align:justify;position:fixed;z-index:999999;right:10px;top:10px;background:#ebebeb url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxOTguODQ4NTMgMTk5LjM4MzA3Ij48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtNC4yNzYgLTE2LjM2NykiPjxjaXJjbGUgY3g9IjEwNC4zMjEiIGN5PSIxMTYuMzI3IiByPSI5OC4yNzQiIGZpbGw9IiNkNjVlNTUiLz48cGF0aCBmaWxsPSIjZTljZTAyIiBzdHJva2U9IiNlOWM4MDIiIHN0cm9rZS13aWR0aD0iMTYuNyIgZD0iTTE2Ni40NSAxNTcuMzEySDQxLjg5bDMxLjE0LTUzLjkzNSAzMS4xNC01My45MzUgMzEuMTM3IDUzLjkzNXoiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz48dGV4dCB4PSI4NS42NDMiIHk9IjE1MS44NjYiIGZpbGw9IiNkNjVlNTUiIHN0cm9rZS13aWR0aD0iMS40NzciIHN0eWxlPSJsaW5lLWhlaWdodDoxLjI1Oy1pbmtzY2FwZS1mb250LXNwZWNpZmljYXRpb246J0Jvb2sgQW50aXF1YSciIGZvbnQtd2VpZ2h0PSI0MDAiIGZvbnQtc2l6ZT0iNTkuMDg4IiBmb250LWZhbWlseT0iQm9vayBBbnRpcXVhIiBsZXR0ZXItc3BhY2luZz0iMCIgd29yZC1zcGFjaW5nPSIwIj48dHNwYW4geD0iODUuNjQzIiB5PSIxNTEuODY2IiBzdHlsZT0iLWlua3NjYXBlLWZvbnQtc3BlY2lmaWNhdGlvbjonQm9vayBBbnRpcXVhJyIgZm9udC13ZWlnaHQ9IjcwMCIgZm9udC1zaXplPSIxMjYuMDU0Ij4hPC90c3Bhbj48L3RleHQ+PC9nPjwvc3ZnPg==) no-repeat 10px center;background-size:40px;padding:0 20px 0 60px;height:55px;line-height:55px}#userscriptupdater2:hover,#userscriptupdater2:visited:hover{color:#b33a3a !important;border-color:#ce4b30}'); }
  313. }
  314. };
  315. update.check();
  316.  
  317. /** Y T M A CLASS
  318. * Bare YTMA class, filled through _new() or _reactivate()
  319. */
  320. function YTMA() {}
  321.  
  322. YTMA.events = {
  323. clicks: function (e) { // YTMA global click dispatcher
  324. var t = e.target;
  325.  
  326. if (t) {
  327. // console.log('YTMA.clicks');
  328. if (t.tagName === 'VAR' && t.hasAttribute('data-ytmuid')) { // trigger the ui
  329. console.log('show', t.dataset.ytmuid);
  330. YTMA.UI.createFromTrigger(t).showPlayer();
  331. } else if (t.hasAttribute('data-ytmdescription')) {
  332. console.log('load', t.dataset.ytmid);
  333. YTMA.external.events.manualLoad(e);
  334. }
  335. }
  336. },
  337. thumb: {
  338. start: function (e) {
  339. var el = e.target;
  340. el.dataset.thumb = el.dataset.thumb > 0 ? (el.dataset.thumb % 3) + 1 : 2;
  341. el.style.backgroundImage = ['url(https://i3.ytimg.com/vi/', el.dataset.ytmid, '/', el.dataset.thumb, '.jpg)'].join('');
  342. el.dataset.timeout = window.setTimeout(YTMA.events.thumb.start.bind(this, e), 800);
  343. },
  344. stop: function (e) {
  345. window.clearTimeout(e.target.dataset.timeout);
  346. }
  347. }
  348. };
  349.  
  350. YTMA.num = 0;
  351.  
  352. YTMA.addToSet = function (ytma) {
  353. YTMA.set[ytma.data.uid] = ytma;
  354. };
  355.  
  356. YTMA.create = function (link) {
  357. return YTMA.grabIdAndSite(link, function (data, err) {
  358. if (err) {
  359. console.error(link.href, err);
  360. return {};
  361. }
  362.  
  363. var y = new YTMA()._new(data.id, data.site, link);
  364. YTMA.addToSet(y);
  365. y.setup();
  366.  
  367. return y;
  368. });
  369. };
  370.  
  371. YTMA.grabIdAndSite = function (link, cb) {
  372. var uri = link.href, id, site, match;
  373. try {
  374. site = YTMA.reg.siteByTest[YTMA.reg.siteExpressions.test(uri) ? RegExp.lastMatch : ''];
  375. // console.log(site);
  376.  
  377. if (site === 'html5') { // || site === 'html5-audio'
  378. id = uri.slice(-15);
  379. } else if (site === 'soundcloud') {
  380. if (!YTMA.reg.extra.soundcloud.playlist.test(uri)) {
  381. link.href = uri = YTMA.reg.fix.soundcloud(uri);
  382. }
  383.  
  384. match = YTMA.DB.sites.soundcloud.matcher.exec(uri);
  385. id = YTMA.escapeId(match[1]);
  386.  
  387. if (match && YTMA.reg.extra.soundcloud.tracks.test(uri)) {
  388. id = id.slice(-50);
  389. }
  390. } else {
  391. id = uri.match(YTMA.DB.sites[site].matcher)[1];
  392. }
  393.  
  394. console.log(id, site, match);
  395. if (id && YTMA.DB.sites[site]) {
  396. return cb({id: id, site: site}, null);
  397. }
  398. throw TypeError('Invalid ID/Site: ' + id + ' @ ' + site);
  399. } catch (e) {
  400. return cb(null, e);
  401. }
  402. };
  403.  
  404. YTMA.escapeId = function (id) {
  405. return (id += '').replace(/(?:\W)/g, '_');
  406. };
  407.  
  408. YTMA.route = {
  409. host: document.location.host.replace('www.', ''),
  410. control: {
  411. $: {
  412. patchSafari: function () {
  413. delete YTMA.DB.sites.gfycat.videoTag;
  414. delete YTMA.DB.sources.gfycat;
  415. },
  416. checkStorage: function () {
  417. if (strg.full() === true) {
  418. console.log('YTMA ERROR: Storage is full!');
  419. try {
  420. localStorage.removeItem(YTMA.external.version);
  421. strg.on = strg.test();
  422. } catch (e) {
  423. console.error(e);
  424. }
  425. }
  426. },
  427. runOnce: function () {
  428. if (!document.body.hasAttribute('ytma-enabled')) {
  429. document.body.setAttribute('ytma-enabled', true);
  430.  
  431. this.checkStorage();
  432.  
  433. if (!YTMA.DB.extension) { update.check(); }
  434.  
  435. YTMA.css();
  436. YTMA.user.init();
  437. YTMA.DB.postInit();
  438.  
  439. document.body.addEventListener('click', YTMA.events.clicks, false);
  440. }
  441. }
  442. },
  443. go: function (host) {
  444. if (/(?:googlevideo|youtube-nocookie\.com|youtube\.com\.?)/i.test(host)) {
  445. this.sites.youtube();
  446. } else if (this.sites[host]) {
  447. this.sites[host]();
  448. } else {
  449. this.sites.$generic();
  450. }
  451. },
  452. sites: {
  453. $generic: function () {
  454. YTMA.route.control.$.runOnce();
  455.  
  456. if (YTMA.DB.browser.safari) { // safari patch
  457. YTMA.route.control.$.patchSafari();
  458. }
  459.  
  460. if (YTMA.selector.processor() > 0) {
  461. YTMA.user.fn.loadPreferences();
  462. }
  463. },
  464. 'gfycat.com': function () {
  465. var v = document.querySelector('video');
  466. v.controls = true;
  467. $$.css('body,html {overflow:hidden;height:100%;width:100%} video {display:table;height:100%;margin:0 auto;}');
  468. document.body.appendChild(v);
  469. },
  470. 'vine.co': function () {
  471. // console.log('vine.co');
  472.  
  473. window.addEventListener('resize', function () {
  474. $$.s('[style]', function (e) {
  475. e.removeAttribute('style');
  476. });
  477. });
  478. },
  479. youtube: function () { // lets force some quality parity
  480. console.log('now inside youtube . . .', document.location);
  481.  
  482. if (/(?:vq=(\w+))/.test(document.location.search)) {
  483. document.body.setAttribute('gm-player-quality', RegExp.lastParen);
  484. }
  485.  
  486. if (/(?:volume=(\d+))/.test(document.location.search)) {
  487. document.body.setAttribute('gm-player-volume', RegExp.lastParen);
  488. }
  489.  
  490. inject(function () {
  491. var max = 10, count = 1, intr = window.setInterval(function () {
  492. console.log('inside says: ', count, !!window.yt);
  493. if (window.yt && window.player) {
  494. window.clearInterval(intr);
  495.  
  496. var p = window.yt.player.getPlayerByElement(window.player);
  497.  
  498. if (document.body.hasAttribute('gm-player-quality')) {
  499. console.log('inside says: setting quality to ', document.body.getAttribute('gm-player-quality'));
  500. p.setPlaybackQuality(document.body.getAttribute('gm-player-quality'));
  501. }
  502.  
  503. if (document.body.hasAttribute('gm-player-volume')) {
  504. console.log('inside says: setting volume to ', document.body.getAttribute('gm-player-volume'));
  505. p.setVolume(document.body.getAttribute('gm-player-volume'));
  506. }
  507. } else {
  508. console.log(count);
  509. count += 1;
  510. if (count > max) {
  511. window.clearInterval(intr);
  512. }
  513. }
  514. }, 500);
  515. });
  516. }
  517. }
  518. },
  519. load: function () {
  520. this.control.go(this.host);
  521. }
  522. };
  523.  
  524. YTMA.main = function () {
  525. YTMA.reg.siteExpressions = YTMA.DB.views.getAllSiteRegExps();
  526. // console.log(YTMA.reg.siteExpressions);
  527. YTMA.route.load();
  528. };
  529.  
  530. YTMA.set = {};
  531.  
  532. YTMA.collect = function (id) {
  533. var i, a = [];
  534. for (i in YTMA.set) {
  535. if (YTMA.set.hasOwnProperty(i) && YTMA.set[i].data.id === id) {
  536. a.push(YTMA.set[i]);
  537. }
  538. }
  539. return a;
  540. };
  541.  
  542. YTMA.reg = {
  543. siteExpressions: null,
  544. time: /(?:t\=(?:(\d+)h)?(?:(\d+)m)?(\d+))/,
  545. ios: /(?:\b(?:ipod|iphone|ipad))\b/i,
  546. extra: {
  547. soundcloud: {
  548. playlist: /(?:soundcloud\.com\/.+\/sets\/)/,
  549. tracks: /(?:soundcloud\.com\/.+\/tracks\/)/
  550. }
  551. },
  552. siteByTest: {
  553. youtu: 'youtube',
  554. vimeo: 'vimeo',
  555. vine: 'vine',
  556. gfycat: 'gfycat',
  557. imgur: 'imgur',
  558. '.webm': 'html5',
  559. '.mp4': 'html5',
  560. // '.mp3': 'html5-audio',
  561. '.gifv': 'html5',
  562. soundcloud: 'soundcloud',
  563. 'streamable.com': 'streamable'
  564. },
  565. fix: {
  566. soundcloud: function (uri) {
  567. var match = YTMA.DB.sites.soundcloud.matcher.exec(uri), id;
  568. if (match) {
  569. id = match[1].split('/', 2).join('/');
  570. uri = removeSearch('https://soundcloud.com/' + id, true);
  571. }
  572.  
  573. return uri;
  574. }
  575. }
  576. };
  577.  
  578. YTMA.selector = { // to build the selector
  579. parentBlacklist: ['.smallfont', '.colhead_dark', '.spoiler', 'pre'],
  580. chrome37Blacklist: 'a[href*="pomf.se/"]',
  581. ignore: function () {
  582. var i, j, ignore = [], all = YTMA.DB.views.getAllSiteSelectors().split(','), blacklist = this.parentBlacklist;
  583. for (i = 0; i < blacklist.length; i++) {
  584. for (j = 0; j < all.length; j++) {
  585. ignore.push(blacklist[i] + ' ' + all[j]);
  586. }
  587. }
  588. //console.log(ignore);
  589. return ignore.join(',');
  590. },
  591. links: function () {
  592. var links;
  593.  
  594. $$.x(YTMA.selector.ignore()).map(function (el) { el.setAttribute('ytmaignore', true); });
  595.  
  596. links = $$.x(YTMA.DB.views.getAllSiteSelectors()).filter(function (el) {
  597. var r = !el.hasAttribute('ytmaprocessed') && !el.hasAttribute('ytmaignore');
  598. el.setAttribute('ytmaprocessed', true);
  599. return r;
  600. });
  601.  
  602. return links;
  603. },
  604. processor: function () {
  605. var links = this.links();
  606.  
  607. if (links.length > 0) {
  608. if (window.chrome && (/(?:Chrome\/(\d+))/.exec(window.navigator.appVersion) && RegExp.lastParen < 38)) {
  609. $$.s(YTMA.selector.chrome37Blacklist, function (a) {
  610. if (/(?:\.webm)/i.test(a.href)) {
  611. a.dataset.ytmscroll = false;
  612. }
  613. });
  614. }
  615.  
  616. links.forEach(YTMA.create);
  617. }
  618.  
  619. return links.length;
  620. }
  621. };
  622.  
  623. /**
  624. * User Preferences
  625. * size: Small (240p), Medium (360p), Large (480p), XL (720p)
  626. * ratio: 1 4:3, 2 16:9
  627. * quality: 240, 360, 480, 720, 1080
  628. * focus: 0/1; Will attempt to set the window's focus near the video
  629. * autoShow: 0/1; Will automatically display HTML5 videos, which currently lack descriptions and thumbnails
  630. * desc: (Descriptions) 0 None; 1 Yes on scroll; 2 Yes all at once
  631. * yt_nocookie: 0/1; Will disable/enable youtube-nocookie.com
  632. * yt_volume: positive number; youtube volume
  633. * yt_annotation: 0/1; youtube annotations
  634. */
  635. YTMA.user = {
  636. KEY: 'ytmasetts',
  637. $form: null,
  638. init: function () {
  639. this.load();
  640.  
  641. if (strg.on) {
  642. this.fn.makeForm();
  643. this.mark();
  644. }
  645. },
  646. valid: {
  647. focus: [0, 1],
  648. desc: [0, 1, 2],
  649. ratio: [1, 2],
  650. size: [240, 360, 480, 720],
  651. quality: [240, 360, 480, 720, 1080],
  652. autoShow: [0, 1],
  653. yt_nocookie: [0, 1],
  654. yt_annotation: [0, 1], // hide | show
  655. yt_volume: 100 // todo? (function () { var a = new Array(101); for (i = 0; i <= 100; i++) { a[i] = i; } return a; }())
  656. },
  657. mapping: { // map values to some other values used by an external API, for example
  658. yt_annotation: [3, 1] // 3 = hide | 1 = show
  659. },
  660. validate: function (property, n) {
  661. n = +n;
  662.  
  663. if (property === 'yt_volume') {
  664. return n >= 0 && n <= 100 ? (+n) : YTMA.user.defaults()[property];
  665. }
  666. return YTMA.user.valid[property].indexOf(n) > -1 ? n : YTMA.user.defaults()[property];
  667. },
  668. defaults: function () {
  669. return {
  670. focus : 0,
  671. desc : 1,
  672. ratio : 2,
  673. size : 360,
  674. quality : 720,
  675. autoShow : 1,
  676. yt_nocookie : 0,
  677. yt_annotation : 1,
  678. yt_volume : 100
  679. };
  680. },
  681. load: function () {
  682. var s = strg.grab(YTMA.user.KEY, {});
  683.  
  684. YTMA.user.preferences = {
  685. size : YTMA.user.validate('size', s.size),
  686. ratio : YTMA.user.validate('ratio', s.ratio),
  687. desc : YTMA.user.validate('desc', s.desc),
  688. focus : YTMA.user.validate('focus', s.focus),
  689. quality : YTMA.user.validate('quality', s.quality),
  690. autoShow : YTMA.user.validate('autoShow', s.autoShow),
  691. yt_nocookie : YTMA.user.validate('yt_nocookie', s.yt_nocookie),
  692. yt_annotation : YTMA.user.validate('yt_annotation', s.yt_annotation),
  693. yt_volume : YTMA.user.validate('yt_volume', s.yt_volume)
  694. };
  695.  
  696. $$.o(YTMA.user.mapping, function (key, val) {
  697. if (!val.hasOwnProperty('indexOf')) {
  698. YTMA.user.preferences[key] = val[YTMA.user.valid[key].indexOf(YTMA.user.preferences[key])];
  699. }
  700. });
  701.  
  702. console.log('loaded: ', YTMA.user.preferences);
  703. },
  704. mark: function () {
  705. var a = {};
  706. a.ytma__focus = !!YTMA.user.preferences.focus;
  707. a.ytma__autoShow = !!YTMA.user.preferences.autoShow;
  708. a.ytma__yt_nocookie = !!YTMA.user.preferences.yt_nocookie;
  709. a.ytma__yt_annotation = !!YTMA.user.preferences.yt_annotation;
  710. a.ytma__yt_volume = YTMA.user.preferences.yt_volume;
  711. a['ytma__ratio' + YTMA.user.preferences.ratio] = true;
  712. a['ytma__size' + YTMA.user.preferences.size] = true;
  713. a['ytma__desc' + YTMA.user.preferences.desc] = true;
  714. a['ytma__quality' + YTMA.user.preferences.quality] = !!YTMA.user.preferences.quality;
  715.  
  716. // console.log('marking', a);
  717. $$.o(a, function (id, val) {
  718. try {
  719. var el = document.getElementById(id);
  720. el.checked = val;
  721. el.value = val;
  722. } catch (e) {
  723. // console.log(id, e);
  724. }
  725. });
  726. },
  727. events: {
  728. save: function (e) {
  729. var o = {};
  730.  
  731. if (e && (/(?:INPUT|LABEL)/i).test(e.target.nodeName)) {
  732. // console.log(YTMA.user.$form.querySelectorAll('[data-key]'));
  733. // [data-key]:checked
  734. $$.ary(YTMA.user.$form.querySelectorAll('[data-key]')).forEach(function (e) {
  735. var key;
  736. key = e.dataset.key;
  737.  
  738. if (e.type === 'checkbox') {
  739. o[key] = +e.checked;
  740. } else if (e.type === 'radio') {
  741. if (e.checked) {
  742. if (e.hasAttribute('data-num')) {
  743. o[key] = +e.dataset.num;
  744. }
  745. }
  746. } else {
  747. o[key] = +e.value;
  748. }
  749. });
  750.  
  751. if (strg.save(YTMA.user.KEY, o)) {
  752. YTMA.user.load();
  753. } else {
  754. YTMA.user.error.classList.remove('ytm_none');
  755. }
  756. }
  757.  
  758. },
  759. reset: function () {
  760. YTMA.user.preferences = YTMA.user.defaults();
  761. YTMA.user.mark();
  762. strg.wipe(YTMA.user.KEY);
  763. YTMA.user.error.classList.add('ytm_none');
  764. },
  765. clear: function () {
  766. try {
  767. localStorage.removeItem(YTMA.external.version);
  768. YTMA.user.events.reset();
  769. console.log('removed all YTMA cache');
  770. } catch (e) {
  771. console.error(e);
  772. }
  773. },
  774. formToggle: function (e) {
  775. if (!e || (e && e.target && !(/(?:INPUT|LABEL)/i).test(e.target.nodeName))) {
  776. YTMA.user.$form.classList.toggle('ytm_none');
  777. }
  778. },
  779. formToggleKeyboard: function (e) {
  780. // press CTRL+SHIFT+Y (META+SHIFT+Y) to display settings form
  781. if ((e.ctrlKey || e.metaKey) && e.shiftKey && String.fromCharCode(e.which).toLowerCase() === 'y') {
  782. e.preventDefault();
  783. YTMA.user.events.formToggle();
  784. }
  785. }
  786. },
  787. fn: {
  788. $scroller: null,
  789. $once: false,
  790. loadPreferences: function () {
  791. YTMA.user.fn.onScrollLoadDescriptions(YTMA.user.preferences.desc === 1);
  792.  
  793. this.loadPreferencesOnce();
  794. },
  795. loadPreferencesOnce: function () {
  796. if (this.$once) { return; }
  797.  
  798. this.$once = true;
  799.  
  800. if (YTMA.user.preferences.autoShow === 1) {
  801. YTMA.user.fn.onScrollViewMedia();
  802. }
  803. },
  804. showMedia: function () {
  805. console.log('showMedia');
  806. return new YTMA.Scroll('a.ytm_scroll:not([data-ytmscroll="false"])', function (link) {
  807. if (YTMA.Scroll.visibleAll(link, 50)) {
  808. $$.s('var[data-ytmsid="' + link.dataset.ytmsid + '"]:not([data-ytmscroll="false"])', function (trigger) {
  809. var ui = YTMA.UI.createFromTrigger(trigger);
  810. ui.showOnScroll(link);
  811. });
  812. }
  813. });
  814. },
  815. toggleMedia: function () {
  816. return new YTMA.Scroll('div.ytm_panel_switcher', function (div) {
  817. var v = div.querySelector('video'),
  818. paused = v && (v.paused || v.ended),
  819. ui = YTMA.set[div.dataset.ytmuid].getUI();
  820.  
  821. if (paused && !YTMA.Scroll.visibleAll(div, 0)) {
  822. return ui.play.switchStandby();
  823. }
  824.  
  825. if (ui.play.isStandby() && YTMA.Scroll.visibleAll(div, 200)) {
  826. return ui.play.switchOn();
  827. }
  828.  
  829. // todo ascertain embedded player properties
  830. // f = div.querySelector('iframe, object');
  831. // if (f && !YTMA.Scroll.visibleAll(div, 200)) {
  832. // y.hidePlayer();
  833. // }
  834. });
  835. },
  836. onScrollViewMedia: function () {
  837. this.showMedia();
  838. this.toggleMedia();
  839. },
  840. onScrollLoadDescriptions: function (ajax) {
  841. if (YTMA.user.fn.$scroller) { YTMA.user.fn.$scroller.stop(); }
  842.  
  843. YTMA.user.fn.$scroller = new YTMA.Scroll('span.ytm_manual > a.ytm_title:not(.ytm_error)', function (a) {
  844. if (YTMA.Scroll.visibleAll(a, 200)) {
  845. if (ajax) {
  846. YTMA.ajax.loadFromDataset(a.dataset);
  847. } else {
  848. YTMA.ajax.loadFromCacheDataset(a.dataset);
  849. }
  850. // console.log('doc', document.querySelectorAll(YTMA.user.fn.$scroller.selector).length, a.dataset.id);
  851. }
  852.  
  853. if (document.querySelectorAll(YTMA.user.fn.$scroller.selector).length === 0) {
  854. YTMA.user.fn.$scroller.stop();
  855. }
  856. });
  857. },
  858. makeForm: function () {
  859. var e, f = [
  860. '<div id="ytm_settings" class="ytm_sans ytm_block ytm_normalize"><form action="" title="Double click to close"><div id="ytm_settingst">ytma! Site Settings</div><div class="ytm_field_container">',
  861. '<fieldset><legend title="Load descriptions from the content sever.">Load Descriptions</legend><p><span><input id="ytma__desc0" type="radio" data-num="0" name="ytma__desc" data-key="desc"><label for="ytma__desc0" title="Load descriptions on demand">Manually</label></span><span><input id="ytma__desc1" type="radio" data-num="1" name="ytma__desc" data-key="desc"><label for="ytma__desc1" title="Load descriptions as they become visible on the screen.">Automatically, on scrolling</label></span></p></fieldset>',
  862. '<fieldset><legend>HTML5 Players</legend><p><input name="ytma__autoShow" data-key="autoShow" id="ytma__autoShow" type="checkbox"><label for="ytma__autoShow">Automatically show WebM, MP4 and Soundcloud players</label></p></fieldset>',
  863. '<fieldset><legend>Player Size</legend><p><span><input type="radio" name="ytma__size" data-key="size" data-num="240" id="ytma__size240" /><label for="ytma__size240">S <small>240p</small></label></span><span><input name="ytma__size" data-key="size" type="radio" id="ytma__size360" data-num="360" /><label for="ytma__size360">M <small>360p</small></label></span><span><input type="radio" name="ytma__size" data-key="size" data-num="480" id="ytma__size480" /><label for="ytma__size480">L <small>480p</small></label></span><span><input type="radio" name="ytma__size" data-key="size" data-num="720" id="ytma__size720" /><label for="ytma__size720">X <small>720p</small></label></span></p></fieldset>',
  864. '<fieldset><legend>Quality</legend><p><span><input name="ytma__quality" data-key="quality" data-num="240" id="ytma__quality240" type="radio"><label for="ytma__quality240">240p</label></span><span><input name="ytma__quality" data-key="quality" id="ytma__quality360" data-num="360" type="radio"><label for="ytma__quality360">360p</label></span><span><input name="ytma__quality" data-key="quality" data-num="480" id="ytma__quality480" type="radio"><label for="ytma__quality480">480p</label></span><span><input name="ytma__quality" data-key="quality" data-num="720" id="ytma__quality720" type="radio"><label for="ytma__quality720">720p</label></span><span><input name="ytma__quality" data-key="quality" data-num="1080" id="ytma__quality1080" type="radio"><label for="ytma__quality1080">1080p</label></span></p></fieldset>',
  865. '<fieldset><legend>Aspect Ratio</legend><p><span><input name="ytma__ratio" data-key="ratio" type="radio" id="ytma__ratio2" data-num="2" /><label for="ytma__ratio2">16:9</label></span><span><input type="radio" name="ytma__ratio" data-key="ratio" data-num="1" id="ytma__ratio1" /><label for="ytma__ratio1">4:3</label></span></p></fieldset>',
  866. '<fieldset><legend>YouTube</legend>',
  867. '<p><input name="ytma__yt_annotation" data-key="yt_annotation" type="checkbox" id="ytma__yt_annotation" /><label for="ytma__yt_annotation">Enable video annotations</label></p>',
  868. '<p><input name="ytma__yt_nocookie" data-key="yt_nocookie" type="checkbox" id="ytma__yt_nocookie" /><label for="ytma__yt_nocookie">Use https://youtube-nocookie.com to load videos</label></p>',
  869. '<p><input name="ytma__yt_volume" data-key="yt_volume" type="number" style="width:4em" min=0 max=100 id="ytma__yt_volume" /><label for="ytma__yt_volume">Video volume (Experimental)</label></p>',
  870. '</fieldset>',
  871. '<fieldset><legend>Window Focus</legend><p><input name="ytma__focus" data-key="focus" type="checkbox" id="ytma__focus" value="focus" /><label for="ytma__focus">After clicking the thumbnail, set the video at the top of the window.</label></p></fieldset>',
  872. '</div><p><small id="ytm_settings_error" class="ytm_error ytm_none ytm_title">Error! Your settings could not be saved.</small></p><p id="ytm_opts"><button type="button" id="ytmaclose">Close</button> <button type="button" id="ytmareset">Reset</button> <button type="button" id="ytmaclear" title="Remove all video descriptions that have been cached">Reset & Remove Cache</button></p></form></div>'
  873. ].join('');
  874.  
  875. YTMA.user.$form = $$.e('div', {className: 'ytm_fix_center ytm_none ytm_box', innerHTML: f}, document.body);
  876. YTMA.user.error = document.getElementById('ytm_settings_error');
  877.  
  878. e = YTMA.Scroll.debounce(YTMA.user.events.save, 500);
  879. YTMA.user.$form.addEventListener('submit', function (evt) { evt.preventDefault(); }, false);
  880. YTMA.user.$form.addEventListener('keyup', e, false);
  881. YTMA.user.$form.addEventListener('click', e, false);
  882.  
  883. YTMA.user.$form.addEventListener('dblclick', YTMA.user.events.formToggle, false);
  884. document.getElementById('ytmaclose').addEventListener('click', YTMA.user.events.formToggle, false);
  885. document.getElementById('ytmareset').addEventListener('click', YTMA.user.events.reset, false);
  886. document.getElementById('ytmaclear').addEventListener('click', YTMA.user.events.clear, false);
  887. document.body.addEventListener('keydown', YTMA.user.events.formToggleKeyboard, false);
  888. }
  889. }
  890. };
  891.  
  892. YTMA.css = function () {
  893. var playerCss = YTMA.Player.css.generator(),
  894. loadingIcon = 'data:image/gif;base64,R0lGODlhDgAKAJEAAP///+BKV////wAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgACACwAAAAADgAKAAACHFSOeQYI71p6MtAJz41162yBH+do5Ih1kKG0QgEAIfkEBQoAAgAsAAABAA0ACAAAAhSUYGEoerkgdIzKGlu2ET/9ceJmFAAh+QQFCgACACwAAAEADQAIAAACFJRhcbmiglx78SXKYK6za+NxHyYVACH5BAUKAAIALAAAAQANAAgAAAIWVCSAl+hqEGRTLhtbdvTqnlUf9nhTAQAh+QQFCgACACwAAAEADQAIAAACFZRiYCh6uaCRzNXYsKVT+5eBW3gJBQAh+QQJCgACACwAAAAADgAKAAACGpSPaWGwfZhwQtIK8VTUvuxpm9Yp4XlmpiIUADs=';
  895.  
  896. // Roboto font-o
  897. // $$.e('link', {
  898. // rel: 'stylesheet',
  899. // $type: 'text/css',
  900. // href: 'https://fonts.googleapis.com/css?family=Roboto'
  901. // }, document.body);
  902.  
  903. // console.log(playerCss);
  904. $$.css(playerCss);
  905.  
  906. // images
  907. // todo update(site, size, padding)
  908. $$.css([
  909. '.ytm_loading{background:url(', loadingIcon, ') 0 3px no-repeat;}',
  910. '.ytm_link{background:url(', YTMA.DB.sites.youtube.favicon, ') 0 center no-repeat !important;margin-left:4px;padding-left:20px!important;}',
  911. '.ytm_link.ytm_link_vimeo{background-image:url(', YTMA.DB.sites.vimeo.favicon, ') !important;background-size:12px 12px !important;padding-left:18px!important}',
  912. '.ytm_link.ytm_link_vine{background-image:url(', YTMA.DB.sites.vine.favicon, ') !important;background-size:10px 10px!important;padding-left:16px!important}',
  913. '.ytm_link.ytm_link_soundcloud{background-image:url(', YTMA.DB.sites.soundcloud.favicon, ')!important;padding-left:17px!important}',
  914. '.ytm_link.ytm_link_html5{background-image:url(', YTMA.DB.sites.html5.favicon, ') !important;padding-left:16px!important}',
  915. '.ytm_link.ytm_link_gfycat{background-image:url(', YTMA.DB.sites.gfycat.favicon, ') !important;background-size:12px 12px !important;padding-left:16px!important;}',
  916. '.ytm_link.ytm_link_imgur{background-image:url(', YTMA.DB.sites.imgur.favicon, ') !important;background-size:12px 12px !important;padding-left:16px!important}',
  917. '.ytm_link.ytm_link_streamable{background-image:url(', YTMA.DB.sites.streamable.favicon, ') !important; background-size: 12px 12px !important;padding-left: 14px !important;}'
  918. ].join(''));
  919.  
  920. // todo
  921. // if (window.NO_YTMA_CSS) { return; }
  922.  
  923. $$.css('.ytm_none,.ytm_link br{display:none!important}.ytm_box{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.ytm_block{display:block;position:relative;clear:both;text-align:left;border:0;margin:0;padding:0;overflow:hidden}.ytm_normalize{font-weight:400!important;font-style:normal!important;line-height:1.2!important}.ytm_sans{font-family:Arial,Helvetica,sans-serif!important}.ytm_spacer{overflow:auto;margin:0 0 6px;padding:4px}.ytm_spacer.ytm_site_slim{display:inline}.ytm_clear:after{content:"";display:table;clear:both}.ytm_center{text-align:center}.ytm_link b,.ytm_link strong{font-weight:400!important}.ytm_link u{text-decoration:none!important}.ytm_link i,.ytm_link em{font-style:normal!important}.ytm_trigger{width:118px;height:66px;background-color:#262626!important;cursor:pointer;background-position:-1px -12px;float:left;box-shadow:2px 2px rgba(0,0,0,.3);background-size:auto 90px!important;color:#fff;text-shadow:#333 0 0 2px;font-size:13px}.ytm_trigger:hover{box-shadow:2px 2px #9eae9e;opacity:.95}.ytm_trigger var{z-index:2;height:100%;width:100%;position:absolute;left:0;top:0;text-align:right}.ytm_label{display:block;padding:3px 6px;line-height:1.2;font-style:normal}.ytm_init{height:22px;background:rgba(11,11,11,.62);padding:4px 25px 6px 6px}.ytm_site_vine .ytm_trigger{background-color:#90ee90!important;background-size:120px auto!important}.ytm_site_slim .ytm_trigger{background:#e34c26!important;height:auto;box-shadow:0 0 2px #ffdb9d inset,2px 2px rgba(0,0,0,.3);margin:0 3px 0 0;width:auto;transition:all .3s ease-in-out 0s}.ytm_site_slim .ytm_trigger:hover{opacity:.8}.ytm_site_slim .ytm_label{text-shadow:0 0 1px #f06529}.ytm_site_slim .ytm_init{background:transparent}.ytm_bd{float:left;max-width:500px;margin:2px 10px;font-size:90%}.ytm_title{font-weight:700}.ytm_error{color:#cc2f24;font-style:italic}.ytm_loading{font-style:italic;padding:1px 1.5em}.ytm_descr{word-wrap:break-word;max-height:48px;overflow:auto;padding-right:20px}.ytm_descr[data-full]{cursor:pointer}.ytm_descr_open{resize:both;white-space:pre-line}.ytm_descr_open[style]{max-height:none}.ytm_projector{margin-bottom:4px}ul.ytm_control{overflow:hidden;margin:0!important;padding:3px 0 1px;list-style-position:outside!important}.ytm_control li{display:inline;margin:0!important;padding:0!important}.ytm_control li>ul{display:inline-block;margin:0;padding:0 1px 0 0}.ytm_control li ul li{-webkit-user-select:none;-moz-user-select:none;-o-user-select:none;user-select:none;list-style-type:none;cursor:pointer;float:left;color:#858585;border:1px solid #1d1d1d;border-bottom:1px solid #000;border-top:1px solid #292929;box-shadow:0 0 1px #555;height:14px;font-size:12px!important;line-height:12px!important;background:#222;background:linear-gradient(#2d2c2c,#222);margin:0!important;padding:5px 9px 3px!important}.ytm_control li ul li:first-child{border-radius:2px 0 0 2px}.ytm_control li ul li:last-child{border-left:0!important;border-radius:0 2px 2px 0;margin:0 2px 0 0!important}.ytm_control li ul li:first-child:last-child,.ytm_li_setting{border-radius:2px}.ytm_control li ul li:hover{color:#ccc;text-shadow:1px 1px 0 #333;background:#181818}.ytm_control li ul li[id]{color:#ddd;text-shadow:0 0 2px #444}.ytm_panel_size{background:#000;max-width:100%;}.ytm_panel_switcher[data-standby="true"]{background:#111}.ytm_panel_switcher[data-standby="true"]:after{cursor:cell;color:#0e0e0e;content:"ytma!";display:block;font-size:85px;font-style:italic;font-weight:700;left:50%;position:absolute;text-shadow:2px 1px #181818,-1px -1px #0a0a0a;top:50%;transform:translate(-50%,-50%)}.ytm_site_soundcloud .ytm_panel_size.ytm_soundcloud-playlist{height:334px!important}.ytm_fix_center{background:rgba(51,51,51,.41);height:100%;left:0;position:fixed;top:0;width:100%;z-index:99998}#ytm_settings{z-index:99999;max-width:500px;max-height:85%;overflow:auto;background:#fbfbfb;border:1px solid #bbb;color:#444;box-shadow:0 0 5px rgba(0,0,0,.2),0 0 3px rgba(239,239,239,.1) inset;margin:4% auto;padding:4px 8px 0}#ytm_settings p{margin:5px 0;padding:0}#ytm_settings fieldset{vertical-align:top;border-radius:3px;border:1px solid #ccc;margin:0 0 5px}#ytm_settings fieldset span{display:inline-block;min-width:5em}#ytm_settings input{vertical-align:baseline!important;margin:3px 5px!important}#ytm_settingst{font-size:110%;border-bottom:1px solid #d00;margin:3px 0 9px;padding:0 3px 3px}#ytm_settings label{cursor:pointer}#ytm_settings small{font-size:90%}#ytm_opts button{cursor:pointer;margin:10px 5px 8px 2px;padding:3px;border:1px solid #adadad;border-radius:2px;background:#eee;font-size:90%}#ytm_opts button:hover{background:#ddd}');
  924. // $$.css('.ytm_site_youtube .ytm_sans { font-family: \'Roboto\'; }');
  925. };
  926.  
  927. YTMA.ajax = {
  928. load: function (site, id, uri) {
  929. console.log('YTMA.ajax.load:', site, id, uri);
  930. uri = YTMA.DB.sites[site].ajax.replace('%key', id).replace('%uri', uri);
  931.  
  932. if (YTMA.DB.sites[site].ajaxExtension) { return this.gmxhr(uri, site, id); }
  933.  
  934. console.log('ajax.site?', YTMA.DB.sites[site].ajax.replace('%key', id).replace('%uri', uri));
  935. if (YTMA.DB.sites[site].ajax) {
  936. console.log('preping uri');
  937. return this.xhr(uri, site, id);
  938. }
  939.  
  940. return null;
  941. },
  942. loadFromDataset: function (dataset) {
  943. if (!this.loadFromCacheDataset(dataset)) {
  944. return this.load(dataset.ytmsite, dataset.ytmid, dataset.ytmuri);
  945. }
  946. },
  947. loadFromCacheDataset: function (dataset) {
  948. var cache = YTMA.external.dataFromStorage(dataset.ytmsite, dataset.ytmid);
  949.  
  950. console.log('YTMA.ajax.cache:', dataset.ytmsite, dataset.ytmid);
  951. console.log('@cache:', cache);
  952.  
  953. if (cache) { YTMA.external.populate(cache); }
  954.  
  955. return cache;
  956. },
  957. gmxhr: function (uri, site, id) {
  958. try {
  959. // console.log('gmxhr starting!');
  960. GM_xmlhttpRequest({
  961. method: 'GET',
  962. url: uri,
  963. onload: function (response) {
  964. // console.log(response);
  965. YTMA.external.parse(response.responseText, site, id);
  966. },
  967. onerror: function () {
  968. console.log('GM Cannot XHR');
  969. YTMA.ajax.failure.call({id: id});
  970. }
  971. });
  972.  
  973. YTMA.ajax.preProcess(id);
  974.  
  975. } catch (e) {
  976. if (YTMA.DB.extension) {
  977. console.log('attempting cs xhr');
  978. this.xhr(uri, site, id);
  979. } else {
  980. console.log('No applicable CORS request available.');
  981. this.failure.call({id: id});
  982. }
  983. }
  984. },
  985. xhr: function (uri, site, id) {
  986. var x = new XMLHttpRequest();
  987. console.log('xhr', uri, id, site);
  988.  
  989. YTMA.ajax.preProcess(id);
  990.  
  991. x.onreadystatechange = function () {
  992. if (this.readyState === this.DONE) {
  993. // console.log(this.readyState, this.status);
  994. if (this.status === 200) {
  995. YTMA.external.parse(this.responseText, site, id);
  996. } else if (this.status === 403) {
  997. YTMA.external.populate({site: site, id: id, title: 'Error 403', desc: ''});
  998. YTMA.external.save({site: site, id: id, title: 'Error 403', desc: ''});
  999. } else { // if (this.status >= 400 || this.status === 0) {
  1000. YTMA.ajax.failure.call({id: id});
  1001. }
  1002. }
  1003. };
  1004.  
  1005. try {
  1006. console.log('sending');
  1007. x.open('get', uri, true);
  1008. x.send();
  1009. } catch (e) {
  1010. console.error('Cannot send xhr', uri);
  1011. YTMA.ajax.failure.call({id: id});
  1012. console.error(e);
  1013. }
  1014. },
  1015. failure: function () {
  1016. $$.s('.ytm_bd._' + YTMA.escapeId(this.id), function (el) {
  1017. var a = el.querySelector('a');
  1018. a.dataset.tries = a.dataset.tries ? parseFloat(a.dataset.tries) + 1 : 1;
  1019. a.textContent = 'Error, unable to load data. ' + (a.dataset.tries > 0 ? ('(' + a.dataset.tries + ')') : '[Retry]');
  1020. a.className = 'ytm_error ytm_title';
  1021. });
  1022. },
  1023. preProcess: function (id) {
  1024. $$.s('.ytm_manual._' + YTMA.escapeId(id) + ' a', function (el) {
  1025. el.classList.add('ytm_loading');
  1026. el.textContent = 'Loading data . . .';
  1027. el.title = 'Retry loading data.';
  1028. });
  1029. }
  1030. };
  1031.  
  1032. /** E X T E R N A L Apparatus
  1033. * Data from external sites
  1034. */
  1035. YTMA.external = {
  1036. version: 'ytma.4.1.dat',
  1037. parse: function (response, site, id) {
  1038. if (this.parsers[site]) {
  1039. response = YTMA.DB.sites[site].rawResponse ? response : JSON.parse(response);
  1040. this.populate(this.helper.cutDescription(this.parsers[site](response, id)));
  1041. }
  1042. },
  1043. parsers: {
  1044. soundcloud: function (j, id) {
  1045. return {
  1046. site: 'soundcloud',
  1047. id: id, //unescape(j.html).match(/tracks\/(\d+)/)[1],
  1048. title: j.title,
  1049. desc: j.description,
  1050. th: removeSearch(j.thumbnail_url)
  1051. };
  1052. },
  1053. vimeo: function (j) {
  1054. j = j[0];
  1055. return {
  1056. site: 'vimeo',
  1057. id: j.id,
  1058. title: j.title + ' ' + YTMA.external.time.fromSeconds(j.duration),
  1059. desc: j.description.replace(/<br.?.?>/g, ''),
  1060. th: decodeURI(j.thumbnail_medium)
  1061. };
  1062. },
  1063. youtube: function (j, id) {
  1064. if (j.pageInfo.totalResults < 1) {
  1065. return { id: id, error: true };
  1066. }
  1067.  
  1068. j = j.items[0];
  1069. var o = {
  1070. site: 'youtube',
  1071. id: id,
  1072. title: j.snippet.title + ' ' + YTMA.external.time.fromIso8601(j.contentDetails.duration),
  1073. desc: j.snippet.description
  1074. // aspectRatio: j.contentDetails.aspectRatio
  1075. };
  1076.  
  1077. return o;
  1078. },
  1079. vine: function (j, id) {
  1080. return {
  1081. site: 'vine',
  1082. id: id,
  1083. title: j.title,
  1084. th: removeSearch(j.thumbnail_url)
  1085. };
  1086. },
  1087. gfycat: function (j, id) {
  1088. j = j.gfyItem;
  1089. if (j) {
  1090. return {
  1091. site: 'gfycat',
  1092. id: id || j.gfyName,
  1093. title: j.title || j.gfyName
  1094. };
  1095. }
  1096. },
  1097. streamable: function (j, id) {
  1098. return {
  1099. site: 'streamable',
  1100. id: id,
  1101. title: j.title || 'Untitled'
  1102. };
  1103. }
  1104. },
  1105. set: function (data) {
  1106. if (!this.db[data.site]) {
  1107. this.db[data.site] = {};
  1108. }
  1109. this.db[data.site][data.id] = data;
  1110. return this.save();
  1111. },
  1112. unset: function (data) {
  1113. // console.log('unset', data.id);
  1114. if (data.site) {
  1115. delete this.db[data.site][data.id];
  1116. return this.save();
  1117. }
  1118. },
  1119. limitDB: function (max, db) {
  1120. // limits an object's items by half of the max
  1121. // removes the older items at the start of the object
  1122. var keys = Object.keys(db),
  1123. half = Math.floor(max / 2),
  1124. start,
  1125. ndb,
  1126. i;
  1127.  
  1128. if (keys.length > max) {
  1129. ndb = {};
  1130. start = keys.length - half;
  1131.  
  1132. for (i = start; i < keys.length; i++) {
  1133. ndb[keys[i]] = db[keys[i]];
  1134. }
  1135. }
  1136.  
  1137. return ndb || db;
  1138. },
  1139. save: function () {
  1140. this.db = this.limitDB(1000, this.db);
  1141. return strg.save(this.version, this.db);
  1142. },
  1143. helper: {
  1144. cutDescription: function (data) {
  1145. if (data.desc && data.desc.length > 140) {
  1146. data.full = data.desc;
  1147. data.desc = data.desc.substr(0, 130) + ' . . .';
  1148. }
  1149. return data;
  1150. },
  1151. thumbnail: function (data) {
  1152. $$.s('[data-ytmid="%id"].ytm_trigger'.replace('%id', data.id), function (el) {
  1153. el.setAttribute('style', 'background: url(' + data.th + ')');
  1154. });
  1155. },
  1156. titleToggle: function () {
  1157. this.classList.toggle('ytm_descr_open');
  1158. this.textContent = this.textContent.length < 140 ? this.dataset.full : this.dataset.full.substr(0, 130) + ' . . .';
  1159. this.removeAttribute('style');
  1160. }
  1161. },
  1162. time: {
  1163. keepMinutesAndSeconds: function (v, i) {
  1164. return i > 1 || v > 0;
  1165. },
  1166. leadingZero: function (v, i) {
  1167. return i > 0 ? ('00' + v).slice(-2) : v;
  1168. },
  1169. fromArray: function (a) { // [days, hours, mins, secs]
  1170. var b, p = '';
  1171.  
  1172. try {
  1173. // Remove empty values, but keep lower indexes (m:s); a[i] > 0 || i > 1
  1174. // Add leading 0's, ignoring the first index
  1175. // a.slice(0, 1).concat(a.slice(1))
  1176. b = a.filter(this.keepMinutesAndSeconds).map(this.leadingZero);
  1177. p = '(' + b.join(':') + ')';
  1178. } catch (e) {
  1179. console.error('Could not parse this time.');
  1180. }
  1181.  
  1182. console.log({a: a, b: b, p: p });
  1183. return p;
  1184. },
  1185. fromIso8601: function (iso8601) { // eg PT3M, T29S
  1186. var a,
  1187. parseDigits = function (reg) {
  1188. if (reg.test(iso8601)) {
  1189. return RegExp.lastParen;
  1190. }
  1191. return 0;
  1192. };
  1193.  
  1194. // P#DT#H#M#S || PT#H#M#S
  1195. a = [/(\d+)D/, /(\d+)H/, /(\d+)M/, /(\d+)S/].map(parseDigits);
  1196.  
  1197. return this.fromArray(a);
  1198. },
  1199. fromSeconds: function (seconds) {
  1200. var a = [
  1201. Math.floor(seconds / 86400) % 24,
  1202. Math.floor(seconds / 3600) % 60,
  1203. Math.floor(seconds / 60) % 60,
  1204. seconds % 60
  1205. ];
  1206. return this.fromArray(a);
  1207. }
  1208. },
  1209. validate: function (data) {
  1210. if (!data || !data.id || data.error) {
  1211. return YTMA.ajax.failure.call(data);
  1212. }
  1213.  
  1214. // todo? empty titles and descriptions should be okay
  1215. // if (data.id && !data.title && !data.desc) {
  1216. // this.unset(data.id);
  1217. // return YTMA.ajax.failure.call(data);
  1218. // }
  1219.  
  1220. return true;
  1221. },
  1222. populate: function (data, ignoreValidation) {
  1223. if (!ignoreValidation && !this.validate(data)) { return; }
  1224.  
  1225. this.set(data);
  1226.  
  1227. if (data.th) { this.helper.thumbnail(data); }
  1228.  
  1229. $$.s('.ytm_bd._' + YTMA.escapeId(data.id), function (el) {
  1230. var q;
  1231. el.innerHTML = '<span class="ytm_title">' + data.title + '</span>';
  1232. if (data.desc) {
  1233. q = $$.e('q', { className: 'ytm_descr ytm_block', textContent: data.desc }, el);
  1234. if (data.full) {
  1235. q.dataset.full = data.full;
  1236. q.title = 'Click to toggle the length of the description.';
  1237. q.addEventListener('dblclick', YTMA.external.helper.titleToggle, false);
  1238. }
  1239. }
  1240. });
  1241. },
  1242. dataFromStorage: function (site, id) {
  1243. if (this.db && this.db[site]) {
  1244. return this.db[site][id];
  1245. }
  1246. },
  1247. events: {
  1248. manualLoad: function (e) {
  1249. // console.log(this);
  1250. e.preventDefault();
  1251. YTMA.ajax.loadFromDataset(e.target.dataset);
  1252. }
  1253. }
  1254. };
  1255. YTMA.external.db = strg.grab(YTMA.external.version, {});
  1256.  
  1257. /** Database */
  1258. YTMA.DB = {
  1259. postInit: function () {
  1260. if (YTMA.user.preferences.yt_nocookie) {
  1261. YTMA.DB.sites.youtube.home = 'https://www.youtube-nocookie.com/';
  1262. YTMA.DB.sites.youtube.embed = 'https://www.youtube-nocookie.com/embed/%key';
  1263. }
  1264. },
  1265. extension: window.chrome && window.chrome.extension,
  1266. browser: {
  1267. pod: YTMA.reg.ios.test(navigator.userAgent),
  1268. ie: !!document.documentMode, // IE, basically | window.navigator.cpuClass
  1269. safari: Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0
  1270. },
  1271. views: {
  1272. getAllSiteRegExps: function () {
  1273. var regs = [];
  1274.  
  1275. $$.o(YTMA.DB.sites, function (k, site) {
  1276. if (site.reg) {
  1277. regs.push(site.reg);
  1278. }
  1279. });
  1280.  
  1281. return new RegExp('\\b' + regs.join('|'));
  1282. },
  1283. getAllSiteSelectors: function () {
  1284. var sels = [];
  1285.  
  1286. $$.o(YTMA.DB.sites, function (k, site) {
  1287. if (site.selector) {
  1288. sels.push(site.selector);
  1289. }
  1290. });
  1291.  
  1292. return sels.join();
  1293. },
  1294. getPlayerSources: function (siteName) {
  1295. return YTMA.DB.sources[siteName] || YTMA.DB.sources.iframe;
  1296. },
  1297. getToolbar: function (site) {
  1298. var bar = YTMA.DB.customToolbars[site] || {};
  1299.  
  1300. return {
  1301. ratio: bar.ratio === undefined ? true : bar.ratio,
  1302. size: bar.size === undefined ? true : bar.size
  1303. };
  1304. },
  1305. getPlayerDimmensions: function (ratio, size) {
  1306. return 'ytm_panel ytm_block ytm_panel-' + YTMA.DB.playerSize.ratios[ratio]
  1307. + ' ytm_panel-' + YTMA.DB.playerSize.sizes[size];
  1308. },
  1309. getPlayerQuality: function (quality) {
  1310. return YTMA.DB.qualities[quality] || YTMA.DB.qualities[360];
  1311. }
  1312. },
  1313. sites: { // supported sites - to add more also make a parser (if api is available) and add an item to sources (if necessary)
  1314. youtube: {
  1315. title: 'ytma!',
  1316. home: 'https://www.youtube.com/',
  1317. embed: 'https://www.youtube.com/embed/%key',
  1318. ajax: 'https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%key' + window.atob('JmtleT1BSXphU3lEVG5INkxzRERyVElYaFZTZWRQQjlyRHo1czBSczQzZnM='),
  1319. thumb: 'url(https://i3.ytimg.com/vi/%key/1.jpg)',
  1320. selector: 'a[href*="youtube."], a[href*="youtu.be/"]',
  1321. favicon: 'https://www.youtube.com/favicon.ico',
  1322. key: 'id',
  1323. reg: '(youtu)',
  1324. matcher: /(?:(?:(?:v\=|#p\/u\/\d*?\/)|(?:v\=|#p\/c\/[a-zA-Z0-9]+\/\d*?\/)|(?:embed\/)|(?:v\/)|(?:\.be\/))([A-Za-z0-9-_]{11}))/i,
  1325. https: true
  1326. },
  1327. vimeo: {
  1328. title: 'vimeo too!',
  1329. home: 'https://vimeo.com/',
  1330. embed: 'https://player.vimeo.com/video/%key?badge=0',
  1331. ajax: 'https://vimeo.com/api/v2/video/%key.json',
  1332. selector: 'a[href*="vimeo.com/"]',
  1333. favicon: 'https://f.vimeocdn.com/images_v6/favicon.ico',
  1334. key: 'id',
  1335. reg: '(vimeo)',
  1336. matcher: /(?:vimeo\.com\/(\d+))/i,
  1337. https: true
  1338. },
  1339. vine: {
  1340. title: 'vine me!',
  1341. home: 'https://vine.co/',
  1342. embed: 'https://vine.co/v/%key/embed/simple?audio=1',
  1343. ajaxExtension: true,
  1344. ajax: 'https://vine.co/oembed.json?url=https%3A%2F%2Fvine.co%2Fv%2F%key',
  1345. selector: 'a[href*="vine.co/"]',
  1346. favicon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABcklEQVQ4jX2SvyvFYRTGP+9NdzAYbpIkysA/ICnKoEwvA4vlUkoyGQzvoOiK4R0YbDLhDhbi9maWIjEqcScWkyRhMNzXcM/3On39OMt5nvOe85wfvSbGyH9mgs8Cq8AUkAMegU1gJVpXqfu3umpbwITiLUBB8HJGuryb4KMJ/izVvUMVnwNjwJPwGYCMkLL4zlT3XoUL0boD4FBNUhO4Fd9ogm9SRVmFn8Una79qgWuV2K3wvcLJdG3iL7TaiUocAI4Fn0vnHDBrgt+L1g2q3NoEl8CH4KHkMVr3CSwI7QeOTPA9WsAk/8AEvw+MSrwrWleuJQVfABZJWbTOZBTfVXg6lbgEHKjQTcL1BFngAWimeuH2aN2LvHUDV1JcjNbl0zdI9l0T2pDsboKvB7Yl/ga4X2+gku+AVqAC9AFzwLik5KN1xT8FRGQEOBL6yfdn2onWTZKyTDoQrSsB60KT4lNSh/1TQETmgQ1ZowQMy41+2BeLRXeRaKuHSAAAAABJRU5ErkJggg==',
  1347. key: 'id',
  1348. reg: '(vine)',
  1349. matcher: /(?:vine\.co\/v\/([A-Za-z0-9-_]{11}))/i
  1350. },
  1351. soundcloud: {
  1352. title: 'sound off!',
  1353. home: 'https://soundcloud.com/',
  1354. embed: 'https://w.soundcloud.com/player/?show_comments=false&url=%key',
  1355. ajax: 'https://soundcloud.com/oembed?format=json&url=%uri',
  1356. favicon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAXZJREFUeNp0UjFOw0AQnD3bCYaEoIgiUIQKikiAKKGgSAkFL0BQ0FEgUfECShpewB+QEE+gokDiBxGRiCCEBBzHd8ucLYGQ4Kw7+9azc7OzJ6oKnFYUxgD+W/DHYNA54GIooifTitYqg1okaPFff6BcTLF5fECYb5IJNJ3kMfUP2dxHPycwcQ0SliARocSGsA46YkKWQW0GS6BDAGlu5Myu84AgSmHKM/DY0NegwwR2+AJrSjBre5BaA1H7GFKpI729QHp9jqiaeTBn5mB7fWirjWB9B9JYBrwEMnop5d0zfD53YO9vYMY8SQcpZGkTZvsIstiivDfoaxfIxt8exYeXCLYOSOwlJZQ6twSpL0DfX4EoZtkCqc7/Njalbxnfdt+oSwhurkBnCWI/1FlgqgKNZ3JnZDSA3FFSpYdQAoF56tKNDmhOYTzJ3OSHXZkflJhYEyawcnH02HpmxZ+DrRJlgmacvrsHRmHln2tRnIiAy5WTLwEGAK4QoBQmtGHkAAAAAElFTkSuQmCC',
  1357. selector: 'a[href*="soundcloud.com/"]',
  1358. key: 'uri',
  1359. reg: '(soundcloud)',
  1360. matcher: /(?:\/\/(?:\bwww|m\.\b)?soundcloud\.com\/(.+?\/.+))/i,
  1361. https: true,
  1362. scroll: true
  1363. },
  1364. gfycat: {
  1365. title: 'gfycat meow!',
  1366. home: 'https://gfycat.com/',
  1367. embed: 'https://gfycat.com/iframe/%key',
  1368. ajax: 'https://gfycat.com/cajax/get/%key',
  1369. thumb: 'url(https://thumbs.gfycat.com/%key-poster.jpg)',
  1370. selector: 'a[href*="gfycat.com/"]',
  1371. favicon: 'https://gfycat.com/favicon.ico',
  1372. key: 'id',
  1373. reg: '(gfycat)',
  1374. matcher: /(?:gfycat\.com\/(?:(\b(?:[A-Z][a-z]*){3,}\b)))/i,
  1375. https: true,
  1376. scroll: true,
  1377. videoTag: true
  1378. },
  1379. streamable: {
  1380. title: 'streamable!',
  1381. home: 'https://streamable.com/',
  1382. embed: 'https://streamable.com/e/%key',
  1383. ajax: 'https://api.streamable.com/oembed.json?url=%uri',
  1384. thumb: 'url(https://cdn.streamable.com/image/%key.jpg)',
  1385. selector: 'a[href*="streamable.com/"]',
  1386. favicon: 'https://streamable.com/favicon.ico',
  1387. key: 'id',
  1388. reg: '(streamable\\.com)',
  1389. matcher: /(?:streamable\.com\/([A-Za-z0-9-_]+))/i,
  1390. https: true
  1391. },
  1392. imgur: {
  1393. title: 'imgur it!',
  1394. home: 'https://i.imgur.com/',
  1395. embed: 'https://i.imgur.com/%key',
  1396. thumb: 'url(https://i.imgur.com/%keyh.jpg)',
  1397. selector: 'a[href*=".gifv"]',
  1398. favicon: 'https://imgur.com/favicon.ico',
  1399. reg: '(\\.gifv$)|(imgur)',
  1400. matcher: /(?:imgur\.com\/(\w+)\.(?:gifv|mp4|webm))/i,
  1401. https: true,
  1402. scroll: true,
  1403. videoTag: true
  1404. },
  1405. html5: {
  1406. home: true,
  1407. title: 'html5 go!',
  1408. selector: 'a[href*=".webm"], a[href*=".mp4"]',
  1409. favicon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAdlJREFUeNpcks9rE0EUx7+b3TRktxvbhUhrNm6SrUWtxxbxJ9iLl0JFKWg8WLD05EWhIuRieogXwYPX4tGb/gPSq/oP6KH+uG2MFaRJJXaTbp7fWbsl+uDDzLx533lv3ozmeyUM2VlSJxeISULygTwhr5Og1JDgOXlPrpLRMAxTIpLlfJa8Ipv/i56Re4lT07SYKIqQSh2eO0/eqYnujI35HF8O19jr9XBneRmt1je0d3ag63qy5ZIAvNObE+WKTJXKktENoVPsrCntdlsWFxbiNXNJyS3KdMUXxv8w6LzY7XZhmiYer9fBYOTzR5HL5XCzWsWxYhFnTs/gxcYGgiCAZVmOyhR5BVcm8nlZvbsi29+3ZdgGg4Gs1+viua4UJiZVpkiJeqo0VaIqxbGPSKfTORRduXQ59peLx5PyItWaj9yDQtmt21XYto1GoxGvbywtxaNhGHE3ab+V6L6aKYczPo7ZuTk8WnuIWq2G64vX4JU8zJw8BXXv5D21gx+xydrn7VEb/f0+tj5twa/4+Pz1C6Y4Oo6DZrOJdDrdYuxkIlKP+ZbZzoV7e8iyk8kD/9rdRXpkBJlMpsmDC/98I97pPF//AQUBl/2/jRvsm5b1kxmeJgJlfwQYAKZQxgzeI6/EAAAAAElFTkSuQmCC',
  1410. reg: '(\\.webm$)|(\\.mp4$)',
  1411. slim: true,
  1412. scroll: true,
  1413. videoTag: true
  1414. },
  1415. 'html5-audio': {
  1416. home: true,
  1417. title: 'hey, listen!',
  1418. selector: 'a[href*=".mp3"]',
  1419. reg: '(\\.mp3$)',
  1420. slim: true,
  1421. scroll: true
  1422. }
  1423. },
  1424. sources: {
  1425. iframe: function (data) {
  1426. var key = YTMA.DB.sites[data.site].key;
  1427.  
  1428. return [
  1429. {type: 'text/html', src: YTMA.DB.sites[data.site].embed.replace('%key', data[key]) }
  1430. ];
  1431. },
  1432. 'html5-audio': function (data) {
  1433. return [
  1434. {type: 'audio/mp3', src: data.uri}
  1435. ];
  1436. },
  1437. html5: function (data) {
  1438. // attaching the type as either mp4 or webm
  1439.  
  1440. if (/(?:webm)/.test(data.uri)) {
  1441. return [
  1442. {type: 'video/webm', src: data.uri}
  1443. ];
  1444. }
  1445.  
  1446. return [
  1447. {type: 'video/mp4', src: data.uri},
  1448. {type: 'video/webm', src: data.uri},
  1449. {type: 'video/ogg; codecs="theora, vorbis"', src: data.uri}
  1450. ];
  1451. },
  1452. imgur: function (data) {
  1453. var src = YTMA.DB.sites.imgur.embed.replace('%key', data.id);
  1454.  
  1455. return [
  1456. {type: 'video/webm', src: src + '.webm'},
  1457. {type: 'video/mp4', src: src + '.mp4'}
  1458. ];
  1459. },
  1460. gfycat: function (data) {
  1461. return [
  1462. {type: 'video/mp4', src: 'https://zippy.gfycat.com/' + data.id + '.mp4'},
  1463. {type: 'video/mp4', src: 'https://fat.gfycat.com/' + data.id + '.mp4'},
  1464. {type: 'video/mp4', src: 'https://giant.gfycat.com/' + data.id + '.mp4'},
  1465. {type: 'video/webm', src: 'https://zippy.gfycat.com/' + data.id + '.webm'},
  1466. {type: 'video/webm', src: 'https://fat.gfycat.com/' + data.id + '.webm'},
  1467. {type: 'video/webm', src: 'https://giant.gfycat.com/' + data.id + '.webm'}
  1468. ];
  1469. },
  1470. youtube: function (data, attrs) {
  1471. var params = '?html5=1&version=3&modestbranding=1&rel=0&showinfo=1&vq=' + attrs.quality
  1472. + '&iv_load_policy=' + YTMA.user.preferences.yt_annotation
  1473. + '&start=' + attrs.start
  1474. + '&volume=' + YTMA.user.preferences.yt_volume;
  1475.  
  1476. return [
  1477. {type: 'text/html', src: YTMA.DB.sites.youtube.embed.replace('%key', data.id) + params}
  1478. ];
  1479. }
  1480. },
  1481. customToolbars: {
  1482. vine: {
  1483. ratio: false,
  1484. size: true
  1485. },
  1486. soundcloud: {
  1487. ratio: false,
  1488. size: false
  1489. }
  1490. },
  1491. playerSize: {
  1492. ratios: {
  1493. 1: 'sd',
  1494. 2: 'hd',
  1495. 3: 'pr'
  1496. },
  1497. sizes: {
  1498. 0 : 'h',
  1499. 240 : 's',
  1500. 360 : 'm',
  1501. 480 : 'l',
  1502. 720 : 'xl'
  1503. },
  1504. aspects: {
  1505. 1: 4 / 3,
  1506. 2: 16 / 9,
  1507. 3: 16 / 9
  1508. }
  1509. },
  1510. qualities: {
  1511. 240 : 'small',
  1512. 360 : 'medium',
  1513. 480 : 'large',
  1514. 720 : 'hd720',
  1515. 1080 : 'hd1080',
  1516. 1081 : 'highres'
  1517. }
  1518. // videoTypes: (function () {
  1519. // var v = document.createElement('video');
  1520.  
  1521. // return {
  1522. // ogg: !!v.canPlayType('video/ogg; codecs="theora, vorbis"'),
  1523. // webm: !!v.canPlayType('video/webm'),
  1524. // mp4: !!v.canPlayType('video/mp4')
  1525. // };
  1526. // }()),
  1527. };
  1528.  
  1529. /** U I CLASS
  1530. * Class for the player controls
  1531. */
  1532. YTMA.UI = function (ytma) {
  1533. this.ytmx = ytma;
  1534.  
  1535. this.play = new YTMA.Player(this.ytmx);
  1536. this.open = false;
  1537. this.selected = { size: null, ratio: null };
  1538.  
  1539. this.trigger = ytma.spn;
  1540. this.projector = $$.e('div', {className: 'ytm_projector ytm_none ytm_block ytm_normalize ytm_sans'});
  1541. this.control = $$.e('ul', {className: 'ytm_control ytm_sans'});
  1542. this.customBar = YTMA.DB.views.getToolbar(this.ytmx.data.site);
  1543. this.controlBar();
  1544. };
  1545.  
  1546. YTMA.UI.ratios = {
  1547. SD: 1,
  1548. HD: 2,
  1549. PORTRAIT: 3
  1550. };
  1551.  
  1552. YTMA.UI.sizes = {
  1553. HIDDEN: 0,
  1554. S: 240,
  1555. M: 360,
  1556. L: 480,
  1557. X: 720
  1558. };
  1559.  
  1560. /** Trigger is the VAR element */
  1561. YTMA.UI.createFromTrigger = function (t) {
  1562. console.log('createFromTrigger');
  1563. if (t.hasAttribute('data-ytmuid') && !YTMA.set[t.dataset.ytmuid]) {
  1564. console.log('createFromTrigger-new');
  1565. YTMA.addToSet(new YTMA()._reactivate(t));
  1566. }
  1567. console.log('createFromTrigger-ui');
  1568. return YTMA.set[t.dataset.ytmuid].getUI();
  1569. };
  1570.  
  1571. YTMA.UI.events = {
  1572. $fire: {
  1573. settings: function () {
  1574. YTMA.user.events.formToggle();
  1575. },
  1576. close: function () {
  1577. if (YTMA.DB.sites[this.ytmx.data.site].scroll) {
  1578. console.log('events.close-1');
  1579. this.hideAllPlayers();
  1580. } else {
  1581. console.log('events.close-2');
  1582. this.ytmx.disableOpenOnScroll();
  1583. this.hidePlayer();
  1584. }
  1585. },
  1586. ratio: function (li) {
  1587. var n = parseInt(li.dataset.value, 10);
  1588. this.play.dimmensions(n);
  1589. this.markSelected(li, 'ratio');
  1590. },
  1591. size: function (li) {
  1592. var n = parseInt(li.dataset.value, 10);
  1593. this.play.dimmensions(null, n);
  1594. this.markSelected(li, 'size');
  1595. }
  1596. },
  1597. videoBar: function (e) {
  1598. var el = e.target, t;
  1599.  
  1600. if (el.tagName.toLowerCase() === 'li' && el.dataset && el.dataset.type) {
  1601. t = el.dataset.type;
  1602. if (YTMA.UI.events.$fire[t]) {
  1603. YTMA.UI.events.$fire[t].call(this, el);
  1604. }
  1605. }
  1606. }
  1607. };
  1608.  
  1609. YTMA.UI.prototype = {
  1610. constructor: YTMA.UI,
  1611. resetViewSize: function () {
  1612. this.play.dimmensions();
  1613. this.setControlBarSize(this.play.attrs.size);
  1614. },
  1615. showOnScroll: function (el) {
  1616. if (!this.open && this.ytmx.canScroll() && this.ytmx.isBelow(el)) {
  1617. this.showPlayer();
  1618. }
  1619. },
  1620. showPlayer: function () {
  1621. this.open = true;
  1622.  
  1623. this.trigger.classList.add('ytm_none');
  1624. this.projector.classList.remove('ytm_none');
  1625.  
  1626. this.attachPlayPanel();
  1627. this.play.switchOn();
  1628.  
  1629. if (YTMA.user.preferences.focus) {
  1630. document.location.hash = '#' + this.ytmx.container.id;
  1631. }
  1632. },
  1633. hidePlayer: function () {
  1634. this.open = false;
  1635.  
  1636. this.play.switchOff();
  1637. this.trigger.classList.remove('ytm_none');
  1638. this.projector.classList.add('ytm_none');
  1639. },
  1640. attachPlayPanel: function () {
  1641. if (!this.play.panel.parentNode) {
  1642. // console.log('attaching display panel');
  1643. this.projector.appendChild(this.play.panel);
  1644. }
  1645. },
  1646. hideAllPlayers: function () {
  1647. var group = YTMA.collect(this.ytmx.data.id);
  1648. console.log('closing all', this.ytmx.data.id, group.length);
  1649. group.forEach(function (y) {
  1650. y.disableOpenOnScroll();
  1651. y.getUI().hidePlayer();
  1652. });
  1653. },
  1654. setControlBarSize: function (size) {
  1655. this.markSelected(this.control.querySelector('li[data-value="' + size + '"]'), 'size');
  1656. },
  1657. controlBar: function () {
  1658. var f = document.createDocumentFragment();
  1659.  
  1660. $$.a(f,
  1661. this.customBar.ratio ? this.buildList('ytm_ratios', [
  1662. {type: 'ratio', text: '4:3', value: YTMA.UI.ratios.SD, title: 'SD'},
  1663. {type: 'ratio', text: '16:9', value: YTMA.UI.ratios.HD, title: 'Landscape'},
  1664. {type: 'ratio', text: '9:16', value: YTMA.UI.ratios.PORTRAIT, title: 'Portrait'}]) : null,
  1665. this.customBar.size ? this.buildList('ytm_sizes', [
  1666. {type: 'size', text: '\u00D8', value: YTMA.UI.sizes.HIDDEN, title: 'Hide the video.'},
  1667. {type: 'size', text: 'S', value: YTMA.UI.sizes.S, title: '240p'},
  1668. {type: 'size', text: 'M', value: YTMA.UI.sizes.M, title: '360p'},
  1669. {type: 'size', text: 'L', value: YTMA.UI.sizes.L, title: '480p'},
  1670. {type: 'size', text: 'X', value: YTMA.UI.sizes.X, title: '720p'}]) : null,
  1671. this.buildList('ytm_options', [
  1672. strg.on ? {type: 'settings', text: '!', title: 'YTMA Settings'} : null,
  1673. {type: 'close', text: '\u00D7', title: 'Close the video.'}])
  1674. );
  1675.  
  1676. this.control.appendChild(f);
  1677. this.control.addEventListener('click', YTMA.UI.events.videoBar.bind(this), false);
  1678. this.projector.appendChild(this.control);
  1679. this.ytmx.container.insertBefore(this.projector, this.trigger.nextSibling);
  1680. },
  1681. markSelected: function (el, type) {
  1682. el.id = type + this.ytmx.data.uid;
  1683. try {
  1684. this.selected[type].removeAttribute('id');
  1685. } catch (e) {}
  1686. this.selected[type] = el;
  1687. },
  1688. buildList: function (className, elements) {
  1689. var li = $$.e('li'),
  1690. ul = $$.e('ul', {className: className}, li),
  1691. f = document.createDocumentFragment(),
  1692. i,
  1693. e;
  1694.  
  1695. for (i = 0; i < elements.length; i++) {
  1696. e = elements[i];
  1697. if (e) {
  1698. f.appendChild(this.li(e.type, e.text, e.value, e.title));
  1699. }
  1700. }
  1701. ul.appendChild(f);
  1702. return li;
  1703. },
  1704. li: function (type, txt, value, title) {
  1705. var l = $$.e('li', {_type: type, textContent: txt, _value: value, title: title});
  1706. if ((type === 'size' && this.play.attrs.size === value) || (type === 'ratio' && this.play.attrs.ratio === value)) {
  1707. this.markSelected(l, type);
  1708. }
  1709. return l;
  1710. }
  1711. };
  1712.  
  1713. /** P L A Y E R CLASS
  1714. * @param parent YTMA instance
  1715. */
  1716. YTMA.Player = function (parent) {
  1717. this.parent = parent;
  1718.  
  1719. this.mode = 'off';
  1720.  
  1721. this.attrs = {
  1722. sources: null,
  1723. quality: YTMA.DB.views.getPlayerQuality(YTMA.user.preferences.quality),
  1724. size: null,
  1725. ratio: null,
  1726. start: this.time(),
  1727. type: null
  1728. };
  1729.  
  1730. this.attrs.sources = YTMA.DB.views.getPlayerSources(parent.data.site)(parent.data, this.attrs);
  1731.  
  1732. // todo improve type/media
  1733. this.attrs.type = this.findType();
  1734. this.media = YTMA.Player.makeMedia[this.attrs.type](this);
  1735.  
  1736. this.channel = $$.e('div', {className: 'ytm_panel_channel ytm_block'}, this.media, true);
  1737. this.switcher = $$.e('div', {className: 'ytm_panel_switcher ytm_panel_size ytm_block ytm_' + this.attrs.type, _ytmuid: this.parent.data.uid, _standby: true});
  1738. this.panel = $$.e('div', {className: 'ytm_panel ytm_block'}, this.switcher, true);
  1739.  
  1740. if (parent.data.site === 'soundcloud' && YTMA.reg.extra.soundcloud.playlist.test(parent.anchor.href)) {
  1741. this.media.classList.add('ytm_soundcloud-playlist');
  1742. this.switcher.classList.add('ytm_soundcloud-playlist');
  1743. }
  1744.  
  1745. this.dimmensions(YTMA.user.preferences.ratio, YTMA.user.preferences.size);
  1746. };
  1747.  
  1748. YTMA.Player.css = {
  1749. item: function (key, value) {
  1750. if (isNumber(value)) {
  1751. value += 'px';
  1752. }
  1753.  
  1754. return '\t' + key + ': ' + value + ';\n';
  1755. },
  1756. iter: function (css, cssEntries) {
  1757. $$.o(cssEntries, function (key, value) {
  1758. css.push(YTMA.Player.css.item(key, value));
  1759. });
  1760. css.push('}');
  1761. },
  1762. generator: function () {
  1763. var css = [];
  1764.  
  1765. $$.o(this.sizes, function (size, sizes) {
  1766. $$.o(sizes, function (dimm, keys) {
  1767. css.push('\n.ytm_panel-' + size + '.ytm_panel-' + dimm + ' .ytm_panel_size {\n');
  1768. YTMA.Player.css.iter(css, keys);
  1769. });
  1770. });
  1771.  
  1772. // add site overrides
  1773. $$.o(this.sites, function (site, data) {
  1774. $$.o(data, function (setting, keys) {
  1775. if (setting === 'all') {
  1776. css.push('\n.ytm_site_' + site + ' .ytm_panel_size {\n');
  1777. } else {
  1778. css.push('\n.ytm_site_' + site + ' .ytm_panel-' + setting + ' .ytm_panel_size {\n');
  1779. }
  1780. YTMA.Player.css.iter(css, keys);
  1781. });
  1782. });
  1783.  
  1784. return css.join('');
  1785. },
  1786. sizes: (function () {
  1787. var merge = {};
  1788.  
  1789. $$.o(YTMA.DB.playerSize.sizes, function (num, size) {
  1790. if (num >= 0) {
  1791. merge[size] = {};
  1792.  
  1793. $$.o(YTMA.DB.playerSize.ratios, function (k, ratio) {
  1794. if (ratio === 'pr') {
  1795. var w = Math.floor(num * 0.95); // smaller than the normal sizes
  1796. merge[size][ratio] = {
  1797. width: w,
  1798. height: Math.floor(w * YTMA.DB.playerSize.aspects[k])
  1799. };
  1800. } else {
  1801. merge[size][ratio] = {
  1802. width: Math.floor(num * YTMA.DB.playerSize.aspects[k]),
  1803. height: num
  1804. };
  1805. }
  1806. });
  1807. }
  1808. });
  1809.  
  1810. return merge;
  1811. }()),
  1812. sites: { // custom sizes per site
  1813. soundcloud: {
  1814. all: {
  1815. height: '118px !important'
  1816. }
  1817. },
  1818. vine: {
  1819. s: {
  1820. width: 240,
  1821. height: 240
  1822. },
  1823. m: {
  1824. width: 360,
  1825. height: 360
  1826. },
  1827. l: {
  1828. width: 480,
  1829. height: 480
  1830. },
  1831. xl: {
  1832. width: 720,
  1833. height: 720
  1834. }
  1835. }
  1836. }
  1837. };
  1838.  
  1839. YTMA.Player.makeMedia = {
  1840. $css: function (type) {
  1841. return 'ytm_panel_media ytm_panel_size ytm_block ytm_' + type;
  1842. },
  1843. video: function (player) {
  1844. var video = $$.e('video', {
  1845. controls: true,
  1846. autoplay: false,
  1847. loop: true,
  1848. className: this.$css('video'),
  1849. $allowscriptaccess: true,
  1850. preload: 'metadata'
  1851. }), links = [];
  1852.  
  1853. player.attrs.sources.forEach(function (source) {
  1854. $$.e('source', {src: source.src, $type: source.type}, video);
  1855.  
  1856. links.push('<a href="' + source.src + '">' + source.src + '</a>');
  1857. });
  1858.  
  1859. $$.e('p', {innerHTML: 'Could not load source(s): ' + links.join('<br />')}, video);
  1860.  
  1861. return video;
  1862. },
  1863. iframe: function (player) {
  1864. return $$.e('iframe', {
  1865. $allowfullscreen: true,
  1866. // $sandbox: 'allow-same-origin allow-scripts allow-popups',
  1867. $type: player.attrs.sources[0].type,
  1868. src: player.attrs.sources[0].src,
  1869. className: this.$css('iframe')
  1870. });
  1871. },
  1872. audio: function (player) {
  1873. return $$.e('audio', {
  1874. src: player.attrs.sources[0].src,
  1875. $type: player.attrs.sources[0].type
  1876. });
  1877. }
  1878. };
  1879.  
  1880. YTMA.Player.prototype = {
  1881. constructor: YTMA.Player,
  1882. dimmensions: function (ratio, size) {
  1883. this.attrs.ratio = isNumber(ratio) ? ratio : this.attrs.ratio;
  1884. this.attrs.size = isNumber(size) ? size : this.attrs.size;
  1885. this.panel.className = YTMA.DB.views.getPlayerDimmensions(this.attrs.ratio, this.attrs.size);
  1886. },
  1887. time: function () {
  1888. try {
  1889. var m = this.parent.data.uri.match(YTMA.reg.time).slice(1, 3);
  1890. return ((+m[0] || 0) * 60 * 60) + ((+m[1] || 0) * 60) + (+m[2] || 0);
  1891. } catch (e) { return 0; }
  1892. },
  1893. findType: function () {
  1894. if (this.parent.data.site === 'html5-audio') { return 'audio'; }
  1895. if (YTMA.DB.sites[this.parent.data.site].videoTag) { return 'video'; }
  1896. return 'iframe';
  1897. },
  1898. switchOff: function () {
  1899. // console.log('removed media');
  1900.  
  1901. if (this.media.pause) {
  1902. console.log('pausing');
  1903. this.media.pause();
  1904. }
  1905.  
  1906. try {
  1907. this.switcher.removeChild(this.channel);
  1908. } catch (e) {
  1909. // console.error(e);
  1910. }
  1911. this.mode = 'off';
  1912. },
  1913. switchOn: function () {
  1914. if (this.attrs.size === 0) {
  1915. this.attrs.size = YTMA.user.preferences.size;
  1916. this.parent.ui.resetViewSize();
  1917. }
  1918. // console.log('switch to media');
  1919. this.switcher.appendChild(this.channel);
  1920. this.switcher.dataset.standby = false;
  1921. this.mode = 'on';
  1922. },
  1923. switchStandby: function () {
  1924. // console.log('switch to standby');
  1925. this.switchOff();
  1926. this.switcher.dataset.standby = true;
  1927. this.mode = 'standby';
  1928. },
  1929. isStandby: function () {
  1930. return this.mode === 'standby';
  1931. }
  1932. };
  1933.  
  1934. YTMA.prototype = {
  1935. constructor: YTMA,
  1936. getUI: function () {
  1937. if (!this.ui) {
  1938. this.ui = new YTMA.UI(this);
  1939. }
  1940.  
  1941. return this.ui;
  1942. },
  1943. setup: function () {
  1944. var site = YTMA.DB.sites[this.data.site];
  1945.  
  1946. if (site) {
  1947. this.spn.title = site.title || 'ytma!';
  1948.  
  1949. if (site.thumb) {
  1950. this.spn.style.backgroundImage = site.thumb.replace('%key', this.data.id);
  1951. }
  1952.  
  1953. if (site.https) {
  1954. this.anchor.href = this.anchor.href.replace('http:', 'https:');
  1955. }
  1956. }
  1957.  
  1958. try {
  1959. this.dom.custom[this.data.site].call(this);
  1960. } catch (e) {}
  1961.  
  1962. this.dom.link.call(this);
  1963. this.dom.span.call(this);
  1964. },
  1965. disableOpenOnScroll: function () {
  1966. this.anchor.dataset.ytmscroll = false;
  1967. },
  1968. canScroll: function () {
  1969. return this.anchor.dataset.ytmscroll === 'true';
  1970. },
  1971. isBelow: function (link) {
  1972. return YTMA.Scroll.compare(this.anchor, link) < 1;
  1973. },
  1974. dom: {
  1975. custom: { // modifies interface according to site
  1976. youtube: function () {
  1977. this.spn.addEventListener('mouseenter', YTMA.events.thumb.start, false);
  1978. this.spn.addEventListener('mouseleave', YTMA.events.thumb.stop, false);
  1979. this.anchor.href = this.anchor.href.replace('youtu.be/', 'youtube.com/watch?v=');
  1980. }
  1981. },
  1982. link: function () {
  1983. if (this.anchor.getElementsByTagName('img').length === 0) {
  1984. this.anchor.className += ' ytm_link ytm_link_' + this.data.site + ' ';
  1985. }
  1986. this.anchor.dataset.ytmid = this.data.id;
  1987. this.anchor.dataset.ytmuid = this.data.uid;
  1988. this.anchor.dataset.ytmsid = this.data.sid;
  1989. this.anchor.title = 'Visit the video page.';
  1990. this.anchor.parentNode.insertBefore(this.container, this.anchor.nextSibling);
  1991. },
  1992. span: function () {
  1993. var f = document.createDocumentFragment(),
  1994. site = YTMA.DB.sites[this.data.site];
  1995.  
  1996. $$.e('span', {className: 'ytm_init ytm_label ytm_sans ytm_box', textContent: this.spn.title}, this.spn);
  1997. $$.e('var', {className: 'ytm_label ytm_box', _ytmid: this.data.id, _ytmuid: this.data.uid, _ytmsid: this.data.sid, _ytmsite: this.data.site, textContent: '\u25B6'}, this.spn);
  1998.  
  1999. this.spn.title = 'Watch now!';
  2000. f.appendChild(this.spn);
  2001.  
  2002. if (site.ajax) { f.appendChild(this.dom.dataLoadLink.call(this)); }
  2003. if (site.slim) { this.container.classList.add('ytm_site_slim'); }
  2004. if (site.scroll) { this.anchor.classList.add('ytm_scroll'); }
  2005.  
  2006. this.container.appendChild(f);
  2007. },
  2008. dataLoadLink: function () {
  2009. var a, s;
  2010. s = $$.e('span', {className: 'ytm_bd ytm_normalize ytm_manual _' + this.data.sid});
  2011. a = $$.e('a', {
  2012. className: 'ytm_title',
  2013. textContent: 'Load description.',
  2014. href: '#',
  2015. title: 'Load this video\'s description.',
  2016. _ytmid: this.data.id,
  2017. _ytmsite: this.data.site,
  2018. _ytmuri: this.data.uri,
  2019. _ytmdescription: 'true'
  2020. });
  2021. return $$.a(s, a);
  2022. }
  2023. }
  2024. };
  2025.  
  2026. /**
  2027. * Creates a new YTMA from the given attributes
  2028. * @String|Number id Unique ID
  2029. * @String site Website eg: youtube, vimeo
  2030. * @HTMLAnchorElement a Anchor element
  2031. */
  2032. YTMA.prototype._new = function (id, site, a) {
  2033. var uid = YTMA.escapeId(id + '_' + (YTMA.num += 1));
  2034.  
  2035. this.data = {
  2036. id: id,
  2037. uid: YTMA.escapeId(uid), // unique id
  2038. sid: YTMA.escapeId(id), // shared id
  2039. site: site,
  2040. uri: a.href
  2041. };
  2042.  
  2043. this.ui = null;
  2044.  
  2045. if (!a.hasAttribute('data-ytmscroll')) { a.dataset.ytmscroll = true; }
  2046.  
  2047. this.anchor = a;
  2048.  
  2049. this.spn = $$.e('span', {className: 'ytm_trigger ytm_block ytm_normalize ytm_sans', _ytmid: this.data.id, _ytmsite: this.data.site});
  2050. this.container = $$.e('div', {id: 'w' + this.data.uid, className: 'ytm_spacer ytm_block ytm_site_' + this.data.site});
  2051.  
  2052. return this;
  2053. };
  2054.  
  2055. /**
  2056. * Recreates a YTMA object from a trigger element
  2057. * @HTMLElement
  2058. */
  2059. YTMA.prototype._reactivate = function (trigger) {
  2060. var id = trigger.dataset.ytmid,
  2061. a = document.querySelector('a[data-ytmuid="' + trigger.dataset.ytmuid + '"]');
  2062.  
  2063. this.data = {
  2064. id: id,
  2065. uid: trigger.dataset.ytmuid,
  2066. sid: trigger.dataset.ytmsid,
  2067. site: trigger.dataset.ytmsite,
  2068. uri: a.href
  2069. };
  2070.  
  2071. this.ui = null;
  2072. this.anchor = a;
  2073. this.spn = trigger.parentElement;
  2074. this.container = this.spn.parentElement;
  2075.  
  2076. return this;
  2077. };
  2078.  
  2079. /** S C R O L L CLASS
  2080. * Window-Scroll Event Helper
  2081. */
  2082. YTMA.Scroll = (function () {
  2083.  
  2084. function Scroll(selector, cb, delay) {
  2085. this.selector = selector;
  2086. this.cb = cb;
  2087.  
  2088. // console.log('YTMA.Scroll Monitor: ', selector);
  2089. this.bound = Scroll.debounce(this.monitor.bind(this), delay || 500);
  2090.  
  2091. this.bound();
  2092. window.addEventListener('scroll', this.bound, false);
  2093. }
  2094.  
  2095. Scroll.debounce = function (fn, delay) {
  2096. var timeout;
  2097. delay = delay || 250;
  2098.  
  2099. return function () {
  2100. var self = this, args = arguments, timed;
  2101.  
  2102. timed = function () {
  2103. timeout = null;
  2104. fn.apply(self, args);
  2105. };
  2106.  
  2107. window.clearTimeout(timeout);
  2108. timeout = window.setTimeout(timed, delay);
  2109. };
  2110. };
  2111.  
  2112. Scroll.visible = function (el) {
  2113. var bound = el.getBoundingClientRect();
  2114. return (bound.top >= 0 && bound.top <= document.documentElement.clientHeight);
  2115. };
  2116.  
  2117. Scroll.visibleAll = function (el, offset) {
  2118. var bound = el.getBoundingClientRect(),
  2119. height = document.documentElement.clientHeight;
  2120. offset = isNumber(offset) ? +offset : 0;
  2121. return ((bound.bottom + offset >= 0)
  2122. && (bound.top <= height + offset || bound.bottom <= height - offset));
  2123. };
  2124.  
  2125. /** Returns 1, 0, -1 when el1 is above, exactly the same, or below el2 */
  2126. Scroll.compare = function (el1, el2) {
  2127. var a = el1.getBoundingClientRect().y,
  2128. b = el2.getBoundingClientRect().y;
  2129.  
  2130. if (a < b) { return 1; }
  2131. if (a === b) { return 0; }
  2132. return -1;
  2133. };
  2134.  
  2135. Scroll.prototype = {
  2136. stop: function () {
  2137. // console.log('clear scroll: ', this.selector);
  2138. window.removeEventListener('scroll', this.bound);
  2139. },
  2140. monitor: function () {
  2141. $$.s(this.selector, this.cb);
  2142. }
  2143. };
  2144.  
  2145. return Scroll;
  2146.  
  2147. }());
  2148.  
  2149. YTMA.main();
  2150.  
  2151. }());