-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-loader-nice.js
340 lines (340 loc) · 10.6 KB
/
youtube-loader-nice.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict';
function YouTubeToHtml5(options = {}) {
this.hooks = {};
this.options = {};
var property;
for (property in this.defaultOptions) {
if (property in options) {
this.options[property] = options[property];
} else {
this.options[property] = this.defaultOptions[property];
}
}
if (this.options.autoload) {
this.load();
}
}
YouTubeToHtml5.prototype.defaultOptions = {
selector : "video[data-yt2html5]",
attribute : "data-yt2html5",
formats : "*",
autoload : true,
withAudio : false
};
YouTubeToHtml5.prototype.globalHooks = {};
YouTubeToHtml5.prototype.getHooks = function(key, type) {
let hooks = [];
if (key in this.globalHooks) {
let elem = this.globalHooks[key];
elem = elem.filter((a) => {
return a.name === type;
});
elem = elem.sort((secondListenerDetails, firstListenerDetails) => {
return secondListenerDetails.priority - firstListenerDetails.priority;
});
hooks = hooks.concat(elem);
}
if (key in this.hooks) {
let elem = this.hooks[key];
elem = elem.filter((a) => {
return a.name === type;
});
elem = elem.sort((secondListenerDetails, firstListenerDetails) => {
return secondListenerDetails.priority - firstListenerDetails.priority;
});
hooks = hooks.concat(elem);
}
return hooks;
};
YouTubeToHtml5.prototype.addHook = function(key, value) {
if (!(key in this.globalHooks)) {
this.globalHooks[key] = [];
}
if (!(key in this.hooks)) {
this.hooks[key] = [];
}
if ("global" in value && value.global) {
this.globalHooks[key].push(value);
} else {
this.hooks[key].push(value);
}
};
YouTubeToHtml5.prototype.addAction = function(name, handler, priority = 10, showSpinner = false) {
this.addHook("actions", {
name : name,
callback : handler,
priority : priority,
global : showSpinner
});
};
YouTubeToHtml5.prototype.doAction = function(type, ...args) {
const _intervals = this.getHooks("actions", type);
for (let i = 0; i < _intervals.length; i++) {
_intervals[i].callback(...args);
}
};
YouTubeToHtml5.prototype.addFilter = function(options, callback, priority = 10, showSpinner = false) {
this.addHook("filters", {
name : options,
callback : callback,
priority : priority,
global : showSpinner
});
};
YouTubeToHtml5.prototype.applyFilters = function(key, val, ...value) {
const _intervals = this.getHooks("filters", key);
for (let i = 0; i < _intervals.length; i++) {
val = _intervals[i].callback(val, ...value);
}
return val;
};
YouTubeToHtml5.prototype.itagMap = {
18 : "360p",
22 : "720p",
37 : "1080p",
38 : "3072p",
82 : "360p3d",
83 : "480p3d",
84 : "720p3d",
85 : "1080p3d",
133 : "240pna",
134 : "360pna",
135 : "480pna",
136 : "720pna",
137 : "1080pna",
264 : "1440pna",
298 : "720p60",
299 : "1080p60na",
160 : "144pna",
139 : "48kbps",
140 : "128kbps",
141 : "256kbps"
};
YouTubeToHtml5.prototype.urlToId = function(colorTxt) {
const regex = /^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|(?:(?:youtube-nocookie\.com\/|youtube\.com\/)(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/)))([a-zA-Z0-9\-_]*)/;
const processNameMatches = colorTxt.match(regex);
return Array.isArray(processNameMatches) && processNameMatches[1] ? processNameMatches[1] : colorTxt;
};
YouTubeToHtml5.prototype.fetch = function(url) {
return new Promise((OnSuccess, notify_success) => {
var xhr = new XMLHttpRequest;
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
OnSuccess(this.responseText);
} else {
notify_success(this);
}
}
};
xhr.send();
xhr = null;
});
};
YouTubeToHtml5.prototype.getAllowedFormats = function() {
let formats = [];
if (Array.isArray(this.options.formats)) {
formats = this.options.formats;
} else {
if (this.itagMap[this.options.formats]) {
formats = [this.options.formats];
} else {
if (this.options.formats === "*") {
formats = Object.values(this.itagMap).sort();
}
}
}
return formats;
};
YouTubeToHtml5.prototype.getElements = function(value) {
var args = null;
if (value) {
if (NodeList.prototype.isPrototypeOf(value) || HTMLCollection.prototype.isPrototypeOf(value)) {
args = value;
} else {
if (typeof value === "object" && "nodeType" in value && value.nodeType) {
args = [value];
} else {
args = document.querySelectorAll(this.options.selector);
}
}
}
args = Array.from(args || "");
return this.applyFilters("elements", args);
};
YouTubeToHtml5.prototype.youtubeDataApiEndpoint = function(value) {
const d = `https://yt2html5.com/?id=${value}`;
return this.applyFilters("api.endpoint", d, value, null);
};
YouTubeToHtml5.prototype.parseUriString = function(string) {
return string.split("&").reduce(function(result, clusterShardData) {
const arrMatch = clusterShardData.split("=").map(function(originalBaseURL) {
return decodeURIComponent(originalBaseURL.replace("+", " "));
});
result[arrMatch[0]] = arrMatch[1];
return result;
}, {});
};
YouTubeToHtml5.prototype.canPlayType = function(type) {
var elem = null;
if (/^audio/i.test(type)) {
elem = document.createElement("audio");
} else {
elem = document.createElement("video");
}
const dashicon = elem && typeof elem.canPlayType === "function" ? elem.canPlayType(type) : "unknown";
return dashicon ? dashicon : "no";
};
YouTubeToHtml5.prototype.parseYoutubeMeta = function(value) {
let $ = [];
let data = [];
if (typeof value === "string") {
try {
value = JSON.parse(value);
} catch (error) {
return null;
}
}
let obj = value.data || {};
obj = this.applyFilters("api.response", obj, value);
if (obj.hasOwnProperty("url_encoded_fmt_stream_map")) {
$ = $.concat(obj.url_encoded_fmt_stream_map.split(",").map((markup_start) => {
return this.parseUriString(markup_start);
}));
}
if (obj.player_response.streamingData && obj.player_response.streamingData.formats) {
$ = $.concat(obj.player_response.streamingData.formats);
}
if (obj.hasOwnProperty("adaptive_fmts")) {
$ = $.concat(obj.adaptive_fmts.split(",").map((markup_start) => {
return this.parseUriString(markup_start);
}));
}
if (obj.player_response.streamingData && obj.player_response.streamingData.adaptiveFormats) {
$ = $.concat(obj.player_response.streamingData.adaptiveFormats);
}
$.forEach((data) => {
if (data && "itag" in data && this.itagMap[data.itag]) {
let self = {
_raw : data,
itag : data.itag,
url : null,
label : null,
type : "unknown",
mime : "unknown",
hasAudio : false,
browserSupport : "unknown"
};
if ("url" in data && data.url) {
self.url = data.url;
} else {
if ("signatureCipher" in data) {
}
}
if ("audioQuality" in data && data.audioQuality) {
self.hasAudio = true;
}
if ("qualityLabel" in data && data.qualityLabel) {
self.label = data.qualityLabel;
} else {
self.label = this.itagMap[data.itag];
}
if ("mimeType" in data) {
const STATIC_PAIR = data.mimeType.match(/^(audio|video)(?:\/([^;]+);)?/i);
if (STATIC_PAIR[1]) {
self.type = STATIC_PAIR[1];
}
if (STATIC_PAIR[2]) {
self.mime = STATIC_PAIR[2];
}
self.browserSupport = this.canPlayType(`${self.type}/${self.mime}`);
}
if (self.url) {
data.push(self);
}
}
});
data = this.applyFilters("api.results", data, obj);
return data;
};
YouTubeToHtml5.prototype.load = function() {
const directiveAttributes = this.getElements(this.options.selector);
if (directiveAttributes && directiveAttributes.length) {
directiveAttributes.forEach((childCompute) => {
this.loadSingle(childCompute);
});
}
};
YouTubeToHtml5.prototype.loadSingle = function(value, isScrollList = null) {
const dateFormatTag = isScrollList || this.options.attribute;
if (value.getAttribute(dateFormatTag)) {
const realVal = this.urlToId(value.getAttribute(dateFormatTag));
const url = this.youtubeDataApiEndpoint(realVal);
this.doAction("api.before", value);
this.fetch(url).then((childCompute) => {
if (childCompute) {
let rows = this.parseYoutubeMeta(childCompute);
if (rows && Array.isArray(rows)) {
rows = rows.filter(function(verifiedEvent) {
return verifiedEvent.type === value.tagName.toLowerCase();
});
rows.sort(function(metadata, options) {
const datum = {
"unknown" : -1,
"no" : -1,
"maybe" : 0,
"probably" : 1
};
return datum[metadata.browserSupport] + datum[options.browserSupport];
});
if (this.options.withAudio) {
rows = rows.filter(function(mi) {
return mi.hasAudio;
});
}
const keywordResults = this.getAllowedFormats();
var data = null;
var height = null;
for (let i = 0; i < keywordResults.length; i++) {
const _expandHeight = keywordResults[i];
const search = rows.filter((a) => {
return this.itagMap[a.itag] === _expandHeight;
});
if (search && search.length) {
data = search.shift();
height = _expandHeight;
break;
}
}
data = this.applyFilters("video.stream", data, value, height, rows);
let item = {
src : "",
type : ""
};
if (data && "url" in data && data.url) {
item.src = data.url;
}
if (data.type && data.type !== "unknown" && data.mime && data.mime !== "unknown") {
item.type = `${data.type}/${data.mime}`;
}
item.src = this.applyFilters("video.source", item.src, data, value, height, rows);
if (item.src && typeof item.src.toString === "function" && item.src.toString().length) {
value.src = item.src;
if (item.type && item.type.length) {
value.type = item.type;
}
} else {
console.warn(`YouTubeToHtml5 unable to load video for ID: ${realVal}`);
}
}
}
}).finally((obj) => {
this.doAction("api.after", value, obj);
});
}
};
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = YouTubeToHtml5;
}
;