-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhtml5-youtube.compat.js
295 lines (254 loc) · 7.5 KB
/
html5-youtube.compat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*! @license html5-youtube.js by Ginpei https://github.com/ginpei/html5-youtube.js MIT License */
(function(window, document, Player) {
var f_slice = Array.prototype.slice;
// shortcut
var prototype = Player.prototype;
// ----------------------------------------------------------------
// Statics
/**
* Overwrite for compat.
*/
Player.bind = function(fn, context) {
var args = f_slice.call(arguments, 2);
return function() {
var curArgs = args.concat(f_slice.call(arguments));
return fn.apply(context, curArgs);
};
};
/**
* Overwrite for compat.
*/
Player._execDefineProperty = function(obj, prop, descriptor) {
obj[prop] = function(value) {
if (arguments.length > 0) {
descriptor.set.call(this, value);
return value;
}
else {
return descriptor.get.call(this);
}
};
};
/**
* Register a callback for initializing.
* @param {String} playerId
* @param {Player} player
*/
Player.registerYouTubePlayerReady = function(playerId, player) {
if (!window.onYouTubePlayerReady) {
window.onYouTubePlayerReady = this.bind(this.onYouTubePlayerReady, this);
this._callbacksForYouTubePlayerReady = {};
}
this._callbacksForYouTubePlayerReady[playerId] = player;
};
/**
* The unified callback for YouTube API Player.
* It is called by YouTube API when it is completly loaded.
* @param {String} playerId
* @see registerYouTubePlayerReady
*/
Player.onYouTubePlayerReady = function(playerId) {
// save the CSSStylesheet object
if (!this._style) {
this._style = document.styleSheets[document.styleSheets.length-1];
}
var player = this._callbacksForYouTubePlayerReady[playerId];
player.onYouTubePlayerReady(playerId);
};
// ----------------------------------------------------------------
// Constructing
/**
* Overwrite for compat.
*/
prototype._initializeEventer = function() {
// nothing to do
};
/**
* Overwrite for compat.
*/
prototype._clearEventer = function() {
// nothing to do
};
/**
* Overwrite for compat.
*/
prototype._buildPlayer = function(options) {
var videoOptions = this._getVideoOptions(options);
if (!videoOptions.videoId) {
videoOptions.videoId = 'xxxxxxxxxxx'; // load dummy video
}
if (!window.swfobject) {
throw new Error('swfobject is required. Use this code:\n<script src="//cdnjs.cloudflare.com/ajax/libs/swfobject/2.2/swfobject.js"></script>');
}
// set up ID
this._originalElemId = videoOptions.el.id;
var playerId = videoOptions.el.id = 'youtubejs' + Date.now();
// save the target element to restore when destroyed
this._elOriginal = videoOptions.el;
// callback
Player.registerYouTubePlayerReady(playerId, this);
// load
// `fs=0` is required because fullscreen button does not work (OMG)
var url = 'http://www.youtube.com/v/' + videoOptions.videoId +
'?wmode=opaque&fs=0&enablejsapi=1&version=3&playerapiid=' + playerId;
var params = { allowScriptAccess:'always', wmode:'transparent' };
var attr = { id:playerId };
swfobject.embedSWF(url, playerId, videoOptions.width, videoOptions.height, '8', null, null, params, attr);
};
prototype._destroyPlayer = function() {
var elPlayer = this.player;
// API's destroying
this.player.destroy();
this.player = null;
// restore the elements
var elOriginal = this._elOriginal;
if (this._originalElemId) {
elOriginal.id = this._originalElemId;
}
else {
elOriginal.removeAttribute('id');
}
this._elOriginal = null;
var elParent = elPlayer.parentNode;
elParent.insertBefore(elOriginal, elPlayer);
elParent.removeChild(elPlayer);
// clear style
this._clearYTStyle(elOriginal.id);
this._elOriginal = null;
};
/**
* Clear style rules that were specified by YouTube API.
* @param {String} id
*/
prototype._clearYTStyle = function(id) {
// API:
// elStyle
// └.sheet
// └.cssRules
// └[index]
// ├.selectorText
// └.style
// ├.length
// ├[index] as defined property's name
// └[type] as value
//
// example:
// var elStyle = document.querySelector('style');
// var stylesheet = elStyle.sheet;
// var rule = stylesheet.cssRules[0];
// var selector = rule.selectorText;
// var style = rule.style;
// var prop = style[0];
// var value = style['color'];
var targetSelector = '#' + id;
var stylesheet = Player._style;
var rules = stylesheet.cssRules || stylesheet.rules;
for (var i=0, l=rules.length; i<l; i++) {
var rule = rules[i];
var selector = rule.selectorText;
if (selector === targetSelector) {
if (stylesheet.deleteRule) {
stylesheet.deleteRule(i);
}
else {
stylesheet.removeRule(i);
}
break;
}
}
};
/**
* A callback that is called when YouTube API is ready.
* @param {String} playerId
* @see Player.registerYouTubePlayerReady
*/
prototype.onYouTubePlayerReady = function(playerId) {
var player = this.player = document.getElementById(playerId);
var prefix = playerId.replace(/-/g, '_');
this._registerVideoEvents(player, prefix);
var event = { type:'onReady', data:null, target:player };
this.onReady(event);
};
/**
* Register callbacks for YouTube APIs.
* @param {Player} player
* @param {String} prefix
* @see #_registerVideoEvent
*/
prototype._registerVideoEvents = function(player, prefix) {
this._registerVideoEvent(player, prefix, 'onApiChang');
this._registerVideoEvent(player, prefix, 'onError');
this._registerVideoEvent(player, prefix, 'onPlaybackQualityChange');
this._registerVideoEvent(player, prefix, 'onPlaybackRateChange');
this._registerVideoEvent(player, prefix, 'onReady');
this._registerVideoEvent(player, prefix, 'onStateChange');
};
/**
* Register a specified callback for YouTube APIs.
* @param {Player} player
* @param {String} prefix
* @param {String} type
*/
prototype._registerVideoEvent = function(player, prefix, type) {
var callbackName = prefix + type;
player.addEventListener(type, callbackName);
window[callbackName] = Player.bind(function(type, data) {
try { // error will be wasted by swf
var event = { type:type, data:data, target:this };
this[type](event);
}
catch (error) {
console.error(error);
}
}, this, type);
};
// ----------------------------------------------------------------
// Events
/**
* Overwrite for compat.
* @see https://github.com/ginpei/Osteoporosis.js/blob/ccf3380fef9f8fd850c44fa017ad863af2ddb9b7/osteoporosis.js#L36-L54
*/
prototype.addEventListener = function(type, listener) {
this._pushListener(type, listener);
};
/**
* Overwrite for compat.
*/
prototype.removeEventListener = function(type, listener) {
this._popListener(type, listener);
};
/**
* Overwrite for compat.
* @see https://github.com/ginpei/Osteoporosis.js/blob/ccf3380fef9f8fd850c44fa017ad863af2ddb9b7/osteoporosis.js#L56-L68
*/
prototype.trigger = function(type, originalEvent) {
var event;
// pick up listeners
var events = this._events[type];
if (!events) {
return;
}
// wrap specified event object
if (document.createEvent) {
event = document.createEvent('CustomEvent');
event.initEvent(type, false, true);
}
else {
event = document.createEventObject();
event.type = type;
}
event.player = this;
if (originalEvent) {
event.playerData = originalEvent.data;
event.originalEvent = originalEvent;
}
// execute triggering
for (var i=0, l=events.length; i<l; i++) {
var data = events[i];
if (data) {
data.binded(event);
}
}
return this;
};
})(window, document, window.youtube.Player);