forked from hoyois/plugin-to-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTubeXHR.js
274 lines (244 loc) · 9.24 KB
/
YouTubeXHR.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
if(window.safari) {
// SITE-SPECIFIC HACK for ClickToPlugin
// Prevents YouTube from removing the Flash player and disables SPF
var script = "\
var s = document.createElement('script');\
s.textContent = 'window.ytplayer=window.ytplayer||{};ytplayer.config=ytplayer.config||{};Object.defineProperty(ytplayer.config,\"min_version\",{\"value\":\"0.0.0\"});window.ytspf=window.ytspf||{};Object.defineProperty(ytspf,\"enabled\",{\"value\":false});';\
document.head.appendChild(s);";
safari.extension.addContentScript(script, ["http://www.youtube.com/*", "https://www.youtube.com/*"], [], true);
}
addKiller("YouTube", {
"playlistFilter": /^UL|^PL|^SP|^AL/,
"canKill": function(data) {
if(/^https?:\/\/s\.ytimg\.com\//.test(data.src)) return true;
if(/^https?:\/\/(?:www\.)?youtube(?:-nocookie|\.googleapis)?\.com\/[vpe]\//.test(data.src)) {data.embed = true; return true;}
return false;
},
"process": function(data, callback) {
var videoID, playlistID, startTime;
var onsite = /^https?:\/\/www\.youtube\.com\/watch\?/.test(data.location);
if(data.embed) { // old-style YT embed
var match = /\.com\/([vpe])\/+([^&?]+)/.exec(data.src);
if(match) {
if(match[1] === "p") playlistID = "PL" + match[2];
else videoID = match[2];
} else return;
match = /[?&]start=([\d]+)/.exec(data.src);
if(match) startTime = parseInt(match[1]);
} else {
var flashvars = parseFlashVariables(data.params.flashvars);
videoID = flashvars.video_id;
if(!videoID) return;
if(this.playlistFilter.test(flashvars.list)) playlistID = flashvars.list;
if(onsite) {
var match = /[#&?]t=(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?/.exec(data.location);
if(match) {
var hours = parseInt(match[1]) || 0;
var minutes = parseInt(match[2]) || 0;
var seconds = parseInt(match[3]) || 0;
startTime = 3600 * hours + 60 * minutes + seconds;
}
} else startTime = parseInt(flashvars.start);
}
var _this = this;
var mainCallback = function(mediaData) {
mediaData.startTime = startTime;
if(onsite) {
mediaData.initScript = _this.initScript;
mediaData.restoreScript = _this.restoreScript;
}
callback(mediaData);
};
if(playlistID) this.processPlaylist(playlistID, videoID, mainCallback, callback);
else if(videoID) this.processVideoID(videoID, !onsite, mainCallback);
},
"processVideoID": function(videoID, isEmbed, callback) {
var _this = this;
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.youtube.com/get_video_info?&video_id=" + videoID + "&eurl=http%3A%2F%2Fwww%2Eyoutube%2Ecom%2F&sts=1588", true);
xhr.addEventListener("load", function() {
var flashvars = parseFlashVariables(xhr.responseText);
if(flashvars.status === "ok") {
_this.processFlashVars(flashvars, isEmbed ? function(mediaData) {
mediaData.playlist[0].siteInfo = {"name": "YouTube", "url": "http://www.youtube.com/watch?v=" + videoID};
callback(mediaData);
} : callback);
} else { // e.g. region-blocked video
callback({"playlist": [null]});
}
}, false);
xhr.send(null);
},
"processFlashVars": function(flashvars, callback) {
if(flashvars.ps === "live" && !flashvars.hlsvp) return;
var sources = [];
// Get video URLs
if(flashvars.url_encoded_fmt_stream_map) {
var path;
// Get 240p, 360p, and 720p
var fmtList = decodeURIComponent(flashvars.url_encoded_fmt_stream_map).split(",");
var fmt, source;
for(var i = 0; i < fmtList.length; i++) {
fmt = parseFlashVariables(fmtList[i]);
if(!fmt.url) continue;
if(fmt.itag === "22") {
source = {"format": "720p MP4", "height": 720, "isNative": true};
} else if(fmt.itag === "18") {
path = decodeURIComponent(fmt.url.substring(0, fmt.url.indexOf("%3F"))).replace(/^https/, "http");
source = {"format": "360p MP4", "height": 360, "isNative": true};
} else if(canPlayFLV && fmt.itag === "5") {
source = {"format": "240p FLV", "height": 240, "isNative": false};
} else continue;
source.url = decodeURIComponent(fmt.url).replace(/^https/, "http") + "&title=" + flashvars.title + "%20%5B" + source.height + "p%5D";
if(fmt.sig) source.url += "&signature=" + fmt.sig;
else if(fmt.s) source.url += "&signature=" + this.decodeSignature(fmt.s);
sources.push(source);
}
} else if(flashvars.hlsvp) {
sources.push({"url": decodeURIComponent(flashvars.hlsvp), "format": "M3U8", "isNative": true});
}
var poster, title;
if(flashvars.iurlmaxres) poster = decodeURIComponent(flashvars.iurlmaxres);
else if(flashvars.iurlsd) poster = decodeURIComponent(flashvars.iurlsd);
else poster = "https://i.ytimg.com/vi/" + flashvars.video_id + "/hqdefault.jpg";
if(flashvars.title) title = decodeURIComponent(flashvars.title.replace(/\+/g, " "));
sources.sort(function(s, t) {
return s.height < t.height ? 1 : -1;
});
callback({
"playlist": [{
"title": title,
"poster": poster,
"sources": sources
}]
});
},
"decodeSignature": function(s) {
s = s.split("");
s = s.slice(2);
s = s.reverse();
s = s.slice(3);
var t = s[0];
s[0] = s[19%s.length];
s[19] = t;
s = s.reverse();
return s.join("");
},
"processPlaylist": function(playlistID, videoID, mainCallback, callback) {
var videoIDList = [];
var _this = this;
var loadAPIList = function(playlistURL, startIndex) {
var xhr = new XMLHttpRequest();
xhr.open("GET", playlistURL + "?start-index=" + startIndex + "&max-results=50", true);
xhr.addEventListener("load", function() {
if(xhr.status === 200) {
var entries = xhr.responseXML.getElementsByTagName("entry");
for(var i = 0; i < entries.length; i++) {
try{ // being lazy
videoIDList.unshift(/\?v=([^&?']+)/.exec(entries[i].getElementsByTagNameNS("http://search.yahoo.com/mrss/", "player")[0].getAttribute("url"))[1]);
} catch(e) {}
}
if(xhr.responseXML.querySelector("link[rel='next']") === null) processList();
else loadAPIList(playlistURL, startIndex + 50);
} else if(videoID) _this.processVideoID(videoID, false, mainCallback);
}, false);
xhr.send(null);
};
var loadPlaylist = function(page) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.youtube.com/playlist?list=" + playlistID + "&page=" + page, true);
xhr.addEventListener("load", function() {
if(xhr.status === 200) {
var regex = /class=\"video-title-container\">\s*<a href=\"\/watch\?v=([^&]*)/g;
var match;
while(match = regex.exec(xhr.responseText)) {
videoIDList.push(match[1]);
}
if(videoIDList.length < 100 * page) processList();
else loadPlaylist(page + 1);
} else if(videoID) _this.processVideoID(videoID, false, mainCallback);
}, false);
xhr.send(null);
};
var processList = function() {
var track = 0;
var length = videoIDList.length;
if(videoID) { // shift list so that videoID is first
while(videoIDList[0] !== videoID && track < length) {
++track;
videoIDList.push(videoIDList.shift());
}
if(track === length) {
videoIDList.unshift(videoID);
++length;
track = 0;
}
}
var callbackForPlaylist = function(mediaData) {
mediaData.playlistLength = length;
mediaData.startTrack = track;
mainCallback(mediaData);
};
// load the first video at once
_this.processVideoID(videoIDList[0], !videoID, callbackForPlaylist);
videoIDList.shift();
unloadList();
};
var unloadList = function() {
if(videoIDList.length === 0) return;
var i = 0;
var imax = videoIDList.length;
if(imax > 3) imax = 3; // load by groups of 3
var mediaData = {"loadAfter": true, "playlist": []};
var next = function(data) {
mediaData.playlist.push(data.playlist[0]);
++i;
if(i === imax) {
callback(mediaData);
unloadList();
} else _this.processVideoID(videoIDList.shift(), true, next);
};
_this.processVideoID(videoIDList.shift(), true, next);
};
if(/^UL/.test(playlistID)) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.youtube.com/watch?&v=" + playlistID.substring(2), true);
xhr.addEventListener("load", function() {
var match = /https?:\/\/www\.youtube\.com\/user\/([^"]*)/.exec(xhr.responseText);
if(match) loadAPIList("https://gdata.youtube.com/feeds/api/users/" + match[1] + "/uploads", 1, true);
else if(videoID) _this.processVideoID(videoID, false, mainCallback);
}, false);
xhr.send(null);
} else loadPlaylist(1);
},
"initScript": "\
try{\
var _this = this;\
var seekTo = function(time) {\
var seek = function() {\
_this.removeEventListener(\"loadeddata\", seek, false);\
_this.currentTime = time;\
_this.play();\
};\
if(_this.readyState >= _this.HAVE_CURRENT_DATA) {\
_this.pause();\
seek();\
} else {\
_this.preload = \"auto\";\
_this.addEventListener(\"loadeddata\", seek, false);\
}\
_this.parentNode.focus();\
};\
window.yt = window.yt || {}; yt.www = yt.www || {}; yt.www.watch = yt.www.watch || {}; yt.www.watch.player = yt.www.watch.player || {};\
yt.www.watch.player.flashSeekTo = yt.www.watch.player.seekTo;\
Object.defineProperty(yt.www.watch.player, \"seekTo\", {\"get\": function() {return seekTo;}, \"set\": function(x) {yt.www.watch.player.flashSeekTo = x;}, \"configurable\": false, \"enumerable\": false});\
} catch(e) {}",
"restoreScript": "\
try{\
var player = {\"seekTo\": yt.www.watch.player.flashSeekTo};\
for(var e in yt.www.watch.player) {\
if(e !== \"flashSeekTo\") player[e] = yt.www.watch.player[e];\
}\
yt.www.watch.player = player;\
} catch(e) {}"
});