-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoembed.js
312 lines (262 loc) · 8.92 KB
/
autoembed.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function AutoEmbed() {
AE_TAG = '<!-- Generated by AutoEmbed-JS (http://autoembed.com) -->';
var self = this;
self._media_id = null;
self._stub = null;
self._object_attribs = null;
self._object_params = null;
/**
* Parse given URL
*
* @param $url string - href to check for embeded video
*
* @return boolean - whether or not the url contains valid/supported video
*/
self.parseUrl = function (url) {
for (var stubKey in AutoEmbed_stubs) {
var stub = AutoEmbed_stubs[stubKey];
var regExp = new RegExp(stub['url-match'], "im");
var match = url.match(regExp);
if (match) {
self._stub = stub;
if (stub['fetch-match']) {
return _parseLink(url);
} else {
self._media_id = match;
_setDefaultParams();
return true;
}
}
}
delete(stub);
return false;
};
/**
* Returns info about the stub
*
* @param string $property - (optional) the specific
* property of the stub to be returned. If
* ommited, array of all properties are returned
*
* @return mixed - details about the stub
*/
this.getStub = function (property) {
if (typeof property === "undefined") {
property = null;
}
return property ? self._stub[property] : self._stub;
};
/**
* Return object params about the video metadata
*
* @return array - object params
*/
this.getObjectParams = function () {
return self._object_params;
};
/**
* Return object attribute
*
* @return array - object attribute
*/
this.getObjectAttribs = function () {
return self._object_attribs;
};
/**
* Convert the url to an embedable tag
*
* return string - the embed html
*/
this.getEmbedCode = function () {
if (self._stub['iframe-player']) {
return _buildiFrame();
}
return _buildObject();
};
/**
* Return a thumbnail for the embeded video
*
* return string - the thumbnail href
*/
this.getImageURL = function () {
if (!self._stub['image-src']) return false;
var thumb = self._stub['image-src'];
for (var i = 1; i <= self._media_id.length; i++) {
thumb = thumb.replace('$' + i, self._media_id[i - 1]);
}
return thumb;
};
/**
* Set the height of the object
*
* @param mixed - height to set the object to
*
* @return boolean - true if the value was set, false
* if parseURL hasn't been called yet
*/
this.setHeight = function (height) {
return this.setObjectAttrib('height', height);
};
/**
* Set the width of the object
*
* @param mixed - width to set the object to
*
* @return boolean - true if the value was set, false
* if parseURL hasn't been called yet
*/
this.setWidth = function (width) {
return this.setObjectAttrib('width', width);
};
/**
* Override a default param value for both the object
* and flash param list
*
* @param $param mixed - the name of the param to be set
* or an array of multiple params to set
* @param $value string - (optional) the value to set the param to
* if only one param is being set
*
* @return boolean - true if the value was set, false
* if parseURL hasn't been called yet
*/
this.setParam = function (param, value) {
if (typeof value === "undefined") {
value = null;
}
return this.setObjectParam(param, value);
};
/**
* Override a default object param value
*
* @param $param mixed - the name of the param to be set
* or an array of multiple params to set
* @param $value string - (optional) the value to set the param to
* if only one param is being set
*
* @return boolean - true if the value was set, false
* if parseURL hasn't been called yet
*/
this.setObjectParam = function (param, value) {
if (typeof value === "undefined") {
value = null;
}
if (self._object_params instanceof Array) return false;
if (param instanceof Array) {
for (var p in param) {
self._object_params[p] = param[p];
}
} else {
self._object_params[param] = value;
}
return true;
};
/**
* Override a default object attribute value
*
* @param $param mixed - the name of the attribute to be set
* or an array of multiple attribs to be set
* @param $value string - (optional) the value to set the param to
* if only one param is being set
*
* @return boolean - true if the value was set, false
* if parseURL hasn't been called yet
*/
this.setObjectAttrib = function (param, value) {
if (typeof value === "undefined") {
value = null;
}
if (self._object_attribs instanceof Array) {
return false;
}
if (param instanceof Array) {
for (var p in param) {
self._object_attribs[p] = param[p];
}
} else {
self._object_attribs[param] = value;
}
return true;
};
/**
* Attempt to parse the embed id from a given URL
*/
function _parseLink(url) {
//FIXME: This is a file read operation. Need to figure out how to achieve this in javascript
//API doc: http://in1.php.net/manual/en/function.file-get-contents.php
//var source = file_get_contents(url).replace('/[^(\x20-\x7F)]*/', '');
var source = url.replace('/[^(\x20-\x7F)]*/', '');
var regExp = new RegExp(self._stub['fetch-match'], "im");
var match = source.match(regExp);
if (match) {
self._media_id = match;
_setDefaultParams();
return true;
}
return false;
}
/**
* Build a generic object skeleton
*/
function _buildObject() {
// console.log("--- _buildObject() call --- ")
var object_attribs = '';
var object_params = '';
for (var param in self._object_attribs) {
var value = self._object_attribs[param];
object_attribs += ' ' + param + '="' + value + '"';
}
for (var param in self._object_params) {
var value = self._object_params[param];
object_params += '<param name="' + param + '" value="' + value + '" />';
}
str = "<object " + object_attribs + "> " + object_params + " " + AutoEmbed.AE_TAG + "</object>"
for (var i = 1; i <= self._media_id.length; i++) {
str = str.replace('$' + i, self._media_id[i - 1]);
}
return str;
}
/**
* Build an iFrame player
*/
function _buildiFrame() {
// console.log("--- _buildiFrame() call --- ")
var source = self._stub['iframe-player'];
for (var i = 1; i <= self._media_id.length; i++) {
source = source.replace('$' + i, self._media_id[i - 1]);
}
var width = self._object_attribs['width'];
var height = self._object_attribs['height'];
return '<iframe type="text/html" width="' + width + '" height="' + height + '" src="' + source + '" frameborder="0"></iframe>';
}
/**
* Set the default params for the type of
* stub we are working with
*/
function _setDefaultParams() {
var source = self._stub['embed-src'];
var flashvars = self._stub['flashvars'] ? self._stub['flashvars'] : null;
for (var i = 1; i <= self._media_id.length; i++) {
source = source.replace(new RegExp('$' + i, 'gim'), self._media_id[i - 1]);
flashvars = flashvars == null ? null : flashvars.replace(new RegExp('$' + i, 'gim'), self._media_id[i - 1]);
}
source = htmlspecialchars(source);
flashvars = flashvars == null ? null : htmlspecialchars(flashvars);
self._object_params = {
'movie': source,
'quality': 'high',
'allowFullScreen': 'true',
'allowScriptAccess': 'always',
'pluginspage': 'http://www.macromedia.com/go/getflashplayer',
'autoplay': 'false',
'autostart': 'false',
'flashvars': flashvars,
};
self._object_attribs = {
'type': 'application/x-shockwave-flash',
'data': source,
'width': self._stub['embed-width'],
'height': self._stub['embed-height']
};
}
}