youtube_playlist.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** @licstart The following is the entire license notice for the
  2. * JavaScript code in this page.
  3. *
  4. * Copyright: (c) 2011-2015 Sébastien SAUVAGE <sebsauvage@sebsauvage.net>
  5. * (c) 2011-2017 The Shaarli Community, see AUTHORS
  6. *
  7. * This software is provided 'as-is', without any express or implied warranty.
  8. * In no event will the authors be held liable for any damages arising from
  9. * the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would
  18. * be appreciated but is not required.
  19. *
  20. * 2. Altered source versions must be plainly marked as such, and must
  21. * not be misrepresented as being the original software.
  22. *
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this page.
  27. */
  28. var run_playideos = (function () {
  29. var e, n, t, o, r, i = [].indexOf || function (e) {
  30. for (var n = 0, t = this.length; n < t; n++) {
  31. if (n in this && this[n] === e) return n
  32. }
  33. return -1
  34. };
  35. if (!window.console) {
  36. window.console = {
  37. log: function () {}
  38. }
  39. }
  40. n = {
  41. shadow: {
  42. "background-color": "black",
  43. position: "fixed",
  44. left: 0,
  45. top: 0,
  46. width: "100%",
  47. height: "100%",
  48. "z-index": 1e3,
  49. opacity: .8
  50. },
  51. player_box: {
  52. position: "fixed",
  53. left: "50%",
  54. top: "50%",
  55. width: 640,
  56. height: 480,
  57. "margin-left": -320,
  58. "margin-top": -240,
  59. "z-index": 1001
  60. },
  61. prev_button: {
  62. "float": "left"
  63. },
  64. next_button: {
  65. "float": "right"
  66. }
  67. };
  68. t = function (e, n) {
  69. var t, o, r;
  70. r = document.createElement("script");
  71. r.src = e;
  72. o = document.getElementsByTagName("head")[0];
  73. t = false;
  74. r.onload = r.onreadystatechange = function () {
  75. var e, i;
  76. e = !this.readyState || (i = this.readyState) === "loaded" || i === "complete";
  77. if (!t && e) {
  78. t = true;
  79. n();
  80. r.onload = r.onreadystatechange = null;
  81. return o.removeChild(r)
  82. }
  83. };
  84. return o.appendChild(r)
  85. };
  86. e = function (e) {
  87. var t, o, r, a, u, l, d, c, f, p, s, y, h, g, v, m, w;
  88. e.getScript("//www.youtube.com/iframe_api");
  89. d = [];
  90. w = new RegExp("https?://(www.)?youtube.com/");
  91. e('a[href^="http"]').each(function () {
  92. var n;
  93. if (!e(this).attr("href").match(w)) {
  94. return
  95. }
  96. n = this.href.replace(/^.*v=/, "").replace(/\&.*$/, "");
  97. if (i.call(d, n) < 0) {
  98. return d.push(n)
  99. }
  100. });
  101. console.log("video ids", d);
  102. c = 0;
  103. y = null;
  104. g = "playlist_player";
  105. f = function () {
  106. console.log("Playing", c, d[c]);
  107. return y.loadVideoById(d[c])
  108. };
  109. p = function () {
  110. c++;
  111. if (c >= d.length) {
  112. c -= d.length
  113. }
  114. return f()
  115. };
  116. s = function () {
  117. c--;
  118. if (c < 0) {
  119. c += d.length
  120. }
  121. return f()
  122. };
  123. l = function () {
  124. e("#shadow, #player_box").remove();
  125. return e(document).unbind("keyup.player")
  126. };
  127. e(document).bind("keyup.player", function (e) {
  128. if (e.keyCode === 27) {
  129. l()
  130. }
  131. if (e.keyCode === 39) {
  132. p()
  133. }
  134. if (e.keyCode === 37) {
  135. return s()
  136. }
  137. });
  138. u = e("<div />", {
  139. id: "shadow",
  140. css: n.shadow,
  141. click: l
  142. });
  143. r = e("<div />", {
  144. id: "player_box",
  145. css: n.player_box
  146. });
  147. o = e("<div />", {
  148. id: g
  149. });
  150. a = e("<a />", {
  151. href: "javascript:;",
  152. text: "previous",
  153. css: n.prev_button,
  154. click: s
  155. });
  156. t = e("<a />", {
  157. href: "javascript:;",
  158. text: "next",
  159. css: n.next_button,
  160. click: p
  161. });
  162. r.append(o).append(a).append(t);
  163. e("body").append(u).append(r);
  164. v = function (e) {
  165. console.log("player ready");
  166. return e.target.playVideo()
  167. };
  168. h = function (e) {
  169. var n, t;
  170. n = {
  171. 2: "invalid video id",
  172. 5: "video not supported in html5",
  173. 100: "video removed or private",
  174. 101: "video not embedable",
  175. 150: "video not embedable"
  176. };
  177. t = n[e.data] || "unknown error";
  178. console.log("Error", t);
  179. d.splice(c, 1);
  180. if (c >= d.length) {
  181. c = 0
  182. }
  183. return f()
  184. };
  185. m = function (e) {
  186. if (e.data === YT.PlayerState.ENDED) {
  187. return p()
  188. }
  189. };
  190. return window.onYouTubeIframeAPIReady = function () {
  191. return y = new YT.Player(g, {
  192. height: "390",
  193. width: "640",
  194. videoId: d[0],
  195. events: {
  196. onReady: v,
  197. onError: h,
  198. onStateChange: m
  199. }
  200. })
  201. }
  202. };
  203. o = false;
  204. if (typeof jQuery !== "undefined" && jQuery !== null && jQuery.fn && jQuery.fn.jquery) {
  205. r = jQuery.fn.jquery.split(".");
  206. if (r.length === 3 && parseInt(r[1]) > 3) {
  207. console.log("using in page jquery version", jQuery.fn.jquery);
  208. e(jQuery);
  209. o = true
  210. }
  211. }
  212. if (!o) {
  213. t("plugins/playvideos/jquery-1.11.2.min.js", function () {
  214. return e(jQuery.noConflict(true))
  215. })
  216. }
  217. });
  218. var input = document.querySelector('#playvideos');
  219. input.addEventListener('click', function()
  220. {
  221. run_playideos();
  222. });