Yays! (Yet Another Youtube Script)

A lightweight and non-intrusive userscript that control video playback and set the preferred player size and playback quality on YouTube.

当前为 2015-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Yays! (Yet Another Youtube Script)
  3. // @namespace youtube
  4. // @description A lightweight and non-intrusive userscript that control video playback and set the preferred player size and playback quality on YouTube.
  5. // @version 1.14
  6. // @author Eugene Nouvellieu <eugenox_gmail_com>
  7. // @license MIT License
  8. // @include http*://*.youtube.com/*
  9. // @include http*://youtube.com/*
  10. // @run-at document-end
  11. // @noframes
  12. // @grant unsafeWindow
  13. // @grant GM_deleteValue
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_xmlhttpRequest
  17. // @homepageURL https://eugenox.appspot.com/script/yays
  18. // @icon https://eugenox.appspot.com/blob/yays/yays.icon.png
  19. // ==/UserScript==
  20.  
  21. // Copyright (c) 2012-2015 Eugene Nouvellieu <eugenox_gmail_com>
  22. //
  23. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  24. // this software and associated documentation files (the "Software"), to deal in
  25. // the Software without restriction, including without limitation the rights to
  26. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  27. // the Software, and to permit persons to whom the Software is furnished to do so,
  28. // subject to the following conditions:
  29. //
  30. // The above copyright notice and this permission notice shall be included in all
  31. // copies or substantial portions of the Software.
  32. //
  33. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  34. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  35. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  36. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  37. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  38. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  39.  
  40. function YAYS(unsafeWindow) {
  41.  
  42. 'use strict';
  43.  
  44. /*
  45. * Meta.
  46. */
  47.  
  48. var Meta = {
  49. title: 'Yays! (Yet Another Youtube Script)',
  50. version: '1.14',
  51. releasedate: 'Jan 11, 2015',
  52. site: 'https://eugenox.appspot.com/script/yays',
  53. ns: 'yays'
  54. };
  55.  
  56. /*
  57. * Utility functions.
  58. */
  59.  
  60. function each(iterable, callback, scope) {
  61. if ('length' in iterable) {
  62. for (var i = 0, len = iterable.length; i < len; ++i) {
  63. callback.call(scope, i, iterable[i]);
  64. }
  65. }
  66. else {
  67. for (var key in iterable) {
  68. if (iterable.hasOwnProperty(key)) {
  69. callback.call(scope, key, iterable[key]);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. function map() {
  76. var
  77. args = Array.prototype.constructor.apply([], arguments),
  78. callback = args.shift() || bind(Array.prototype.constructor, []),
  79. results = [],
  80. i, len;
  81.  
  82. if (args.length > 1) {
  83. var getter = function(arg) { return arg[i]; };
  84. len = Math.max.apply(Math, map(function(arg) { return arg.length; }, args));
  85.  
  86. for (i = 0; i < len; ++i) {
  87. results.push(callback.apply(null, map(getter, args)));
  88. }
  89. }
  90. else {
  91. var arg = args[0];
  92. len = arg.length;
  93.  
  94. for (i = 0; i < len; ++i) {
  95. results.push(callback(arg[i]));
  96. }
  97. }
  98.  
  99. return results;
  100. }
  101.  
  102. function unique(values) {
  103. values.sort();
  104.  
  105. for (var i = 0, j; i < values.length; ) {
  106. j = i;
  107.  
  108. while (values[j] === values[j - 1]) {
  109. j++;
  110. }
  111.  
  112. if (j - i) {
  113. values.splice(i, j - i);
  114. }
  115. else {
  116. ++i;
  117. }
  118. }
  119.  
  120. return values;
  121. }
  122.  
  123. function combine(keys, values) {
  124. var object = {};
  125.  
  126. map(function(key, value) { object[key] = value; }, keys, values);
  127.  
  128. return object;
  129. }
  130.  
  131. function merge(target, source, override) {
  132. override = override === undefined || override;
  133.  
  134. for (var key in source) {
  135. if (override || ! target.hasOwnProperty(key)) {
  136. target[key] = source[key];
  137. }
  138. }
  139.  
  140. return target;
  141. }
  142.  
  143. function extend(base, proto) {
  144. function T() {}
  145. T.prototype = base.prototype;
  146.  
  147. return merge(new T(), proto);
  148. }
  149.  
  150. function noop() {
  151. return;
  152. }
  153.  
  154. function bind(func, scope, args) {
  155. if (args && args.length > 0) {
  156. return func.bind.apply(func, [scope].concat(args));
  157. }
  158.  
  159. return func.bind(scope);
  160. }
  161.  
  162. function intercept(original, extension) {
  163. original = original || noop;
  164.  
  165. return function() {
  166. extension.apply(this, arguments);
  167.  
  168. return original.apply(this, arguments);
  169. };
  170. }
  171.  
  172. function asyncCall(func, scope, args) {
  173. window.setTimeout(bind(func, scope, args), 0);
  174. }
  175.  
  176. function asyncProxy(func) {
  177. return function() {
  178. asyncCall(func, this, Array.prototype.slice.call(arguments));
  179. };
  180. }
  181.  
  182. function buildURL(path, parameters) {
  183. var query = [];
  184. each(parameters, function(key, value) { query.push(key.concat('=', encodeURIComponent(value))); });
  185.  
  186. return path.concat('?', query.join('&'));
  187. }
  188.  
  189. function parseJSON(data) {
  190. if (typeof JSON != 'undefined') {
  191. return JSON.parse(data);
  192. }
  193.  
  194. return eval('(' + data + ')');
  195. }
  196.  
  197. /*
  198. * Script context.
  199. */
  200.  
  201. var Context = (function() {
  202. function BasicContext(context, namespace) {
  203. if (context) {
  204. this._scope = this._createNamespace(context._scope, namespace);
  205. this._ns = context._ns + '.' + namespace;
  206. }
  207. else {
  208. this._scope = unsafeWindow;
  209. this._ns = 'window';
  210. }
  211. }
  212.  
  213. BasicContext.prototype = {
  214. _scope: null,
  215. _ns: null,
  216.  
  217. _createNamespace: function(scope, name) {
  218. return scope[name] = {};
  219. },
  220.  
  221. protect: function(entity) {
  222. return entity;
  223. },
  224.  
  225. publish: function(name, entity) {
  226. this._scope[name] = this.protect(entity);
  227.  
  228. return this._ns + '.' + name;
  229. },
  230.  
  231. revoke: function(name) {
  232. delete this._scope[name];
  233. }
  234. };
  235.  
  236. var Context;
  237.  
  238. if (typeof exportFunction == 'function') {
  239. Context = function(context, namespace) {
  240. BasicContext.call(this, context, namespace);
  241. };
  242.  
  243. Context.prototype = extend(BasicContext, {
  244. _createNamespace: function(scope, name) {
  245. return createObjectIn(scope, {defineAs: name});
  246. },
  247.  
  248. protect: function(entity) {
  249. switch (typeof entity) {
  250. case 'function':
  251. return exportFunction(entity, this._scope);
  252.  
  253. case 'object':
  254. return cloneInto(entity, this._scope);
  255. }
  256. }
  257. });
  258. }
  259. else {
  260. Context = BasicContext;
  261. }
  262.  
  263. return Context;
  264. })();
  265.  
  266. var
  267. pageContext = new Context(),
  268. scriptContext = new Context(pageContext, Meta.ns);
  269.  
  270. /*
  271. * Console singleton.
  272. */
  273.  
  274. var Console = {
  275. debug: function() {
  276. unsafeWindow.console.debug('[' + Meta.ns + ']' + Array.prototype.join.call(arguments, ' '));
  277. }
  278. };
  279.  
  280. /*
  281. * DOM Helper singleton.
  282. */
  283.  
  284. var DH = {
  285. ELEMENT_NODE: 1,
  286.  
  287. build: function(def) {
  288. switch (Object.prototype.toString.call(def)) {
  289. case '[object Object]':
  290. var node = this.createElement(def.tag || 'div');
  291.  
  292. if ('style' in def) {
  293. this.style(node, def.style);
  294. }
  295.  
  296. if ('attributes' in def) {
  297. this.attributes(node, def.attributes);
  298. }
  299.  
  300. if ('listeners' in def) {
  301. this.listeners(node, def.listeners);
  302. }
  303.  
  304. if ('children' in def) {
  305. this.append(node, def.children);
  306. }
  307.  
  308. return node;
  309.  
  310. case '[object String]':
  311. return this.createTextNode(def);
  312.  
  313. default:
  314. return def;
  315. }
  316. },
  317.  
  318. id: bind(document.getElementById, document),
  319. query: bind(document.querySelectorAll, document),
  320. createElement: bind(document.createElement, document),
  321. createTextNode: bind(document.createTextNode, document),
  322.  
  323. style: function(node, style) {
  324. each(style, node.style.setProperty, node.style);
  325. },
  326.  
  327. append: function(node, children) {
  328. each([].concat(children), function(i, child) { node.appendChild(this.build(child)); }, this);
  329. node.normalize();
  330. },
  331.  
  332. insertAfter: function(node, children) {
  333. var parent = node.parentNode, sibling = node.nextSibling;
  334. if (sibling) {
  335. each([].concat(children), function(i, child) { parent.insertBefore(this.build(child), sibling); }, this);
  336. parent.normalize();
  337. }
  338. else {
  339. this.append(parent, children);
  340. }
  341. },
  342.  
  343. prepend: function(node, children) {
  344. if (node.hasChildNodes()) {
  345. each([].concat(children), function(i, child) { node.insertBefore(this.build(child), node.firstChild); }, this);
  346. }
  347. else {
  348. this.append(node, children);
  349. }
  350. },
  351.  
  352. remove: function(node) {
  353. node.parentNode.removeChild(node);
  354. },
  355.  
  356. attributes: function(node, attributes) {
  357. each(attributes, node.setAttribute, node);
  358. },
  359.  
  360. hasClass: function(node, cls) {
  361. return node.hasAttribute('class') && new RegExp('\\b' + cls + '\\b').test(node.getAttribute('class'));
  362. },
  363.  
  364. addClass: function(node, clss) {
  365. node.setAttribute('class', node.hasAttribute('class') ? unique(node.getAttribute('class').concat(' ', clss).trim().split(/ +/)).join(' ') : clss);
  366. },
  367.  
  368. delClass: function(node, clss) {
  369. if (node.hasAttribute('class')) {
  370. node.setAttribute('class', node.getAttribute('class').replace(new RegExp('\\s*\\b(?:' + clss.replace(/ +/g, '|') + ')\\b\\s*', 'g'), ' ').trim());
  371. }
  372. },
  373.  
  374. listeners: function(node, listeners) {
  375. each(listeners, function(type, listener) { this.on(node, type, listener); }, this);
  376. },
  377.  
  378. on: function(node, type, listener) {
  379. node.addEventListener(type, listener, false);
  380. },
  381.  
  382. un: function(node, type, listener) {
  383. node.removeEventListener(type, listener, false);
  384. },
  385.  
  386. unwrap: function(element) {
  387. try {
  388. return XPCNativeWrapper.unwrap(element);
  389. }
  390. catch (e) {
  391. return element;
  392. }
  393. },
  394.  
  395. walk: function(node, path) {
  396. var steps = path.split('/'), step = null;
  397.  
  398. while (node && (step = steps.shift())) {
  399. if (step == '..') {
  400. node = node.parentNode;
  401. continue;
  402. }
  403.  
  404. var selector = /^(\w*)(?:\[(\d+)\])?$/.exec(step), name = selector[1], index = Number(selector[2]) || 0;
  405.  
  406. for (var i = 0, j = 0, nodes = node.childNodes; node = nodes.item(i); ++i) {
  407. if (node.nodeType == this.ELEMENT_NODE && (! name || node.tagName.toLowerCase() == name) && j++ == index) {
  408. break;
  409. }
  410. }
  411. }
  412.  
  413. return node;
  414. },
  415.  
  416. closest: function(node, predicate) {
  417. do {
  418. if (predicate(node)) {
  419. return node;
  420. }
  421. } while ((node = node.parentNode) && node.nodeType == this.ELEMENT_NODE);
  422.  
  423. return null;
  424. }
  425. };
  426.  
  427. /*
  428. * i18n
  429. */
  430.  
  431. var _ = (function() {
  432. var vocabulary = ["Playback", "START", "PAUSE", "STOP", "AUTO PAUSE", "AUTO STOP", "Set default playback state", "Quality", "AUTO", "ORIGINAL", "Set default video quality", "Size", "AUTO", "WIDE", "FIT", "Set default player size", "Player settings", "Help"];
  433. function translation(language) {
  434. switch (language) {
  435. // Hungarian - eugenox
  436. case 'hu':
  437. return ["Lej\u00e1tsz\u00e1s", "ELIND\u00cdTVA", "SZ\u00dcNETELTETVE", "MEG\u00c1LL\u00cdTVA", "AUTOMATIKUS SZ\u00dcNETELTET\u00c9S", "AUTOMATIKUS MEG\u00c1LL\u00cdT\u00c1S", "Lej\u00e1tsz\u00e1s alap\u00e9rtelmezett \u00e1llapota", "Min\u0151s\u00e9g", "AUTO", "EREDETI", "Vide\u00f3k alap\u00e9rtelmezett felbont\u00e1sa", "M\u00e9ret", "AUTO", "SZ\u00c9LES", "ILLESZTETT", "Lej\u00e1tsz\u00f3 alap\u00e9rtelmezett m\u00e9rete", "Lej\u00e1tsz\u00f3 be\u00e1ll\u00edt\u00e1sai", "S\u00fag\u00f3"];
  438. // Dutch - Mike-RaWare
  439. case 'nl':
  440. return [null, null, null, null, null, null, null, "Kwaliteit", "AUTOMATISCH", null, "Stel standaard videokwaliteit in", null, null, null, null, null, null, null];
  441. // Spanish - yonane, Dinepada, jdarlan
  442. case 'es':
  443. return ["Reproducci\u00f3n autom\u00e1tica", "Iniciar", "Pausar", "Detener", "Auto pausa", "Auto detener", "Fijar reproducci\u00f3n autom\u00e1tica", "Calidad", "Auto", "Original", "Calidad por defecto", "Tama\u00f1o", "Autom\u00e1tico", "Ancho", "Ajustar", "Tama\u00f1o del reproductor predeterminado", "Configuraci\u00f3n", "Ayuda"];
  444. // German - xemino, ich01
  445. case 'de':
  446. return ["Wiedergabe", "Start", "Pause", "Stop", "Auto-Pause", "Auto-Stop", "Standardm\u00e4\u00dfigen Wiedergabezustand setzen", "Qualit\u00e4t", "Auto", "Original", "Standard Video Qualit\u00e4t setzen", "Gr\u00f6\u00dfe", "Auto", "Breit", "Passend", "Standard Player Gr\u00f6\u00dfe setzen", "Einstellungen", "Hilfe"];
  447. // Portuguese - Pitukinha
  448. case 'pt':
  449. return [null, null, null, null, null, null, null, "Qualidade", "AUTOM\u00c1TICO", null, "Defini\u00e7\u00e3o padr\u00e3o de v\u00eddeo", null, null, null, null, null, "Configura\u00e7\u00e3o do usu\u00e1rio", null];
  450. // Greek - TastyTeo
  451. case 'el':
  452. return ["\u0391\u03bd\u03b1\u03c0\u03b1\u03c1\u03b1\u03b3\u03c9\u03b3\u03ae", null, "\u03a0\u0391\u03a5\u03a3\u0397", "\u03a3\u03a4\u0391\u039c\u0391\u03a4\u0397\u039c\u0391", "\u0391\u03a5\u03a4\u039f\u039c\u0391\u03a4\u0397 \u03a0\u0391\u03a5\u03a3\u0397", "\u0391\u03a5\u03a4\u039f\u039c\u0391\u03a4\u039f \u03a3\u03a4\u0391\u039c\u0391\u03a4\u0397\u039c\u0391", "\u039f\u03c1\u03b9\u03c3\u03bc\u03cc\u03c2 \u03c0\u03c1\u03bf\u03b5\u03c0\u03b9\u03bb\u03b5\u03b3\u03bc\u03ad\u03bd\u03b7\u03c2 \u03ba\u03b1\u03c4\u03ac\u03c3\u03c4\u03b1\u03c3\u03b7\u03c2 \u03b1\u03bd\u03b1\u03c0\u03b1\u03c1\u03b1\u03b3\u03c9\u03b3\u03ae\u03c2", "\u03a0\u03bf\u03b9\u03cc\u03c4\u03b7\u03c4\u03b1", "\u0391\u03a5\u03a4\u039f\u039c\u0391\u03a4\u039f", "\u03a0\u03a1\u039f\u0395\u03a0\u0399\u039b\u039f\u0393\u0397", "\u039f\u03c1\u03b9\u03c3\u03bc\u03cc\u03c2 \u03c0\u03c1\u03bf\u03b5\u03c0\u03b9\u03bb\u03b5\u03b3\u03bc\u03ad\u03bd\u03b7\u03c2 \u03c0\u03bf\u03b9\u03cc\u03c4\u03b7\u03c4\u03b1\u03c2 \u03b2\u03af\u03bd\u03c4\u03b5\u03bf", "\u039c\u03ad\u03b3\u03b5\u03b8\u03bf\u03c2", "\u0391\u03a5\u03a4\u039f\u039c\u0391\u03a4\u039f", "\u03a0\u039b\u0391\u03a4\u03a5", "\u03a0\u03a1\u039f\u03a3\u0391\u03a1\u039c\u039f\u0393\u0397", "\u039f\u03c1\u03b9\u03c3\u03bc\u03cc\u03c2 \u03c0\u03c1\u03bf\u03b5\u03c0\u03b9\u03bb\u03b5\u03b3\u03bc\u03ad\u03bd\u03b7\u03c2 \u03b1\u03bd\u03ac\u03bb\u03c5\u03c3\u03b7\u03c2 \u03b1\u03bd\u03b1\u03c0\u03b1\u03c1\u03b1\u03b3\u03c9\u03b3\u03ad\u03b1", "\u0395\u03c0\u03b9\u03bb\u03bf\u03b3\u03ad\u03c2 \u03b1\u03bd\u03b1\u03c0\u03b1\u03c1\u03b1\u03b3\u03c9\u03b3\u03ad\u03b1", "\u0392\u03bf\u03ae\u03b8\u03b5\u03b9\u03b1"];
  453. // French - eXa
  454. case 'fr':
  455. return [null, null, null, null, null, null, null, "Qualit\u00e9", "AUTO", "ORIGINAL", "Qualit\u00e9 par d\u00e9faut", "Taille", "AUTO", "LARGE", "ADAPT\u00c9", "Taille par d\u00e9faut du lecteur", "Options du lecteur", "Aide"];
  456. // Slovenian - Paranoia.Com
  457. case 'sl':
  458. return [null, null, null, null, null, null, null, "Kakovost", "Samodejno", null, "Nastavi privzeto kakovost videa", null, null, null, null, null, "Nastavitve predvajalnika", "Pomo\u010d"];
  459. // Russian - an1k3y
  460. case 'ru':
  461. return [null, null, null, null, null, null, null, "\u041a\u0430\u0447\u0435\u0441\u0442\u0432\u043e", "\u0410\u0412\u0422\u041e", null, "\u041a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0438\u0434\u0435\u043e \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e", "\u0420\u0410\u0417\u041c\u0415\u0420", null, "\u0420\u0410\u0417\u0412\u0415\u0420\u041d\u0423\u0422\u042c", "\u0420\u0410\u0421\u0422\u042f\u041d\u0423\u0422\u042c", "\u0420\u0430\u0437\u043c\u0435\u0440 \u0432\u0438\u0434\u0435\u043e \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e", "\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043b\u0435\u0435\u0440\u0430", "\u041f\u043e\u043c\u043e\u0449\u044c"];
  462. // Hebrew - baryoni
  463. case 'iw':
  464. return [null, null, null, null, null, null, null, "\u05d0\u05d9\u05db\u05d5\u05ea", "\u05d0\u05d5\u05d8\u05d5\u05de\u05d8\u05d9\u05ea", null, "\u05d4\u05d2\u05d3\u05e8 \u05d0\u05ea \u05d0\u05d9\u05db\u05d5\u05ea \u05d1\u05e8\u05d9\u05e8\u05ea \u05d4\u05de\u05d7\u05d3\u05dc \u05e9\u05dc \u05d4\u05d5\u05d9\u05d3\u05d0\u05d5", "\u05d2\u05d5\u05d3\u05dc", null, "\u05e8\u05d7\u05d1", "\u05de\u05dc\u05d0", "\u05d4\u05d2\u05d3\u05e8 \u05d0\u05ea \u05d2\u05d5\u05d3\u05dc \u05d1\u05e8\u05d9\u05e8\u05ea \u05d4\u05de\u05d7\u05d3\u05dc \u05e9\u05dc \u05d4\u05e0\u05d2\u05df", "\u05d4\u05d2\u05d3\u05e8\u05d5\u05ea \u05e0\u05d2\u05df", "\u05e2\u05d6\u05e8\u05d4"];
  465. // Chinese - blankhang
  466. case 'zh':
  467. return ["\u64ad\u653e\u6a21\u5f0f", "\u64ad\u653e", "\u6682\u505c", "\u505c\u6b62", "\u81ea\u52a8\u6682\u505c", "\u81ea\u52a8\u505c\u6b62", "\u8bbe\u7f6e\u9ed8\u8ba4\u64ad\u653e\u6a21\u5f0f", "\u89c6\u9891\u8d28\u91cf", "\u81ea\u52a8", "\u539f\u753b", "\u8bbe\u7f6e\u9ed8\u8ba4\u89c6\u9891\u8d28\u91cf", "\u64ad\u653e\u5668\u5927\u5c0f", "\u81ea\u52a8", "\u5bbd\u5c4f", "\u81ea\u9002\u5e94", "\u8bbe\u7f6e\u64ad\u653e\u5668\u9ed8\u8ba4\u5927\u5c0f", "\u64ad\u653e\u5668\u8bbe\u7f6e", "\u5e2e\u52a9"];
  468. // Polish - mkvs
  469. case 'pl':
  470. return ["Odtwarzanie", "URUCHOM", "WSTRZYMAJ", "ZATRZYMAJ", "AUTOMATYCZNIE WSTRZYMAJ", "AUTOMATYCZNIE ZATRZYMAJ", "Ustaw domy\u015blny stan odtwarzania", "Jako\u015b\u0107", "AUTOMATYCZNA", "ORYGINALNA", "Ustaw domy\u015bln\u0105 jako\u015b\u0107 film\u00f3w", "Rozmiar", "AUTOMATYCZNY", "SZEROKI", "DOPASOWANY", "Ustaw domy\u015blny rozmiar odtwarzacza", "Ustawienia odtwarzacza", "Pomoc"];
  471. // Swedish - eson
  472. case 'sv':
  473. return ["Uppspelning", "START", "PAUS", "STOPP", "AUTOPAUS", "AUTOSTOPP", "Ange uppspelningsl\u00e4ge", "Kvalitet", "AUTO", "ORIGINAL", "Ange standardkvalitet", "Storlek", "AUTO", "BRED", "ANPASSAD", "Ange standardstorlek", "Inst\u00e4llningar", "Hj\u00e4lp"];
  474. // Ukrainian - mukolah
  475. case 'uk':
  476. return ["\u0421\u0442\u0430\u043d \u043f\u0440\u043e\u0433\u0440\u0430\u0432\u0430\u0447\u0430", "\u0412\u0406\u0414\u0422\u0412\u041e\u0420\u0418\u0422\u0418", "\u041f\u0420\u0418\u0417\u0423\u041f\u0418\u041d\u0418\u0422\u0418", "\u0417\u0423\u041f\u0418\u041d\u0418\u0422\u0418", "\u0410\u0412\u0422\u041e\u041c\u0410\u0422\u0418\u0427\u041d\u0415 \u041f\u0420\u0418\u0417\u0423\u041f\u0418\u041d\u0415\u041d\u041d\u042f", "\u0410\u0412\u0422\u041e\u041c\u0410\u0422\u0418\u0427\u041d\u0415 \u0417\u0423\u041f\u0418\u041d\u0415\u041d\u041d\u042f", "\u0412\u0438\u0431\u0435\u0440\u0456\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0438\u0439 \u0441\u0442\u0430\u043d \u0432\u0456\u0434\u0442\u0432\u043e\u0440\u0435\u043d\u043d\u044f", "\u042f\u043a\u0456\u0441\u0442\u044c", "\u0410\u0412\u0422\u041e\u041c\u0410\u0422\u0418\u0427\u041d\u041e", "\u041e\u0420\u0418\u0413\u0406\u041d\u0410\u041b\u042c\u041d\u0410", "\u0412\u0438\u0431\u0435\u0440\u0456\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443 \u044f\u043a\u0456\u0441\u0442\u044c \u0432\u0456\u0434\u0442\u0432\u043e\u0440\u0435\u043d\u043d\u044f", "\u0420\u043e\u0437\u043c\u0456\u0440", "\u0410\u0412\u0422\u041e\u041c\u0410\u0422\u0418\u0427\u041d\u0418\u0419", "\u0428\u0418\u0420\u041e\u041a\u0418\u0419", "\u041f\u041e \u0428\u0418\u0420\u0418\u041d\u0406 \u0415\u041a\u0420\u0410\u041d\u0423", "\u0412\u0438\u0431\u0435\u0440\u0456\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0438\u0439 \u0440\u043e\u0437\u043c\u0456\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u0432\u0430\u0447\u0430", "\u041d\u0430\u043b\u0430\u0448\u0442\u0443\u0432\u0430\u043d\u043d\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u0432\u0430\u0447\u0430", "\u0414\u043e\u043f\u043e\u043c\u043e\u0433\u0430"];
  477. }
  478.  
  479. return [];
  480. }
  481.  
  482. var dictionary = combine(vocabulary, translation((document.documentElement.lang || 'en').substr(0, 2)));
  483.  
  484. return function(text) {
  485. return dictionary[text] || text;
  486. };
  487. })();
  488.  
  489. /**
  490. * @class ScopedStorage
  491. */
  492. function ScopedStorage(scope, namespace) {
  493. this._scope = scope;
  494. this._ns = namespace;
  495. }
  496.  
  497. ScopedStorage.prototype = {
  498. _scope: null,
  499. _ns: null,
  500.  
  501. getItem: function(key) {
  502. return this._scope.getItem(this._ns + '.' + key);
  503. },
  504.  
  505. setItem: function(key, value) {
  506. this._scope.setItem(this._ns + '.' + key, value);
  507. },
  508.  
  509. removeItem: function(key) {
  510. this._scope.removeItem(this._ns + '.' + key);
  511. }
  512. };
  513.  
  514. var scriptStorage = new ScopedStorage(unsafeWindow.localStorage, Meta.ns);
  515.  
  516. /*
  517. * Configuration handler singleton.
  518. */
  519.  
  520. var Config = (function() {
  521. // Greasemonkey compatible
  522. if (typeof GM_getValue == 'function') {
  523. return {
  524. get: GM_getValue,
  525. set: GM_setValue,
  526. del: GM_deleteValue
  527. };
  528. }
  529.  
  530. var configStorage = new ScopedStorage(scriptStorage, 'config');
  531.  
  532. // HTML5
  533. return {
  534. get: function(key) {
  535. return configStorage.getItem(key);
  536. },
  537.  
  538. set: function(key, value) {
  539. configStorage.setItem(key, value);
  540. },
  541.  
  542. del: function(key) {
  543. configStorage.removeItem(key);
  544. }
  545. };
  546. })();
  547.  
  548. /**
  549. * @class JSONRequest
  550. * Create XHR or JSONP requests.
  551. */
  552. var JSONRequest = (function() {
  553. var Request = null;
  554.  
  555. // XHR
  556. if (typeof GM_xmlhttpRequest == 'function') {
  557. Request = function(url, parameters, callback) {
  558. this._callback = callback;
  559.  
  560. GM_xmlhttpRequest({
  561. method: 'GET',
  562. url: buildURL(url, parameters),
  563. onload: bind(this._onLoad, this)
  564. });
  565. };
  566.  
  567. Request.prototype = {
  568. _onLoad: function(response) {
  569. this._callback(parseJSON(response.responseText));
  570. }
  571. };
  572. }
  573. // JSONP
  574. else {
  575. Request = function(url, parameters, callback) {
  576. this._callback = callback;
  577. this._id = 'jsonp_' + Request.counter++;
  578.  
  579. parameters.callback = scriptContext.publish(this._id, bind(this._onLoad, this));
  580.  
  581. this._scriptNode = document.body.appendChild(DH.build({
  582. tag: 'script',
  583. attributes: {
  584. 'type': 'text/javascript',
  585. 'src': buildURL(url, parameters)
  586. }
  587. }));
  588. };
  589.  
  590. Request.counter = 0;
  591.  
  592. Request.prototype = {
  593. _callback: null,
  594. _id: null,
  595. _scriptNode: null,
  596.  
  597. _onLoad: function(response) {
  598. this._callback(response);
  599.  
  600. scriptContext.revoke(this._id);
  601.  
  602. document.body.removeChild(this._scriptNode);
  603. }
  604. };
  605. }
  606.  
  607. return Request;
  608. })();
  609.  
  610. /*
  611. * Update checker.
  612. */
  613.  
  614. (function() {
  615. if (new Date().valueOf() - Number(scriptStorage.getItem('update_checked_at')) < 86400000) { // 1 day
  616. return;
  617. }
  618.  
  619. var popup = null;
  620.  
  621. new JSONRequest(Meta.site + '/changelog', {version: Meta.version}, function(changelog) {
  622. scriptStorage.setItem('update_checked_at', new Date().valueOf().toFixed());
  623.  
  624. if (changelog && changelog.length) {
  625. popup = renderPopup(changelog);
  626. }
  627. });
  628.  
  629. function renderPopup(changelog) {
  630. return document.body.appendChild(DH.build({
  631. style: {
  632. 'position': 'fixed',
  633. 'bottom': '0',
  634. 'width': '100%',
  635. 'z-index': '1000',
  636. 'background-color': '#f1f1f1',
  637. 'border-top': '1px solid #cccccc'
  638. },
  639. children: {
  640. style: {
  641. 'margin': '15px'
  642. },
  643. children: [{
  644. tag: 'strong',
  645. children: ['There is an update available for ', Meta.title, '.']
  646. }, {
  647. tag: 'p',
  648. style: {
  649. 'margin': '10px 0'
  650. },
  651. children: [
  652. 'You are using version ', {
  653. tag: 'strong',
  654. children: Meta.version
  655. }, ', released on ', {
  656. tag: 'em',
  657. children: Meta.releasedate
  658. }, '. Please consider updating to the latest version.'
  659. ]
  660. }, {
  661. style: {
  662. 'margin': '10px 0',
  663. 'max-height': '150px',
  664. 'overflow-y': 'auto'
  665. },
  666. children: {
  667. tag: 'a',
  668. children: 'Show changes',
  669. listeners: {
  670. click: function(e) {
  671. e.preventDefault();
  672.  
  673. DH.insertAfter(e.target, map(function(entry) {
  674. return {
  675. style: {
  676. 'margin-bottom': '5px'
  677. },
  678. children: [{
  679. tag: 'strong',
  680. children: entry.version
  681. }, ' ', {
  682. tag: 'em',
  683. children: ['(', entry.date, ')']
  684. }, {
  685. style: {
  686. 'padding': '0 0 2px 10px',
  687. 'white-space': 'pre'
  688. },
  689. children: [].concat(entry.note).join('\n')
  690. }]
  691. };
  692. }, [].concat(changelog)));
  693.  
  694. DH.remove(e.target);
  695. }
  696. }
  697. }
  698. }, {
  699. children: map(function(text, handler) {
  700. return DH.build({
  701. tag: 'button',
  702. attributes: {
  703. 'type': 'button',
  704. 'class': 'yt-uix-button yt-uix-button-default'
  705. },
  706. style: {
  707. 'margin-right': '10px',
  708. 'padding': '5px 15px'
  709. },
  710. children: text,
  711. listeners: {
  712. 'click': handler
  713. }
  714. });
  715. }, ['Update', 'Dismiss'], [openDownloadSite, removePopup])
  716. }]
  717. }
  718. }));
  719. }
  720.  
  721. function removePopup() {
  722. document.body.removeChild(popup);
  723. }
  724.  
  725. function openDownloadSite() {
  726. removePopup();
  727. unsafeWindow.open(buildURL(Meta.site + '/download', {version: Meta.version}));
  728. }
  729. })();
  730.  
  731. /*
  732. * Migrations.
  733. */
  734.  
  735. (function(currentVersion) {
  736. var previousVersion = Config.get('version') || scriptStorage.getItem('version') || '1.0';
  737.  
  738. if (previousVersion == currentVersion) {
  739. return;
  740. }
  741.  
  742. previousVersion = map(Number, previousVersion.split('.'));
  743.  
  744. each([
  745. {
  746. // Added "144p" to the quality levels.
  747. version: '1.7', apply: function() {
  748. var videoQuality = Number(Config.get('video_quality'));
  749. if (videoQuality > 0 && videoQuality < 7) {
  750. Config.set('video_quality', ++videoQuality);
  751. }
  752. }
  753. },
  754. {
  755. // Autoplay reworked.
  756. version: '1.8', apply: function() {
  757. switch (Number(Config.get('auto_play'))) {
  758. case 1: // OFF > PAUSE
  759. Config.set('video_playback', 1);
  760. break;
  761.  
  762. case 2: // AUTO > AUTO PAUSE
  763. Config.set('video_playback', 3);
  764. break;
  765. }
  766.  
  767. Config.del('auto_play');
  768. }
  769. },
  770. {
  771. // Added "1440p" to the quality levels.
  772. version: '1.10', apply: function() {
  773. var videoQuality = Number(Config.get('video_quality'));
  774. if (videoQuality > 6) {
  775. Config.set('video_quality', ++videoQuality);
  776. }
  777. }
  778. },
  779. {
  780. // Introduced the ScopedStorage class.
  781. version: '1.14', apply: function() {
  782. // Using a unique ScopedStorage for config outside of GM.
  783. each(['video_playback', 'video_quality', 'player_size', 'version'], function(i, key) {
  784. var value = scriptStorage.getItem(key);
  785.  
  786. if (value) {
  787. Config.set(key, value);
  788.  
  789. scriptStorage.removeItem(key);
  790. }
  791. });
  792.  
  793. // Removed from the config.
  794. Config.del('update_checked_at');
  795. }
  796. }
  797. ], function(i, migration) {
  798. var migrationVersion = map(Number, migration.version.split('.'));
  799.  
  800. for (var j = 0, parts = Math.max(previousVersion.length, migrationVersion.length); j < parts; ++j) {
  801. if ((previousVersion[j] || 0) < (migrationVersion[j] || 0)) {
  802. Console.debug('Applying migration', migration.version);
  803.  
  804. migration.apply();
  805.  
  806. break;
  807. }
  808. }
  809. });
  810.  
  811. Config.set('version', currentVersion);
  812. })(Meta.version);
  813.  
  814. /**
  815. * @class Player
  816. */
  817. function Player(element) {
  818. this._element = element;
  819. this._context = new Context(scriptContext, 'player' + Player._elements.indexOf(element));
  820.  
  821. this._exportApiInterface();
  822.  
  823. Console.debug('Player ready');
  824.  
  825. this._muted = Number(this.isMuted() && ! Number(Player._storage.getItem('muted')));
  826.  
  827. this._addStateChangeListener();
  828. }
  829.  
  830. merge(Player, {
  831. UNSTARTED: -1,
  832. ENDED: 0,
  833. PLAYING: 1,
  834. PAUSED: 2,
  835. BUFFERING: 3,
  836. CUED: 5,
  837.  
  838. _elements: [],
  839.  
  840. _storage: new ScopedStorage(scriptStorage, 'player'),
  841.  
  842. test: function(element) {
  843. return typeof element.getApiInterface == 'function';
  844. },
  845.  
  846. create: function(element) {
  847. switch (element.tagName) {
  848. case 'EMBED':
  849. return new FlashPlayer(element);
  850. case 'DIV':
  851. return new HTML5Player(element);
  852. }
  853.  
  854. throw 'Unknown player type';
  855. },
  856.  
  857. initialize: function(element) {
  858. if (this._elements.indexOf(element) > -1) {
  859. throw 'Player already initialized';
  860. }
  861.  
  862. var index = this._elements.indexOf(null);
  863.  
  864. if (index > -1) {
  865. this._elements[index] = element;
  866. }
  867. else {
  868. this._elements.push(element);
  869. }
  870.  
  871. return this.create(element);
  872. },
  873.  
  874. invalidate: function(player) {
  875. this._elements[this._elements.indexOf(player._element)] = null;
  876.  
  877. player.invalidate();
  878. }
  879. });
  880.  
  881. Player.prototype = {
  882. _element: null,
  883. _context: null,
  884. _muted: 0,
  885.  
  886. _exportApiInterface: function() {
  887. each(this._element.getApiInterface(), function(i, method) {
  888. if (! (method in this)) {
  889. this[method] = bind(this._element[method], this._element);
  890. }
  891. }, this);
  892. },
  893.  
  894. _unexportApiInterface: function() {
  895. each(this._element.getApiInterface(), function(i, method) {
  896. if (this.hasOwnProperty(method)) {
  897. delete this[method];
  898. }
  899. }, this);
  900. },
  901.  
  902. _onStateChange: function(state) {
  903. Console.debug('State changed to', ['unstarted', 'ended', 'playing', 'paused', 'buffering', undefined, 'cued'][state + 1]);
  904.  
  905. this.onStateChange(state);
  906. },
  907.  
  908. _addStateChangeListener: function() {
  909. this.addEventListener('onStateChange', this._context.publish('onPlayerStateChange', asyncProxy(bind(this._onStateChange, this))));
  910. },
  911.  
  912. _removeStateChangeListener: function() {
  913. this.removeEventListener('onStateChange', this._context.publish('onPlayerStateChange', noop));
  914. },
  915.  
  916. invalidate: function() {
  917. this._removeStateChangeListener();
  918. this._unexportApiInterface();
  919.  
  920. delete this.onStateChange;
  921. delete this._element;
  922.  
  923. Console.debug('Player invalidated');
  924.  
  925. this.invalidate = noop;
  926. },
  927.  
  928. onStateChange: noop,
  929.  
  930. isPlayerState: function() {
  931. return Array.prototype.indexOf.call(arguments, this.getPlayerState()) > -1;
  932. },
  933.  
  934. getVideoId: function() {
  935. try {
  936. return this.getVideoData().video_id;
  937. }
  938. catch (e) {
  939. return (this.getVideoUrl().match(/\bv=([\w-]+)/) || [, undefined])[1];
  940. }
  941. },
  942.  
  943. restartPlayback: function() {
  944. if (this.getCurrentTime() > 60) {
  945. Console.debug('Restart threshold exceeded');
  946.  
  947. return;
  948. }
  949.  
  950. var
  951. code = (location.hash + location.search).match(/\bt=(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?/) || new Array(4),
  952. seconds = (Number(code[1]) || 0) * 3600 + (Number(code[2]) || 0) * 60 + (Number(code[3]) || 0);
  953.  
  954. this.seekTo(seconds, true);
  955. },
  956.  
  957. resetState: function() {
  958. this.seekTo(this.getCurrentTime(), true);
  959. },
  960.  
  961. mute: function() {
  962. if (! this._muted++) {
  963. this._element.mute();
  964.  
  965. Player._storage.setItem('muted', '1');
  966.  
  967. Console.debug('Player muted');
  968. }
  969. },
  970.  
  971. unMute: function() {
  972. if (! --this._muted) {
  973. this._element.unMute();
  974.  
  975. Player._storage.setItem('muted', '0');
  976.  
  977. Console.debug('Player unmuted');
  978. }
  979. },
  980.  
  981. playVideo: function() {
  982. this._element.playVideo();
  983.  
  984. Console.debug('Playback started');
  985. },
  986.  
  987. pauseVideo: function() {
  988. this._element.pauseVideo();
  989.  
  990. Console.debug('Playback paused');
  991. },
  992.  
  993. stopVideo: function() {
  994. this._element.stopVideo();
  995.  
  996. Console.debug('Playback stopped');
  997. },
  998.  
  999. setPlaybackQuality: function(quality) {
  1000. this._element.setPlaybackQuality(quality);
  1001.  
  1002. Console.debug('Quality changed to', quality);
  1003. }
  1004. };
  1005.  
  1006. /**
  1007. * @class FlashPlayer
  1008. */
  1009. function FlashPlayer(element) {
  1010. Player.call(this, element);
  1011. }
  1012.  
  1013. FlashPlayer.prototype = extend(Player, {
  1014. _exportApiInterface: function() {
  1015. try {
  1016. Player.prototype._exportApiInterface.call(this);
  1017. }
  1018. catch (e) {
  1019. throw 'Player has not loaded yet';
  1020. }
  1021. },
  1022.  
  1023. _unexportApiInterface: function() {
  1024. try {
  1025. Player.prototype._unexportApiInterface.call(this);
  1026. }
  1027. catch (e) {
  1028. Console.debug('Player has unloaded');
  1029. }
  1030. },
  1031.  
  1032. _removeStateChangeListener: function() {
  1033. try {
  1034. Player.prototype._removeStateChangeListener.call(this);
  1035. }
  1036. catch (e) {
  1037. Console.debug('Player has unloaded');
  1038. }
  1039. }
  1040. });
  1041.  
  1042. /**
  1043. * @class HTML5Player
  1044. */
  1045. function HTML5Player(element) {
  1046. Player.call(this, element);
  1047. }
  1048.  
  1049. HTML5Player.prototype = extend(Player, {
  1050. restartPlayback: function() {
  1051. Player.prototype.restartPlayback.call(this);
  1052.  
  1053. this.restartPlayback = noop;
  1054. }
  1055. });
  1056.  
  1057. /**
  1058. * @class Button
  1059. */
  1060. function Button(label, tooltip) {
  1061. this._node = DH.build(this._def(tooltip, label, this._indicator = DH.build('-')));
  1062. }
  1063.  
  1064. Button.prototype = {
  1065. _indicator: null,
  1066. _node: null,
  1067.  
  1068. _def: function(tooltip, label, indicator) {
  1069. return {
  1070. tag: 'button',
  1071. attributes: {
  1072. 'type': 'button',
  1073. 'class': 'yt-uix-button yt-uix-button-default yt-uix-tooltip',
  1074. 'title': tooltip
  1075. },
  1076. listeners: {
  1077. 'click': bind(this._onClick, this)
  1078. },
  1079. style: {
  1080. 'margin': '0 0.5%'
  1081. },
  1082. children: [{
  1083. tag: 'span',
  1084. attributes: {
  1085. 'class': 'yt-uix-button-content'
  1086. },
  1087. children: label
  1088. }, {
  1089. tag: 'span',
  1090. style: {
  1091. 'font-size': '14px',
  1092. 'margin-left': '5px'
  1093. },
  1094. attributes: {
  1095. 'class': 'yt-uix-button-content'
  1096. },
  1097. children: indicator
  1098. }]
  1099. };
  1100. },
  1101.  
  1102. _onClick: function() {
  1103. this.handler();
  1104. this.refresh();
  1105. },
  1106.  
  1107. refresh: function() {
  1108. this._indicator.data = this.display();
  1109. },
  1110.  
  1111. render: function() {
  1112. this.refresh();
  1113. return this._node;
  1114. },
  1115.  
  1116. handler: noop,
  1117. display: noop
  1118. };
  1119.  
  1120. /**
  1121. * @class PlayerOption
  1122. */
  1123. function PlayerOption(player, key) {
  1124. this._player = player;
  1125. this._key = key;
  1126. }
  1127.  
  1128. PlayerOption.prototype = {
  1129. _player: null,
  1130. _key: null,
  1131.  
  1132. get: function() {
  1133. return Number(Config.get(this._key) || '0');
  1134. },
  1135.  
  1136. set: function(value) {
  1137. Config.set(this._key, Number(value));
  1138. },
  1139.  
  1140. apply: noop,
  1141. cease: noop
  1142. };
  1143.  
  1144. /**
  1145. * @class PlayerOption.Button
  1146. */
  1147. PlayerOption.Button = function(option) {
  1148. Button.call(this, this.label, this.tooltip);
  1149.  
  1150. this._option = option;
  1151. };
  1152.  
  1153. PlayerOption.Button.extend = function(attributes) {
  1154. var superclass = this;
  1155.  
  1156. function Button(option) {
  1157. superclass.call(this, option);
  1158. }
  1159.  
  1160. Button.prototype = extend(superclass, attributes);
  1161.  
  1162. return Button;
  1163. };
  1164.  
  1165. PlayerOption.Button.prototype = extend(Button, {
  1166. _option: null,
  1167.  
  1168. label: null,
  1169. tooltip: null,
  1170. states: null,
  1171.  
  1172. handler: function() {
  1173. this._option.set((this._option.get() + 1) % this.states.length);
  1174. },
  1175.  
  1176. display: function() {
  1177. return this.states[this._option.get()];
  1178. }
  1179. });
  1180.  
  1181. /**
  1182. * @class SilentPlayerOption
  1183. */
  1184. function SilentPlayerOption(player, key) {
  1185. PlayerOption.call(this, player, key);
  1186. }
  1187.  
  1188. SilentPlayerOption.prototype = extend(PlayerOption, {
  1189. _muted: false,
  1190.  
  1191. mute: function(state) {
  1192. if (this._muted != state) {
  1193. if (state) {
  1194. this._player.mute();
  1195. }
  1196. else {
  1197. this._player.unMute();
  1198. }
  1199.  
  1200. this._muted = state;
  1201. }
  1202. },
  1203.  
  1204. cease: function() {
  1205. this.mute(false);
  1206. }
  1207. });
  1208.  
  1209. /**
  1210. * @class VideoPlayback
  1211. */
  1212. function VideoPlayback(player) {
  1213. SilentPlayerOption.call(this, player, 'video_playback');
  1214.  
  1215. switch (this.get()) {
  1216. case 0: // PLAY
  1217. this._applied = true;
  1218. break;
  1219.  
  1220. case 3: // AUTO PAUSE
  1221. case 4: // AUTO STOP
  1222. // Video is visible or opened in the same window.
  1223. if (this._isVisible() || unsafeWindow.history.length > 1) {
  1224. this._applied = true;
  1225. }
  1226. // Video is opened in a background tab.
  1227. else {
  1228. this._handler = pageContext.protect(bind(this._handler, this));
  1229.  
  1230. DH.on(unsafeWindow, 'focus', this._handler);
  1231. DH.on(unsafeWindow, 'blur', this._handler);
  1232. }
  1233. break;
  1234. }
  1235. }
  1236.  
  1237. VideoPlayback.prototype = extend(SilentPlayerOption, {
  1238. _applied: false,
  1239. _timer: null,
  1240.  
  1241. _handler: function(e) {
  1242. switch (e.type) {
  1243. case 'focus':
  1244. if (this._timer === null) {
  1245. this._timer = window.setTimeout(bind(function() {
  1246. if (this._applied) {
  1247. this._player.resetState();
  1248. this._player.playVideo();
  1249.  
  1250. Console.debug('Playback autostarted');
  1251. }
  1252. else {
  1253. this._applied = true;
  1254.  
  1255. Console.debug('Playback not affected');
  1256.  
  1257. this.mute(false);
  1258. }
  1259.  
  1260. DH.un(unsafeWindow, 'focus', this._handler);
  1261. DH.un(unsafeWindow, 'blur', this._handler);
  1262.  
  1263. this._timer = null;
  1264. }, this), 500);
  1265. }
  1266. break;
  1267.  
  1268. case 'blur':
  1269. if (this._timer !== null) {
  1270. clearTimeout(this._timer);
  1271.  
  1272. this._timer = null;
  1273. }
  1274. break;
  1275. }
  1276. },
  1277.  
  1278. // @see http://www.w3.org/TR/page-visibility/
  1279. _isVisible: function() {
  1280. var doc = unsafeWindow.document;
  1281. return doc.hidden === false || doc.mozHidden === false || doc.webkitHidden === false;
  1282. },
  1283.  
  1284. apply: function() {
  1285. if (! this._applied) {
  1286. this.mute(true);
  1287.  
  1288. if (this._player.isPlayerState(Player.PLAYING)) {
  1289. this._applied = true;
  1290.  
  1291. this._player.restartPlayback();
  1292.  
  1293. if (this.get() % 2) { // (AUTO) PAUSE
  1294. this._player.pauseVideo();
  1295. }
  1296. else { // (AUTO) STOP
  1297. this._player.stopVideo();
  1298. }
  1299.  
  1300. this.mute(false);
  1301. }
  1302. }
  1303. }
  1304. });
  1305.  
  1306. /**
  1307. * @class VideoPlayback.Button
  1308. */
  1309. VideoPlayback.Button = PlayerOption.Button.extend({
  1310. label: _('Playback'),
  1311. tooltip: _('Set default playback state'),
  1312. states: [_('START'), _('PAUSE'), _('STOP'), _('AUTO PAUSE'), _('AUTO STOP')]
  1313. });
  1314.  
  1315. /**
  1316. * @class VideoQuality
  1317. */
  1318. function VideoQuality(player) {
  1319. SilentPlayerOption.call(this, player, 'video_quality');
  1320.  
  1321. this._applied = ! this.get();
  1322. }
  1323.  
  1324. VideoQuality.prototype = extend(SilentPlayerOption, {
  1325. _applied: false,
  1326.  
  1327. apply: function() {
  1328. if (! this._applied) {
  1329. this.mute(true);
  1330.  
  1331. if (this._player.isPlayerState(Player.PLAYING, Player.PAUSED, Player.BUFFERING)) {
  1332. this._applied = true;
  1333.  
  1334. var quality = ['tiny', 'small', 'medium', 'large', 'hd720', 'hd1080', 'hd1440', 'highres'][this.get() - 1];
  1335.  
  1336. if (quality != this._player.getPlaybackQuality()) {
  1337. this._player.restartPlayback();
  1338. this._player.setPlaybackQuality(quality);
  1339. }
  1340.  
  1341. asyncCall(this.apply, this);
  1342. }
  1343. } else if (this._player.isPlayerState(Player.PLAYING, Player.CUED)) {
  1344. this.mute(false);
  1345. }
  1346. }
  1347. });
  1348.  
  1349. /**
  1350. * @class VideoQuality.Button
  1351. */
  1352. VideoQuality.Button = PlayerOption.Button.extend({
  1353. label: _('Quality'),
  1354. tooltip: _('Set default video quality'),
  1355. states: [_('AUTO'), '144p', '240p', '360p', '480p', '720p', '1080p', '1440p', _('ORIGINAL')]
  1356. });
  1357.  
  1358. /**
  1359. * @class PlayerSize
  1360. */
  1361. function PlayerSize(player) {
  1362. PlayerOption.call(this, player, 'player_size');
  1363. }
  1364.  
  1365. PlayerSize.prototype = extend(PlayerOption, {
  1366. apply: function() {
  1367. var mode = this.get(), rules = [];
  1368.  
  1369. switch (mode) {
  1370. case 2: // FIT
  1371. rules.push(
  1372. '.watch-medium,',
  1373. '.watch-medium .player-width {',
  1374. 'width: 1066px !important;',
  1375. '}',
  1376. '.watch-medium .player-height,',
  1377. '.watch-medium #theater-background {',
  1378. 'height: 629.157px !important;',
  1379. '}',
  1380. '.watch-medium .html5-video-content[style*="640"],',
  1381. '.watch-medium .html5-video-content[style*="360"],',
  1382. '.watch-medium .html5-main-video[style*="640"],',
  1383. '.watch-medium .html5-main-video[style*="360"] {',
  1384. 'transform: matrix(1.66562, 0, 0, 1.66562, 213, 119.812) !important;',
  1385. '-o-transform: matrix(1.66562, 0, 0, 1.66562, 213, 119.812) !important;',
  1386. '-moz-transform: matrix(1.66562, 0, 0, 1.66562, 213, 119.812) !important;',
  1387. '-webkit-transform: matrix(1.66562, 0, 0, 1.66562, 213, 119.812) !important;',
  1388. '}',
  1389. '.watch-medium .html5-video-content[style*="854"],',
  1390. '.watch-medium .html5-video-content[style*="480"],',
  1391. '.watch-medium .html5-main-video[style*="854"],',
  1392. '.watch-medium .html5-main-video[style*="480"] {',
  1393. 'transform: matrix(1.24824, 0, 0, 1.24824, 106, 59.5785) !important;',
  1394. '-o-transform: matrix(1.24824, 0, 0, 1.24824, 106, 59.5785) !important;',
  1395. '-moz-transform: matrix(1.24824, 0, 0, 1.24824, 106, 59.5785) !important;',
  1396. '-webkit-transform: matrix(1.24824, 0, 0, 1.24824, 106, 59.5785) !important;',
  1397. '}'
  1398. );
  1399.  
  1400. break;
  1401.  
  1402. case 1: // WIDE
  1403. rules.push(
  1404. '.watch-medium .html5-video-content[style*="640"],',
  1405. '.watch-medium .html5-video-content[style*="360"],',
  1406. '.watch-medium .html5-main-video[style*="640"],',
  1407. '.watch-medium .html5-main-video[style*="360"] {',
  1408. 'transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1409. '-o-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1410. '-moz-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1411. '-webkit-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1412. '}'
  1413. );
  1414.  
  1415. break;
  1416.  
  1417. default:
  1418. return;
  1419. }
  1420.  
  1421. rules.push(
  1422. '.watch-medium .html5-main-video {',
  1423. 'z-index: -1;',
  1424. '}'
  1425. );
  1426.  
  1427. DH.id('yays-player-size') || DH.append(document.body, {
  1428. tag: 'style',
  1429. attributes: {
  1430. 'id': 'yays-player-size',
  1431. 'type': 'text/css'
  1432. },
  1433. children: rules
  1434. });
  1435.  
  1436. var container = DH.id('watch7-container'), player = DH.id('player'), page = DH.id('page');
  1437.  
  1438. DH.addClass(container, 'watch-wide');
  1439. DH.delClass(player, 'watch-small');
  1440. DH.addClass(player, 'watch-medium');
  1441. DH.delClass(page, 'watch-non-stage-mode');
  1442. DH.addClass(page, 'watch-stage-mode');
  1443.  
  1444. Console.debug('Size set to', ['wide', 'fit'][mode - 1]);
  1445. }
  1446. });
  1447.  
  1448. /**
  1449. * @class PlayerSize.Button
  1450. */
  1451. PlayerSize.Button = PlayerOption.Button.extend({
  1452. label: _('Size'),
  1453. tooltip: _('Set default player size'),
  1454. states: [_('AUTO'), _('WIDE'), _('FIT')]
  1455. });
  1456.  
  1457. /**
  1458. * @class UI
  1459. * Abstract UI class.
  1460. */
  1461. function UI(content) {
  1462. this.content = content;
  1463. this.button = DH.build(this._def.button(bind(this.toggle, this)));
  1464. this.panel = DH.build(this._def.panel(content));
  1465. }
  1466.  
  1467. merge(UI, {
  1468. instance: null,
  1469.  
  1470. initialize: function(type, content) {
  1471. if (this.instance) {
  1472. this.instance.destroy();
  1473. }
  1474.  
  1475. return this.instance = new type(content);
  1476. }
  1477. });
  1478.  
  1479. UI.prototype = {
  1480. _def: {
  1481. icon: function(def) {
  1482. def = merge({tag: 'img', attributes: {}}, def);
  1483.  
  1484. def.attributes.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAs0lEQVQoz4VRuRGDQAxcQDNLeKEzl2BCl+CQkIyjA3dCCYQugxJowWQ4AocEnnPAWTxjhlUirUar0y2wAGs6Otb4DyZ0PpKZjQAmcpVOMjwQezaTlzzlJvGnA8BGJ7fRABHvsNjDSd4hLloOKMZgDFBgUO48v91RlWg9U6/K1UU6EuIAIYD8Jzyv8EkOgKUe1U8NWvbKlYJW1QwqVpsN7eFHTR6kNCvhnpaG6dKTXbNwZPcX5uNiEvATXN0AAAAASUVORK5CYII=';
  1485.  
  1486. return def;
  1487. },
  1488.  
  1489. button: function(click) {
  1490. return {
  1491. listeners: {
  1492. 'click': click
  1493. }
  1494. };
  1495. },
  1496.  
  1497. panel: function(content) {
  1498. return [{
  1499. style: {
  1500. 'margin-bottom': '10px'
  1501. },
  1502. children: [{
  1503. tag: 'strong',
  1504. children: _('Player settings')
  1505. }, {
  1506. tag: 'a',
  1507. attributes: {
  1508. 'href': Meta.site,
  1509. 'target': '_blank'
  1510. },
  1511. style: {
  1512. 'margin-left': '4px',
  1513. 'vertical-align': 'super',
  1514. 'font-size': '10px'
  1515. },
  1516. children: _('Help')
  1517. }]
  1518. }, {
  1519. style: {
  1520. 'text-align': 'center'
  1521. },
  1522. children: content.render()
  1523. }];
  1524. }
  1525. },
  1526.  
  1527. content: null,
  1528. button: null,
  1529. panel: null,
  1530.  
  1531. destroy: function() {
  1532. DH.remove(this.button);
  1533. DH.remove(this.panel);
  1534. },
  1535.  
  1536. toggle: function() {
  1537. this.content.refresh();
  1538. }
  1539. };
  1540.  
  1541. /**
  1542. * @class UI.Content
  1543. */
  1544. UI.Content = function(buttons) {
  1545. this._buttons = buttons;
  1546. };
  1547.  
  1548. UI.Content.prototype = {
  1549. _buttons: null,
  1550.  
  1551. render: function() {
  1552. return map(function(button) { return button.render(); }, this._buttons);
  1553. },
  1554.  
  1555. refresh: function() {
  1556. each(this._buttons, function(i, button) { button.refresh(); });
  1557. }
  1558. };
  1559.  
  1560. /**
  1561. * @class UI.Requirement
  1562. */
  1563. UI.Requirement = function(queries) {
  1564. this._queries = [].concat(queries);
  1565. };
  1566.  
  1567. UI.Requirement.prototype = {
  1568. _queries: null,
  1569.  
  1570. test: function() {
  1571. return DH.query(this._queries.join(', ')).length >= this._queries.length;
  1572. }
  1573. };
  1574.  
  1575. /**
  1576. * @class WatchUI
  1577. */
  1578. function WatchUI(buttons) {
  1579. UI.call(this, new UI.Content(buttons));
  1580. }
  1581.  
  1582. WatchUI.prototype = extend(UI, {
  1583. _def: {
  1584. panel: function(content) {
  1585. return {
  1586. attributes: {
  1587. 'id': 'action-panel-yays',
  1588. 'class': 'action-panel-content hid',
  1589. 'data-panel-loaded': 'true'
  1590. },
  1591. children: UI.prototype._def.panel(content)
  1592. };
  1593. }
  1594. }
  1595. });
  1596.  
  1597. /**
  1598. * @class Watch8UI
  1599. */
  1600. function Watch8UI(buttons) {
  1601. WatchUI.call(this, buttons);
  1602.  
  1603. DH.append(DH.id('watch8-secondary-actions'), this.button);
  1604. DH.append(DH.id('watch-action-panels'), this.panel);
  1605. }
  1606.  
  1607. Watch8UI.requirement = new UI.Requirement(['#page.watch #watch8-secondary-actions', '#page.watch #watch-action-panels']);
  1608.  
  1609. Watch8UI.prototype = extend(WatchUI, {
  1610. _def: {
  1611. button: function(click) {
  1612. return {
  1613. tag: 'span',
  1614. children: {
  1615. tag: 'button',
  1616. attributes: {
  1617. 'type': 'button',
  1618. 'class': 'action-panel-trigger yt-uix-button yt-uix-button-empty yt-uix-button-has-icon yt-uix-button-opacity yt-uix-button-size-default yt-uix-tooltip',
  1619. 'data-button-toggle': 'true',
  1620. 'data-trigger-for': 'action-panel-yays',
  1621. 'data-tooltip-text': _('Player settings')
  1622. },
  1623. listeners: {
  1624. 'click': click
  1625. },
  1626. children: {
  1627. tag: 'span',
  1628. attributes: {
  1629. 'class': 'yt-uix-button-icon-wrapper'
  1630. },
  1631. children: UI.prototype._def.icon({
  1632. attributes: {
  1633. 'class': 'yt-uix-button-icon'
  1634. }
  1635. })
  1636. }
  1637. }
  1638. };
  1639. },
  1640.  
  1641. panel: WatchUI.prototype._def.panel
  1642. }
  1643. });
  1644.  
  1645. /**
  1646. * @class ChannelUI
  1647. */
  1648. function ChannelUI(buttons) {
  1649. UI.call(this, new UI.Content(buttons));
  1650.  
  1651. DH.append(DH.id('channel-navigation-menu'), {
  1652. tag: 'li',
  1653. children: [this.button, this.panel]
  1654. });
  1655. }
  1656.  
  1657. ChannelUI.requirement = new UI.Requirement('#channel-navigation-menu');
  1658.  
  1659. ChannelUI.prototype = extend(UI, {
  1660. _def: {
  1661. button: function(click) {
  1662. return {
  1663. tag: 'button',
  1664. attributes: {
  1665. 'type': 'button',
  1666. 'role': 'button',
  1667. 'class': 'yt-uix-button yt-uix-button-empty yt-uix-button-epic-nav-item yt-uix-tooltip flip',
  1668. 'data-button-menu-id': 'yays-panel-dropdown',
  1669. 'data-tooltip-text': _('Player settings')
  1670. },
  1671. style: {
  1672. 'position': 'absolute',
  1673. 'right': '20px',
  1674. 'width': '30px'
  1675. },
  1676. listeners: {
  1677. 'click': click
  1678. },
  1679. children: {
  1680. tag: 'span',
  1681. attributes: {
  1682. 'class': 'yt-uix-button-icon-wrapper'
  1683. },
  1684. style: {
  1685. 'opacity': '0.75'
  1686. },
  1687. children: UI.prototype._def.icon()
  1688. }
  1689. };
  1690. },
  1691.  
  1692. panel: function(content) {
  1693. return {
  1694. attributes: {
  1695. 'id': 'yays-panel-dropdown',
  1696. 'class': 'epic-nav-item-dropdown hid'
  1697. },
  1698. style: {
  1699. 'padding': '5px 10px 10px',
  1700. 'width': '450px'
  1701. },
  1702. children: UI.prototype._def.panel(content)
  1703. };
  1704. }
  1705. }
  1706. });
  1707.  
  1708. /*
  1709. * Ready callbacks.
  1710. */
  1711.  
  1712. function onReady(player) {
  1713. var
  1714. videoPlayback = new VideoPlayback(player),
  1715. videoQuality = new VideoQuality(player),
  1716. playerSize = new PlayerSize(player),
  1717. previousVideo = player.getVideoId();
  1718.  
  1719. player.onStateChange = function() {
  1720. try {
  1721. var currentVideo = player.getVideoId();
  1722.  
  1723. if (currentVideo == previousVideo) {
  1724. videoQuality.apply();
  1725. videoPlayback.apply();
  1726. }
  1727. else {
  1728. videoQuality.cease();
  1729. videoPlayback.cease();
  1730.  
  1731. throw null;
  1732. }
  1733. }
  1734. catch (e) {
  1735. Player.invalidate(player);
  1736.  
  1737. asyncCall(onPlayerReady);
  1738. }
  1739. };
  1740.  
  1741. videoQuality.apply();
  1742. videoPlayback.apply();
  1743.  
  1744. if (Watch8UI.requirement.test()) {
  1745. playerSize.apply();
  1746.  
  1747. UI.initialize(Watch8UI, [
  1748. new VideoQuality.Button(videoQuality),
  1749. new PlayerSize.Button(playerSize),
  1750. new VideoPlayback.Button(videoPlayback)
  1751. ]);
  1752. }
  1753. else if (ChannelUI.requirement.test()) {
  1754. UI.initialize(ChannelUI, [
  1755. new VideoQuality.Button(videoQuality),
  1756. new VideoPlayback.Button(videoPlayback)
  1757. ]);
  1758. }
  1759. }
  1760.  
  1761. function onPlayerReady() {
  1762. each(DH.query('video, embed'), function(i, node) {
  1763. var player = DH.closest(node, function(node) { return Player.test(DH.unwrap(node)); });
  1764.  
  1765. if (player) {
  1766. try {
  1767. player = Player.initialize(DH.unwrap(player));
  1768.  
  1769. onReady(player);
  1770.  
  1771. Console.debug('Initialization finished');
  1772. }
  1773. catch (e) {
  1774. Console.debug(e);
  1775. }
  1776. }
  1777. });
  1778. }
  1779.  
  1780. onPlayerReady();
  1781.  
  1782. // FIXME: The call to an exported function is rejected if a function (or an
  1783. // object with methods) is passed as argument.
  1784. //
  1785. // This restriction can be lifted in FF 33+ by setting the 'allowCallbacks'
  1786. // option when exporting the function.
  1787.  
  1788. var node = DH.build({
  1789. tag: 'script',
  1790. attributes: {
  1791. 'type': 'text/javascript'
  1792. }
  1793. });
  1794.  
  1795. node.text = 'function onYouTubePlayerReady() { ' + scriptContext.publish('onPlayerReady', intercept(unsafeWindow.onYouTubePlayerReady, asyncProxy(onPlayerReady))) + '(); }';
  1796.  
  1797. document.documentElement.appendChild(node);
  1798. document.documentElement.removeChild(node);
  1799.  
  1800. } // YAYS
  1801.  
  1802. if (window.top === window.self) {
  1803. if (this.unsafeWindow) { // Greasemonkey.
  1804. YAYS(unsafeWindow);
  1805. }
  1806. else {
  1807. var node = document.createElement('script');
  1808. node.setAttribute('type', 'text/javascript');
  1809. node.text = '(' + YAYS.toString() + ')(window);';
  1810.  
  1811. document.documentElement.appendChild(node);
  1812. document.documentElement.removeChild(node);
  1813. }
  1814. }