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