youtube_playlist.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var run_playideos = (function () {
  2. var e, n, t, o, r, i = [].indexOf || function (e) {
  3. for (var n = 0, t = this.length; n < t; n++) {
  4. if (n in this && this[n] === e) return n
  5. }
  6. return -1
  7. };
  8. if (!window.console) {
  9. window.console = {
  10. log: function () {}
  11. }
  12. }
  13. n = {
  14. shadow: {
  15. "background-color": "black",
  16. position: "fixed",
  17. left: 0,
  18. top: 0,
  19. width: "100%",
  20. height: "100%",
  21. "z-index": 1e3,
  22. opacity: .8
  23. },
  24. player_box: {
  25. position: "fixed",
  26. left: "50%",
  27. top: "50%",
  28. width: 640,
  29. height: 480,
  30. "margin-left": -320,
  31. "margin-top": -240,
  32. "z-index": 1001
  33. },
  34. prev_button: {
  35. "float": "left"
  36. },
  37. next_button: {
  38. "float": "right"
  39. }
  40. };
  41. t = function (e, n) {
  42. var t, o, r;
  43. r = document.createElement("script");
  44. r.src = e;
  45. o = document.getElementsByTagName("head")[0];
  46. t = false;
  47. r.onload = r.onreadystatechange = function () {
  48. var e, i;
  49. e = !this.readyState || (i = this.readyState) === "loaded" || i === "complete";
  50. if (!t && e) {
  51. t = true;
  52. n();
  53. r.onload = r.onreadystatechange = null;
  54. return o.removeChild(r)
  55. }
  56. };
  57. return o.appendChild(r)
  58. };
  59. e = function (e) {
  60. var t, o, r, a, u, l, d, c, f, p, s, y, h, g, v, m, w;
  61. e.getScript("//www.youtube.com/iframe_api");
  62. d = [];
  63. w = new RegExp("https?://(www.)?youtube.com/");
  64. e('a[href^="http"]').each(function () {
  65. var n;
  66. if (!e(this).attr("href").match(w)) {
  67. return
  68. }
  69. n = this.href.replace(/^.*v=/, "").replace(/\&.*$/, "");
  70. if (i.call(d, n) < 0) {
  71. return d.push(n)
  72. }
  73. });
  74. console.log("video ids", d);
  75. c = 0;
  76. y = null;
  77. g = "playlist_player";
  78. f = function () {
  79. console.log("Playing", c, d[c]);
  80. return y.loadVideoById(d[c])
  81. };
  82. p = function () {
  83. c++;
  84. if (c >= d.length) {
  85. c -= d.length
  86. }
  87. return f()
  88. };
  89. s = function () {
  90. c--;
  91. if (c < 0) {
  92. c += d.length
  93. }
  94. return f()
  95. };
  96. l = function () {
  97. e("#shadow, #player_box").remove();
  98. return e(document).unbind("keyup.player")
  99. };
  100. e(document).bind("keyup.player", function (e) {
  101. if (e.keyCode === 27) {
  102. l()
  103. }
  104. if (e.keyCode === 39) {
  105. p()
  106. }
  107. if (e.keyCode === 37) {
  108. return s()
  109. }
  110. });
  111. u = e("<div />", {
  112. id: "shadow",
  113. css: n.shadow,
  114. click: l
  115. });
  116. r = e("<div />", {
  117. id: "player_box",
  118. css: n.player_box
  119. });
  120. o = e("<div />", {
  121. id: g
  122. });
  123. a = e("<a />", {
  124. href: "javascript:;",
  125. text: "previous",
  126. css: n.prev_button,
  127. click: s
  128. });
  129. t = e("<a />", {
  130. href: "javascript:;",
  131. text: "next",
  132. css: n.next_button,
  133. click: p
  134. });
  135. r.append(o).append(a).append(t);
  136. e("body").append(u).append(r);
  137. v = function (e) {
  138. console.log("player ready");
  139. return e.target.playVideo()
  140. };
  141. h = function (e) {
  142. var n, t;
  143. n = {
  144. 2: "invalid video id",
  145. 5: "video not supported in html5",
  146. 100: "video removed or private",
  147. 101: "video not embedable",
  148. 150: "video not embedable"
  149. };
  150. t = n[e.data] || "unknown error";
  151. console.log("Error", t);
  152. d.splice(c, 1);
  153. if (c >= d.length) {
  154. c = 0
  155. }
  156. return f()
  157. };
  158. m = function (e) {
  159. if (e.data === YT.PlayerState.ENDED) {
  160. return p()
  161. }
  162. };
  163. return window.onYouTubeIframeAPIReady = function () {
  164. return y = new YT.Player(g, {
  165. height: "390",
  166. width: "640",
  167. videoId: d[0],
  168. events: {
  169. onReady: v,
  170. onError: h,
  171. onStateChange: m
  172. }
  173. })
  174. }
  175. };
  176. o = false;
  177. if (typeof jQuery !== "undefined" && jQuery !== null && jQuery.fn && jQuery.fn.jquery) {
  178. r = jQuery.fn.jquery.split(".");
  179. if (r.length === 3 && parseInt(r[1]) > 3) {
  180. console.log("using in page jquery version", jQuery.fn.jquery);
  181. e(jQuery);
  182. o = true
  183. }
  184. }
  185. if (!o) {
  186. t("plugins/playvideos/jquery-1.11.2.min.js", function () {
  187. return e(jQuery.noConflict(true))
  188. })
  189. }
  190. });
  191. var input = document.querySelector('#playvideos');
  192. input.addEventListener('click', function()
  193. {
  194. run_playideos();
  195. });