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.

当前为 2014-08-23 提交的版本,查看 最新版本

  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.13
  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-2014 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.13',
  51. releasedate: 'Aug 23, 2014',
  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(namespace) {
  203. if (namespace) {
  204. this._scope = this._createNamespace(namespace);
  205. this._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(namespace) {
  218. return unsafeWindow[namespace] = {};
  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(namespace) {
  240. BasicContext.call(this, namespace);
  241. };
  242.  
  243. Context.prototype = extend(BasicContext, {
  244. _createNamespace: function(namespace) {
  245. return createObjectIn(unsafeWindow, {defineAs: namespace});
  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. scriptContext = new Context(Meta.ns),
  268. pageContext = new Context();
  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. * Configuration handler singleton.
  491. */
  492.  
  493. var Config = (function(namespace) {
  494. // Greasemonkey compatible
  495. if (typeof GM_getValue == 'function') {
  496. return {
  497. get: GM_getValue,
  498. set: GM_setValue,
  499. del: GM_deleteValue
  500. };
  501. }
  502.  
  503. // HTML5
  504. return {
  505. get: function(key) {
  506. return unsafeWindow.localStorage.getItem(namespace + '.' + key);
  507. },
  508.  
  509. set: function(key, value) {
  510. unsafeWindow.localStorage.setItem(namespace + '.' + key, value);
  511. },
  512.  
  513. del: function(key) {
  514. unsafeWindow.localStorage.removeItem(namespace + '.' + key);
  515. }
  516. };
  517. })(Meta.ns);
  518.  
  519. /**
  520. * @class JSONRequest
  521. * Create XHR or JSONP requests.
  522. */
  523. var JSONRequest = (function() {
  524. var Request = null;
  525.  
  526. // XHR
  527. if (typeof GM_xmlhttpRequest == 'function') {
  528. Request = function(url, parameters, callback) {
  529. this._callback = callback;
  530.  
  531. GM_xmlhttpRequest({
  532. method: 'GET',
  533. url: buildURL(url, parameters),
  534. onload: bind(this._onLoad, this)
  535. });
  536. };
  537.  
  538. Request.prototype = {
  539. _onLoad: function(response) {
  540. this._callback(parseJSON(response.responseText));
  541. }
  542. };
  543. }
  544. // JSONP
  545. else {
  546. Request = function(url, parameters, callback) {
  547. this._callback = callback;
  548. this._id = 'jsonp_' + Request.counter++;
  549.  
  550. parameters.callback = scriptContext.publish(this._id, bind(this._onLoad, this));
  551.  
  552. this._scriptNode = document.body.appendChild(DH.build({
  553. tag: 'script',
  554. attributes: {
  555. 'type': 'text/javascript',
  556. 'src': buildURL(url, parameters)
  557. }
  558. }));
  559. };
  560.  
  561. Request.counter = 0;
  562.  
  563. Request.prototype = {
  564. _callback: null,
  565. _id: null,
  566. _scriptNode: null,
  567.  
  568. _onLoad: function(response) {
  569. this._callback(response);
  570.  
  571. scriptContext.revoke(this._id);
  572.  
  573. document.body.removeChild(this._scriptNode);
  574. }
  575. };
  576. }
  577.  
  578. return Request;
  579. })();
  580.  
  581. /*
  582. * Update checker.
  583. */
  584.  
  585. (function() {
  586. if (new Date().valueOf() - Number(Config.get('update_checked_at')) < 86400000) { // 1 day
  587. return;
  588. }
  589.  
  590. var popup = null;
  591.  
  592. new JSONRequest(Meta.site + '/changelog', {version: Meta.version}, function(changelog) {
  593. Config.set('update_checked_at', new Date().valueOf().toFixed());
  594.  
  595. if (changelog && changelog.length) {
  596. popup = renderPopup(changelog);
  597. }
  598. });
  599.  
  600. function renderPopup(changelog) {
  601. return document.body.appendChild(DH.build({
  602. style: {
  603. 'position': 'fixed',
  604. 'bottom': '0',
  605. 'width': '100%',
  606. 'z-index': '1000',
  607. 'background-color': '#f1f1f1',
  608. 'border-top': '1px solid #cccccc'
  609. },
  610. children: {
  611. style: {
  612. 'margin': '15px'
  613. },
  614. children: [{
  615. tag: 'strong',
  616. children: ['There is an update available for ', Meta.title, '.']
  617. }, {
  618. tag: 'p',
  619. style: {
  620. 'margin': '10px 0'
  621. },
  622. children: [
  623. 'You are using version ', {
  624. tag: 'strong',
  625. children: Meta.version
  626. }, ', released on ', {
  627. tag: 'em',
  628. children: Meta.releasedate
  629. }, '. Please consider updating to the latest version.'
  630. ]
  631. }, {
  632. style: {
  633. 'margin': '10px 0',
  634. 'max-height': '150px',
  635. 'overflow-y': 'auto'
  636. },
  637. children: {
  638. tag: 'a',
  639. children: 'Show changes',
  640. listeners: {
  641. click: function(e) {
  642. e.preventDefault();
  643.  
  644. DH.insertAfter(e.target, map(function(entry) {
  645. return {
  646. style: {
  647. 'margin-bottom': '5px'
  648. },
  649. children: [{
  650. tag: 'strong',
  651. children: entry.version
  652. }, ' ', {
  653. tag: 'em',
  654. children: ['(', entry.date, ')']
  655. }, {
  656. style: {
  657. 'padding': '0 0 2px 10px',
  658. 'white-space': 'pre'
  659. },
  660. children: [].concat(entry.note).join('\n')
  661. }]
  662. };
  663. }, [].concat(changelog)));
  664.  
  665. DH.remove(e.target);
  666. }
  667. }
  668. }
  669. }, {
  670. children: map(function(text, handler) {
  671. return DH.build({
  672. tag: 'button',
  673. attributes: {
  674. 'type': 'button',
  675. 'class': 'yt-uix-button yt-uix-button-default'
  676. },
  677. style: {
  678. 'margin-right': '10px',
  679. 'padding': '5px 15px'
  680. },
  681. children: text,
  682. listeners: {
  683. 'click': handler
  684. }
  685. });
  686. }, ['Update', 'Dismiss'], [openDownloadSite, removePopup])
  687. }]
  688. }
  689. }));
  690. }
  691.  
  692. function removePopup() {
  693. document.body.removeChild(popup);
  694. }
  695.  
  696. function openDownloadSite() {
  697. removePopup();
  698. unsafeWindow.open(buildURL(Meta.site + '/download', {version: Meta.version}));
  699. }
  700. })();
  701.  
  702. /*
  703. * Migrations.
  704. */
  705.  
  706. (function(currentVersion) {
  707. var previousVersion = Config.get('version') || '1.0';
  708.  
  709. if (previousVersion == currentVersion) {
  710. return;
  711. }
  712.  
  713. previousVersion = map(Number, previousVersion.split('.'));
  714.  
  715. each([
  716. {
  717. // Added "144p" to the quality levels.
  718. version: '1.7', apply: function() {
  719. var videoQuality = Number(Config.get('video_quality'));
  720. if (videoQuality > 0 && videoQuality < 7) {
  721. Config.set('video_quality', ++videoQuality);
  722. }
  723. }
  724. },
  725. {
  726. // Autoplay reworked.
  727. version: '1.8', apply: function() {
  728. switch (Number(Config.get('auto_play'))) {
  729. case 1: // OFF > PAUSE
  730. Config.set('video_playback', 1);
  731. break;
  732.  
  733. case 2: // AUTO > AUTO PAUSE
  734. Config.set('video_playback', 3);
  735. break;
  736. }
  737.  
  738. Config.del('auto_play');
  739. }
  740. },
  741. {
  742. // Added "1440p" to the quality levels.
  743. version: '1.10', apply: function() {
  744. var videoQuality = Number(Config.get('video_quality'));
  745. if (videoQuality > 6) {
  746. Config.set('video_quality', ++videoQuality);
  747. }
  748. }
  749. }
  750. ], function(i, migration) {
  751. var migrationVersion = map(Number, migration.version.split('.'));
  752.  
  753. for (var j = 0, parts = Math.max(previousVersion.length, migrationVersion.length); j < parts; ++j) {
  754. if ((previousVersion[j] || 0) < (migrationVersion[j] || 0)) {
  755. Console.debug('Applying migration', migration.version);
  756.  
  757. migration.apply();
  758.  
  759. break;
  760. }
  761. }
  762. });
  763.  
  764. Config.set('version', currentVersion);
  765. })(Meta.version);
  766.  
  767. /**
  768. * @class Player
  769. */
  770. function Player(element) {
  771. this._element = element;
  772.  
  773. this._exportApiInterface();
  774.  
  775. Console.debug('Player ready');
  776.  
  777. this._muted = Number(this.isMuted());
  778.  
  779. this._addStateChangeListener();
  780. }
  781.  
  782. merge(Player, {
  783. UNSTARTED: -1,
  784. ENDED: 0,
  785. PLAYING: 1,
  786. PAUSED: 2,
  787. BUFFERING: 3,
  788. CUED: 5,
  789.  
  790. instance: null,
  791.  
  792. test: function(element) {
  793. return typeof element.getApiInterface == 'function';
  794. },
  795.  
  796. create: function(element) {
  797. switch (element.tagName) {
  798. case 'EMBED':
  799. return new FlashPlayer(element);
  800. case 'DIV':
  801. return new HTML5Player(element);
  802. }
  803.  
  804. throw 'Unknown player type';
  805. },
  806.  
  807. initialize: function(element) {
  808. if (this.instance) {
  809. if (this.instance._element === element) {
  810. throw 'Player already initialized';
  811. }
  812.  
  813. this.instance.invalidate();
  814. }
  815.  
  816. return this.instance = this.create(element);
  817. }
  818. });
  819.  
  820. Player.prototype = {
  821. _element: null,
  822. _muted: 0,
  823.  
  824. _exportApiInterface: function() {
  825. each(this._element.getApiInterface(), function(i, method) {
  826. if (! (method in this)) {
  827. this[method] = bind(this._element[method], this._element);
  828. }
  829. }, this);
  830. },
  831.  
  832. _unexportApiInterface: function() {
  833. each(this._element.getApiInterface(), function(i, method) {
  834. if (this.hasOwnProperty(method)) {
  835. delete this[method];
  836. }
  837. }, this);
  838. },
  839.  
  840. _onStateChange: function(state) {
  841. Console.debug('State changed to', ['unstarted', 'ended', 'playing', 'paused', 'buffering', undefined, 'cued'][state + 1]);
  842.  
  843. this.onStateChange(state);
  844. },
  845.  
  846. _addStateChangeListener: function() {
  847. this.addEventListener('onStateChange', scriptContext.publish('onPlayerStateChange', asyncProxy(bind(this._onStateChange, this))));
  848. },
  849.  
  850. _removeStateChangeListener: function() {
  851. this.removeEventListener('onStateChange', scriptContext.publish('onPlayerStateChange', noop));
  852. },
  853.  
  854. invalidate: function() {
  855. this._removeStateChangeListener();
  856. this._unexportApiInterface();
  857.  
  858. delete this.onStateChange;
  859. delete this._element;
  860.  
  861. Console.debug('Player invalidated');
  862.  
  863. this.invalidate = noop;
  864. },
  865.  
  866. onStateChange: noop,
  867.  
  868. isPlayerState: function() {
  869. return Array.prototype.indexOf.call(arguments, this.getPlayerState()) > -1;
  870. },
  871.  
  872. isVideoLoaded: function() {
  873. return Boolean(this.getVideoId());
  874. },
  875.  
  876. getVideoId: function() {
  877. try {
  878. return this.getVideoData().video_id;
  879. }
  880. catch (e) {
  881. return (this.getVideoUrl().match(/\bv=([\w-]+)/) || [, undefined])[1];
  882. }
  883. },
  884.  
  885. restartPlayback: function() {
  886. if (this.getCurrentTime() > 60) {
  887. Console.debug('Restart threshold exceeded');
  888.  
  889. return;
  890. }
  891.  
  892. var
  893. code = (location.hash + location.search).match(/\bt=(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?/) || new Array(4),
  894. seconds = (Number(code[1]) || 0) * 3600 + (Number(code[2]) || 0) * 60 + (Number(code[3]) || 0);
  895.  
  896. this.seekTo(seconds, true);
  897. },
  898.  
  899. resetState: function() {
  900. this.seekTo(this.getCurrentTime(), true);
  901. },
  902.  
  903. mute: function() {
  904. if (! this._muted++) {
  905. this._element.mute();
  906.  
  907. Console.debug('Player muted');
  908. }
  909. },
  910.  
  911. unMute: function() {
  912. if (! --this._muted) {
  913. this._element.unMute();
  914.  
  915. Console.debug('Player unmuted');
  916. }
  917. },
  918.  
  919. playVideo: function() {
  920. this._element.playVideo();
  921.  
  922. Console.debug('Playback started');
  923. },
  924.  
  925. pauseVideo: function() {
  926. this._element.pauseVideo();
  927.  
  928. Console.debug('Playback paused');
  929. },
  930.  
  931. stopVideo: function() {
  932. this._element.stopVideo();
  933.  
  934. Console.debug('Playback stopped');
  935. },
  936.  
  937. setPlaybackQuality: function(quality) {
  938. this._element.setPlaybackQuality(quality);
  939.  
  940. Console.debug('Quality changed to', quality);
  941. }
  942. };
  943.  
  944. /**
  945. * @class FlashPlayer
  946. */
  947. function FlashPlayer(element) {
  948. Player.call(this, element);
  949. }
  950.  
  951. FlashPlayer.prototype = extend(Player, {
  952. _exportApiInterface: function() {
  953. try {
  954. Player.prototype._exportApiInterface.call(this);
  955. }
  956. catch (e) {
  957. throw 'Player has not loaded yet';
  958. }
  959. },
  960.  
  961. _unexportApiInterface: function() {
  962. try {
  963. Player.prototype._unexportApiInterface.call(this);
  964. }
  965. catch (e) {
  966. Console.debug('Player has unloaded');
  967. }
  968. },
  969.  
  970. _removeStateChangeListener: function() {
  971. try {
  972. Player.prototype._removeStateChangeListener.call(this);
  973. }
  974. catch (e) {
  975. Console.debug('Player has unloaded');
  976. }
  977. }
  978. });
  979.  
  980. /**
  981. * @class HTML5Player
  982. */
  983. function HTML5Player(element) {
  984. Player.call(this, element);
  985. }
  986.  
  987. HTML5Player.prototype = extend(Player, {
  988. restartPlayback: function() {
  989. Player.prototype.restartPlayback.call(this);
  990.  
  991. this.restartPlayback = noop;
  992. }
  993. });
  994.  
  995. /**
  996. * @class Button
  997. */
  998. function Button(label, tooltip) {
  999. this._node = DH.build(this._def(tooltip, label, this._indicator = DH.build('-')));
  1000. }
  1001.  
  1002. Button.prototype = {
  1003. _indicator: null,
  1004. _node: null,
  1005.  
  1006. _def: function(tooltip, label, indicator) {
  1007. return {
  1008. tag: 'button',
  1009. attributes: {
  1010. 'type': 'button',
  1011. 'class': 'yt-uix-button yt-uix-button-default yt-uix-tooltip',
  1012. 'title': tooltip
  1013. },
  1014. listeners: {
  1015. 'click': bind(this._onClick, this)
  1016. },
  1017. style: {
  1018. 'margin': '0 0.5%'
  1019. },
  1020. children: [{
  1021. tag: 'span',
  1022. attributes: {
  1023. 'class': 'yt-uix-button-content'
  1024. },
  1025. children: label
  1026. }, {
  1027. tag: 'span',
  1028. style: {
  1029. 'font-size': '14px',
  1030. 'margin-left': '5px'
  1031. },
  1032. attributes: {
  1033. 'class': 'yt-uix-button-content'
  1034. },
  1035. children: indicator
  1036. }]
  1037. };
  1038. },
  1039.  
  1040. _onClick: function() {
  1041. this.handler();
  1042. this.refresh();
  1043. },
  1044.  
  1045. refresh: function() {
  1046. this._indicator.data = this.display();
  1047. },
  1048.  
  1049. render: function() {
  1050. this.refresh();
  1051. return this._node;
  1052. },
  1053.  
  1054. handler: noop,
  1055. display: noop
  1056. };
  1057.  
  1058. /**
  1059. * @class PlayerOption
  1060. */
  1061. function PlayerOption(player, key) {
  1062. this._player = player;
  1063. this._key = key;
  1064. }
  1065.  
  1066. PlayerOption.prototype = {
  1067. _player: null,
  1068. _key: null,
  1069.  
  1070. get: function() {
  1071. return Number(Config.get(this._key) || '0');
  1072. },
  1073.  
  1074. set: function(value) {
  1075. Config.set(this._key, Number(value));
  1076. },
  1077.  
  1078. apply: noop,
  1079. cease: noop
  1080. };
  1081.  
  1082. /**
  1083. * @class PlayerOption.Button
  1084. */
  1085. PlayerOption.Button = function(option) {
  1086. Button.call(this, this.label, this.tooltip);
  1087.  
  1088. this._option = option;
  1089. };
  1090.  
  1091. PlayerOption.Button.extend = function(attributes) {
  1092. var superclass = this;
  1093.  
  1094. function Button(option) {
  1095. superclass.call(this, option);
  1096. }
  1097.  
  1098. Button.prototype = extend(superclass, attributes);
  1099.  
  1100. return Button;
  1101. };
  1102.  
  1103. PlayerOption.Button.prototype = extend(Button, {
  1104. _option: null,
  1105.  
  1106. label: null,
  1107. tooltip: null,
  1108. states: null,
  1109.  
  1110. handler: function() {
  1111. this._option.set((this._option.get() + 1) % this.states.length);
  1112. },
  1113.  
  1114. display: function() {
  1115. return this.states[this._option.get()];
  1116. }
  1117. });
  1118.  
  1119. /**
  1120. * @class SilentPlayerOption
  1121. */
  1122. function SilentPlayerOption(player, key) {
  1123. PlayerOption.call(this, player, key);
  1124. }
  1125.  
  1126. SilentPlayerOption.prototype = extend(PlayerOption, {
  1127. _muted: false,
  1128.  
  1129. mute: function(state) {
  1130. if (this._muted != state) {
  1131. if (state) {
  1132. this._player.mute();
  1133. }
  1134. else {
  1135. this._player.unMute();
  1136. }
  1137.  
  1138. this._muted = state;
  1139. }
  1140. },
  1141.  
  1142. cease: function() {
  1143. this.mute(false);
  1144. }
  1145. });
  1146.  
  1147. /**
  1148. * @class VideoPlayback
  1149. */
  1150. function VideoPlayback(player) {
  1151. SilentPlayerOption.call(this, player, 'video_playback');
  1152.  
  1153. switch (this.get()) {
  1154. case 0: // PLAY
  1155. this._applied = true;
  1156. break;
  1157.  
  1158. case 3: // AUTO PAUSE
  1159. case 4: // AUTO STOP
  1160. // Video is visible or opened in the same window.
  1161. if (this._isVisible() || unsafeWindow.history.length > 1) {
  1162. this._applied = true;
  1163. }
  1164. // Video is opened in a background tab.
  1165. else {
  1166. this._handler = pageContext.protect(bind(this._handler, this));
  1167.  
  1168. DH.on(unsafeWindow, 'focus', this._handler);
  1169. DH.on(unsafeWindow, 'blur', this._handler);
  1170. }
  1171. break;
  1172. }
  1173. }
  1174.  
  1175. VideoPlayback.prototype = extend(SilentPlayerOption, {
  1176. _applied: false,
  1177. _timer: null,
  1178.  
  1179. _handler: function(e) {
  1180. switch (e.type) {
  1181. case 'focus':
  1182. if (this._timer === null) {
  1183. this._timer = window.setTimeout(bind(function() {
  1184. if (this._applied) {
  1185. this._player.resetState();
  1186. this._player.playVideo();
  1187.  
  1188. Console.debug('Playback autostarted');
  1189. }
  1190. else {
  1191. this._applied = true;
  1192.  
  1193. Console.debug('Playback not affected');
  1194.  
  1195. this.mute(false);
  1196. }
  1197.  
  1198. DH.un(unsafeWindow, 'focus', this._handler);
  1199. DH.un(unsafeWindow, 'blur', this._handler);
  1200.  
  1201. this._timer = null;
  1202. }, this), 500);
  1203. }
  1204. break;
  1205.  
  1206. case 'blur':
  1207. if (this._timer !== null) {
  1208. clearTimeout(this._timer);
  1209.  
  1210. this._timer = null;
  1211. }
  1212. break;
  1213. }
  1214. },
  1215.  
  1216. // @see http://www.w3.org/TR/page-visibility/
  1217. _isVisible: function() {
  1218. var doc = unsafeWindow.document;
  1219. return doc.hidden === false || doc.mozHidden === false || doc.webkitHidden === false;
  1220. },
  1221.  
  1222. apply: function() {
  1223. if (! this._applied) {
  1224. this.mute(true);
  1225.  
  1226. if (this._player.isPlayerState(Player.PLAYING)) {
  1227. this._applied = true;
  1228.  
  1229. this._player.restartPlayback();
  1230.  
  1231. if (this.get() % 2) { // (AUTO) PAUSE
  1232. this._player.pauseVideo();
  1233. }
  1234. else { // (AUTO) STOP
  1235. this._player.stopVideo();
  1236. }
  1237.  
  1238. this.mute(false);
  1239. }
  1240. }
  1241. }
  1242. });
  1243.  
  1244. /**
  1245. * @class VideoPlayback.Button
  1246. */
  1247. VideoPlayback.Button = PlayerOption.Button.extend({
  1248. label: _('Playback'),
  1249. tooltip: _('Set default playback state'),
  1250. states: [_('START'), _('PAUSE'), _('STOP'), _('AUTO PAUSE'), _('AUTO STOP')]
  1251. });
  1252.  
  1253. /**
  1254. * @class VideoQuality
  1255. */
  1256. function VideoQuality(player) {
  1257. SilentPlayerOption.call(this, player, 'video_quality');
  1258.  
  1259. this._applied = ! this.get();
  1260. }
  1261.  
  1262. VideoQuality.prototype = extend(SilentPlayerOption, {
  1263. _applied: false,
  1264.  
  1265. apply: function() {
  1266. if (! this._applied) {
  1267. this.mute(true);
  1268.  
  1269. if (this._player.isPlayerState(Player.PLAYING, Player.PAUSED, Player.BUFFERING)) {
  1270. this._applied = true;
  1271.  
  1272. var quality = ['tiny', 'small', 'medium', 'large', 'hd720', 'hd1080', 'hd1440', 'highres'][this.get() - 1];
  1273.  
  1274. if (quality != this._player.getPlaybackQuality()) {
  1275. this._player.restartPlayback();
  1276. this._player.setPlaybackQuality(quality);
  1277. }
  1278.  
  1279. asyncCall(this.apply, this);
  1280. }
  1281. } else if (this._player.isPlayerState(Player.PLAYING, Player.CUED)) {
  1282. this.mute(false);
  1283. }
  1284. }
  1285. });
  1286.  
  1287. /**
  1288. * @class VideoQuality.Button
  1289. */
  1290. VideoQuality.Button = PlayerOption.Button.extend({
  1291. label: _('Quality'),
  1292. tooltip: _('Set default video quality'),
  1293. states: [_('AUTO'), '144p', '240p', '360p', '480p', '720p', '1080p', '1440p', _('ORIGINAL')]
  1294. });
  1295.  
  1296. /**
  1297. * @class PlayerSize
  1298. */
  1299. function PlayerSize(player) {
  1300. PlayerOption.call(this, player, 'player_size');
  1301. }
  1302.  
  1303. PlayerSize.prototype = extend(PlayerOption, {
  1304. apply: function() {
  1305. var mode = this.get(), rules = [];
  1306.  
  1307. switch (mode) {
  1308. case 2: // FIT
  1309. rules.push(
  1310. '.watch-medium,',
  1311. '.watch-medium .player-width {',
  1312. 'width: 1040px !important;',
  1313. '}',
  1314. '.watch-medium .player-height {',
  1315. 'height: 614.543px !important;',
  1316. '}',
  1317. '.watch-medium #theater-background {',
  1318. 'height: 624.543px !important;',
  1319. '}',
  1320. '.watch-medium .html5-video-content[style*="640"],',
  1321. '.watch-medium .html5-main-video[style*="640"] {',
  1322. 'transform: matrix(1.625, 0, 0, 1.625, 200, 112.5) !important;',
  1323. '-o-transform: matrix(1.625, 0, 0, 1.625, 200, 112.5) !important;',
  1324. '-moz-transform: matrix(1.625, 0, 0, 1.625, 200, 112.5) !important;',
  1325. '-webkit-transform: matrix(1.625, 0, 0, 1.625, 200, 112.5) !important;',
  1326. '}',
  1327. '.watch-medium .html5-video-content[style*="854"],',
  1328. '.watch-medium .html5-main-video[style*="854"] {',
  1329. 'transform: matrix(1.2178, 0, 0, 1.2178, 93, 52.2717) !important;',
  1330. '-o-transform: matrix(1.2178, 0, 0, 1.2178, 93, 52.2717) !important;',
  1331. '-moz-transform: matrix(1.2178, 0, 0, 1.2178, 93, 52.2717) !important;',
  1332. '-webkit-transform: matrix(1.2178, 0, 0, 1.2178, 93, 52.2717) !important;',
  1333. '}'
  1334. );
  1335.  
  1336. break;
  1337.  
  1338. case 1: // WIDE
  1339. rules.push(
  1340. '.watch-medium .html5-video-content[style*="640"],',
  1341. '.watch-medium .html5-main-video[style*="640"] {',
  1342. 'transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1343. '-o-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1344. '-moz-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1345. '-webkit-transform: matrix(1.33438, 0, 0, 1.33438, 107, 60.1875) !important;',
  1346. '}'
  1347. );
  1348.  
  1349. break;
  1350.  
  1351. default:
  1352. return;
  1353. }
  1354.  
  1355. rules.push(
  1356. '.watch-medium .html5-main-video {',
  1357. 'z-index: -1;',
  1358. '}'
  1359. );
  1360.  
  1361. DH.id('yays-player-size') || DH.append(document.body, {
  1362. tag: 'style',
  1363. attributes: {
  1364. 'id': 'yays-player-size',
  1365. 'type': 'text/css'
  1366. },
  1367. children: rules
  1368. });
  1369.  
  1370. var container = DH.id('watch7-container'), player = DH.id('player'), page = DH.id('page');
  1371.  
  1372. DH.addClass(container, 'watch-wide');
  1373. DH.delClass(player, 'watch-small');
  1374. DH.addClass(player, 'watch-medium');
  1375. DH.delClass(page, 'watch-non-stage-mode');
  1376. DH.addClass(page, 'watch-stage-mode');
  1377.  
  1378. Console.debug('Size set to', ['wide', 'fit'][mode - 1]);
  1379. }
  1380. });
  1381.  
  1382. /**
  1383. * @class PlayerSize.Button
  1384. */
  1385. PlayerSize.Button = PlayerOption.Button.extend({
  1386. label: _('Size'),
  1387. tooltip: _('Set default player size'),
  1388. states: [_('AUTO'), _('WIDE'), _('FIT')]
  1389. });
  1390.  
  1391. /**
  1392. * @class UI
  1393. * Abstract UI class.
  1394. */
  1395. function UI(content) {
  1396. this.content = content;
  1397. this.button = DH.build(this._def.button(bind(this.toggle, this)));
  1398. this.panel = DH.build(this._def.panel(content));
  1399. }
  1400.  
  1401. merge(UI, {
  1402. instance: null,
  1403.  
  1404. initialize: function(type, content) {
  1405. if (this.instance) {
  1406. this.instance.destroy();
  1407. }
  1408.  
  1409. return this.instance = new type(content);
  1410. }
  1411. });
  1412.  
  1413. UI.prototype = {
  1414. _def: {
  1415. icon: function(def) {
  1416. def = merge({tag: 'img', attributes: {}}, def);
  1417.  
  1418. def.attributes.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAA4ElEQVQoz32RMU4CQRhG38xqQ0e7CbHCnnxHEM/AEUiIthZegFAYErIhegTuwAWIGYiWWGKypY0bkgUZCxZ2JIuvmnkz8//fzECA2ppqqnbozJ8NOZfA2tVKZwE0lFcGbADwoExeo6KCujxTzb1LLBBxDgsRpK/xmtuK5Uf3BEZvNKgXakEHmNAq5t+sjHxw5tp9gJosT27xHxe8By0m2rc4kPFpAPTAoDJkHyJQj2Fl9Zv4K51Z4OdsgB1YcC8kQO4MOQSjsUvKb9pn2crLa1ua4zOnAMRzrlhxly4PBn4BWEpBljV5iJUAAAAASUVORK5CYII=';
  1419.  
  1420. return def;
  1421. },
  1422.  
  1423. button: function(click) {
  1424. return {
  1425. listeners: {
  1426. 'click': click
  1427. }
  1428. };
  1429. },
  1430.  
  1431. panel: function(content) {
  1432. return [{
  1433. style: {
  1434. 'margin-bottom': '10px'
  1435. },
  1436. children: [{
  1437. tag: 'strong',
  1438. children: _('Player settings')
  1439. }, {
  1440. tag: 'a',
  1441. attributes: {
  1442. 'href': Meta.site,
  1443. 'target': '_blank'
  1444. },
  1445. style: {
  1446. 'margin-left': '4px',
  1447. 'vertical-align': 'super',
  1448. 'font-size': '10px'
  1449. },
  1450. children: _('Help')
  1451. }]
  1452. }, {
  1453. style: {
  1454. 'text-align': 'center'
  1455. },
  1456. children: content.render()
  1457. }];
  1458. }
  1459. },
  1460.  
  1461. content: null,
  1462. button: null,
  1463. panel: null,
  1464.  
  1465. destroy: function() {
  1466. DH.remove(this.button);
  1467. DH.remove(this.panel);
  1468. },
  1469.  
  1470. toggle: function() {
  1471. this.content.refresh();
  1472. }
  1473. };
  1474.  
  1475. /**
  1476. * @class UI.Content
  1477. */
  1478. UI.Content = function(buttons) {
  1479. this._buttons = buttons;
  1480. };
  1481.  
  1482. UI.Content.prototype = {
  1483. _buttons: null,
  1484.  
  1485. render: function() {
  1486. return map(function(button) { return button.render(); }, this._buttons);
  1487. },
  1488.  
  1489. refresh: function() {
  1490. each(this._buttons, function(i, button) { button.refresh(); });
  1491. }
  1492. };
  1493.  
  1494. /**
  1495. * @class UI.Requirement
  1496. */
  1497. UI.Requirement = function(queries) {
  1498. this._queries = [].concat(queries);
  1499. };
  1500.  
  1501. UI.Requirement.prototype = {
  1502. _queries: null,
  1503.  
  1504. test: function() {
  1505. return DH.query(this._queries.join(', ')).length >= this._queries.length;
  1506. }
  1507. };
  1508.  
  1509. /**
  1510. * @class WatchUI
  1511. */
  1512. function WatchUI(buttons) {
  1513. UI.call(this, new UI.Content(buttons));
  1514. }
  1515.  
  1516. WatchUI.prototype = extend(UI, {
  1517. _def: {
  1518. panel: function(content) {
  1519. return {
  1520. attributes: {
  1521. 'id': 'action-panel-yays',
  1522. 'class': 'action-panel-content hid',
  1523. 'data-panel-loaded': 'true'
  1524. },
  1525. style: {
  1526. 'display': 'none',
  1527. 'color': '#333'
  1528. },
  1529. children: UI.prototype._def.panel(content)
  1530. };
  1531. }
  1532. }
  1533. });
  1534.  
  1535. /**
  1536. * @class Watch7UI
  1537. */
  1538. function Watch7UI(buttons) {
  1539. WatchUI.call(this, buttons);
  1540.  
  1541. DH.append(DH.id('watch7-secondary-actions'), this.button);
  1542. DH.append(DH.id('watch7-action-panels'), this.panel);
  1543. }
  1544.  
  1545. Watch7UI.requirement = new UI.Requirement(['#page.watch #watch7-secondary-actions', '#page.watch #watch7-action-panels']);
  1546.  
  1547. Watch7UI.prototype = extend(WatchUI, {
  1548. _def: {
  1549. button: function(click) {
  1550. return {
  1551. tag: 'span',
  1552. children: {
  1553. tag: 'button',
  1554. attributes: {
  1555. 'type': 'button',
  1556. 'role': 'button',
  1557. 'class': 'action-panel-trigger yt-uix-button yt-uix-button-text yt-uix-button-empty yt-uix-tooltip',
  1558. 'data-button-toggle': 'true',
  1559. 'data-trigger-for': 'action-panel-yays',
  1560. 'data-tooltip-text': _('Player settings')
  1561. },
  1562. listeners: {
  1563. 'click': click
  1564. },
  1565. children: {
  1566. tag: 'span',
  1567. attributes: {
  1568. 'class': 'yt-uix-button-icon-wrapper'
  1569. },
  1570. children: [UI.prototype._def.icon(), {
  1571. tag: 'span',
  1572. attributes: {
  1573. 'class': 'yt-uix-button-valign'
  1574. }
  1575. }]
  1576. }
  1577. }
  1578. };
  1579. },
  1580.  
  1581. panel: WatchUI.prototype._def.panel
  1582. }
  1583. });
  1584.  
  1585. /**
  1586. * @class Watch8UI
  1587. */
  1588. function Watch8UI(buttons) {
  1589. WatchUI.call(this, buttons);
  1590.  
  1591. DH.append(DH.id('watch8-secondary-actions'), this.button);
  1592. DH.append(DH.id('watch-action-panels'), this.panel);
  1593. }
  1594.  
  1595. Watch8UI.requirement = new UI.Requirement(['#page.watch #watch8-secondary-actions', '#page.watch #watch-action-panels']);
  1596.  
  1597. Watch8UI.prototype = extend(WatchUI, {
  1598. _def: {
  1599. button: function(click) {
  1600. return {
  1601. tag: 'span',
  1602. children: {
  1603. tag: 'button',
  1604. attributes: {
  1605. 'type': 'button',
  1606. 'class': 'action-panel-trigger yt-uix-button yt-uix-button-empty yt-uix-button-has-icon yt-uix-button-opacity yt-uix-button-opacity yt-uix-button-size-default yt-uix-tooltip',
  1607. 'data-button-toggle': 'true',
  1608. 'data-trigger-for': 'action-panel-yays',
  1609. 'data-tooltip-text': _('Player settings')
  1610. },
  1611. listeners: {
  1612. 'click': click
  1613. },
  1614. children: {
  1615. tag: 'span',
  1616. attributes: {
  1617. 'class': 'yt-uix-button-icon-wrapper'
  1618. },
  1619. children: UI.prototype._def.icon({
  1620. attributes: {
  1621. 'class': 'yt-uix-button-icon'
  1622. }
  1623. })
  1624. }
  1625. }
  1626. };
  1627. },
  1628.  
  1629. panel: WatchUI.prototype._def.panel
  1630. }
  1631. });
  1632.  
  1633. /**
  1634. * @class ChannelUI
  1635. */
  1636. function ChannelUI(buttons) {
  1637. UI.call(this, new UI.Content(buttons));
  1638.  
  1639. DH.append(DH.id('channel-navigation-menu'), {
  1640. tag: 'li',
  1641. children: [this.button, this.panel]
  1642. });
  1643. }
  1644.  
  1645. ChannelUI.requirement = new UI.Requirement('#page.channel #channel-navigation-menu');
  1646.  
  1647. ChannelUI.prototype = extend(UI, {
  1648. _def: {
  1649. button: function(click) {
  1650. return {
  1651. tag: 'button',
  1652. attributes: {
  1653. 'type': 'button',
  1654. 'role': 'button',
  1655. 'class': 'yt-uix-button yt-uix-button-empty yt-uix-button-epic-nav-item yt-uix-tooltip flip',
  1656. 'data-button-menu-id': 'yays-panel-dropdown',
  1657. 'data-tooltip-text': _('Player settings')
  1658. },
  1659. style: {
  1660. 'position': 'absolute',
  1661. 'right': '20px',
  1662. 'width': '30px'
  1663. },
  1664. listeners: {
  1665. 'click': click
  1666. },
  1667. children: {
  1668. tag: 'span',
  1669. attributes: {
  1670. 'class': 'yt-uix-button-icon-wrapper'
  1671. },
  1672. style: {
  1673. 'opacity': '0.75'
  1674. },
  1675. children: UI.prototype._def.icon()
  1676. }
  1677. };
  1678. },
  1679.  
  1680. panel: function(content) {
  1681. return {
  1682. attributes: {
  1683. 'id': 'yays-panel-dropdown',
  1684. 'class': 'epic-nav-item-dropdown hid'
  1685. },
  1686. style: {
  1687. 'padding': '5px 10px 10px',
  1688. 'width': '450px'
  1689. },
  1690. children: UI.prototype._def.panel(content)
  1691. };
  1692. }
  1693. }
  1694. });
  1695.  
  1696. /**
  1697. * @class FeatherUI
  1698. */
  1699. function FeatherUI(buttons) {
  1700. UI.call(this, new FeatherUI.Content(buttons));
  1701.  
  1702. var toolbar = DH.walk(DH.id('movie_player'), '../../div[2]');
  1703.  
  1704. DH.append(toolbar, this.button);
  1705. DH.insertAfter(toolbar, this.panel);
  1706. }
  1707.  
  1708. FeatherUI.requirement = new UI.Requirement('div:nth-child(2) > * > * > #movie_player');
  1709.  
  1710. FeatherUI.prototype = extend(UI, {
  1711. _def: {
  1712. button: function(click) {
  1713. return {
  1714. tag: 'button',
  1715. attributes: {
  1716. 'class': 'b'
  1717. },
  1718. style: {
  1719. 'margin-left': '10px'
  1720. },
  1721. listeners: {
  1722. 'click': click
  1723. },
  1724. children: UI.prototype._def.icon({
  1725. style: {
  1726. 'vertical-align': 'sub',
  1727. 'opacity': '0.82'
  1728. }
  1729. })
  1730. };
  1731. },
  1732.  
  1733. panel: function(content) {
  1734. return {
  1735. attributes: {
  1736. 'class': 'hid'
  1737. },
  1738. style: {
  1739. 'padding': '10px',
  1740. 'margin-top': '0.5em'
  1741. },
  1742. children: UI.prototype._def.panel(content)
  1743. };
  1744. }
  1745. },
  1746.  
  1747. toggle: function() {
  1748. UI.prototype.toggle.call(this);
  1749.  
  1750. (DH.hasClass(this.panel, 'hid') ? DH.delClass : DH.addClass).call(DH, this.panel, 'hid');
  1751. }
  1752. });
  1753.  
  1754. /**
  1755. * @class FeatherUI.Content
  1756. */
  1757. FeatherUI.Content = function(buttons) {
  1758. UI.Content.call(this, buttons);
  1759. };
  1760.  
  1761. FeatherUI.Content.prototype = extend(UI.Content, {
  1762. render: function() {
  1763. var nodes = UI.Content.prototype.render.call(this);
  1764.  
  1765. return map(function(node) {
  1766. DH.attributes(node, {
  1767. 'class': 'b'
  1768. });
  1769.  
  1770. DH.style(DH.walk(node, 'span[0]'), {
  1771. 'font-size': '11px'
  1772. });
  1773.  
  1774. DH.style(DH.walk(node, 'span[1]'), {
  1775. 'font-weight': 'bold'
  1776. });
  1777.  
  1778. return node;
  1779. }, nodes);
  1780. }
  1781. });
  1782.  
  1783. /*
  1784. * Ready callbacks.
  1785. */
  1786.  
  1787. function onReady(player) {
  1788. var
  1789. videoPlayback = new VideoPlayback(player),
  1790. videoQuality = new VideoQuality(player),
  1791. playerSize = new PlayerSize(player),
  1792. previousVideo = player.getVideoId();
  1793.  
  1794. player.onStateChange = function() {
  1795. try {
  1796. var currentVideo = player.getVideoId();
  1797.  
  1798. if (currentVideo == previousVideo) {
  1799. videoQuality.apply();
  1800. videoPlayback.apply();
  1801. }
  1802. else {
  1803. videoQuality.cease();
  1804. videoPlayback.cease();
  1805.  
  1806. throw null;
  1807. }
  1808. }
  1809. catch (e) {
  1810. player.invalidate();
  1811.  
  1812. asyncCall(onPlayerReady);
  1813. }
  1814. };
  1815.  
  1816. videoQuality.apply();
  1817. videoPlayback.apply();
  1818.  
  1819. if (Watch7UI.requirement.test()) {
  1820. playerSize.apply();
  1821.  
  1822. UI.initialize(Watch7UI, [
  1823. new VideoQuality.Button(videoQuality),
  1824. new PlayerSize.Button(playerSize),
  1825. new VideoPlayback.Button(videoPlayback)
  1826. ]);
  1827. }
  1828. else if (Watch8UI.requirement.test()) {
  1829. playerSize.apply();
  1830.  
  1831. UI.initialize(Watch8UI, [
  1832. new VideoQuality.Button(videoQuality),
  1833. new PlayerSize.Button(playerSize),
  1834. new VideoPlayback.Button(videoPlayback)
  1835. ]);
  1836. }
  1837. else if (ChannelUI.requirement.test()) {
  1838. UI.initialize(ChannelUI, [
  1839. new VideoQuality.Button(videoQuality),
  1840. new VideoPlayback.Button(videoPlayback)
  1841. ]);
  1842. }
  1843. else if (FeatherUI.requirement.test()) {
  1844. UI.initialize(FeatherUI, [
  1845. new VideoQuality.Button(videoQuality),
  1846. new VideoPlayback.Button(videoPlayback)
  1847. ]);
  1848. }
  1849. }
  1850.  
  1851. function onPlayerReady() {
  1852. try {
  1853. each(DH.query('video, embed'), function(i, node) {
  1854. var player = DH.closest(node, function(node) { return Player.test(DH.unwrap(node)); });
  1855.  
  1856. if (player) {
  1857. player = Player.initialize(DH.unwrap(player));
  1858.  
  1859. if (player.isVideoLoaded()) {
  1860. onReady(player);
  1861.  
  1862. throw 'Initialization finished';
  1863. }
  1864. else {
  1865. player.invalidate();
  1866. }
  1867. }
  1868. });
  1869.  
  1870. throw 'Player not found';
  1871. }
  1872. catch (e) {
  1873. Console.debug(e);
  1874. }
  1875. }
  1876.  
  1877. onPlayerReady();
  1878.  
  1879. // FIXME: The call to an exported function is rejected if a function (or an
  1880. // object with methods) is passed as argument.
  1881. //
  1882. // This restriction can be lifted in FF 33+ by setting the 'allowCallbacks'
  1883. // option when exporting the function.
  1884.  
  1885. var node = DH.build({
  1886. tag: 'script',
  1887. attributes: {
  1888. 'type': 'text/javascript'
  1889. }
  1890. });
  1891.  
  1892. node.text = 'function onYouTubePlayerReady() { ' + scriptContext.publish('onPlayerReady', intercept(unsafeWindow.onYouTubePlayerReady, asyncProxy(onPlayerReady))) + '(); }';
  1893.  
  1894. document.documentElement.appendChild(node);
  1895. document.documentElement.removeChild(node);
  1896.  
  1897. } // YAYS
  1898.  
  1899. if (window.top === window.self) {
  1900. if (this.unsafeWindow) { // Greasemonkey.
  1901. YAYS(unsafeWindow);
  1902. }
  1903. else {
  1904. var node = document.createElement('script');
  1905. node.setAttribute('type', 'text/javascript');
  1906. node.text = '(' + YAYS.toString() + ')(window);';
  1907.  
  1908. document.documentElement.appendChild(node);
  1909. document.documentElement.removeChild(node);
  1910. }
  1911. }