This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathYoutubeProtocolHandler.js
132 lines (112 loc) · 4.25 KB
/
YoutubeProtocolHandler.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
return Cc["@mozilla.org/childprocessmessagemanager;1"]
.getService(Ci.nsIMessageSender);
});
// Splits parameters in a query string.
function extractParameters(aQuery) {
let params = aQuery.split("&");
let res = {};
params.forEach(function(aParam) {
let obj = aParam.split("=");
res[obj[0]] = decodeURIComponent(obj[1]);
});
return res;
}
function YoutubeProtocolHandler() {
}
YoutubeProtocolHandler.prototype = {
classID: Components.ID("{c3f1b945-7e71-49c8-95c7-5ae9cc9e2bad}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
scheme: "vnd.youtube",
defaultPort: -1,
protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE |
Ci.nsIProtocolHandler.URI_NOAUTH |
Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
// Sample URL:
// vnd.youtube:iNuKL2Gy_QM?vndapp=youtube_mobile&vndclient=mv-google&vndel=watch&vnddnc=1
newURI: function yt_phNewURI(aSpec, aOriginCharset, aBaseURI) {
let uri = Cc["@mozilla.org/network/standard-url;1"]
.createInstance(Ci.nsIStandardURL);
let id = aSpec.substring(this.scheme.length + 1);
id = id.substring(0, id.indexOf('?'));
uri.init(Ci.nsIStandardURL.URLTYPE_STANDARD, -1, this.scheme + "://dummy_host/" + id, aOriginCharset,
aBaseURI);
return uri.QueryInterface(Ci.nsIURI);
},
newChannel: function yt_phNewChannel(aURI) {
// Get a list of streams for this video id.
let infoURI = "http://www.youtube.com/get_video_info?&video_id=" +
aURI.path.substring(1);
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Ci.nsIXMLHttpRequest);
xhr.open("GET", infoURI, true);
xhr.addEventListener("load", function() {
// Youtube sends the response as a double wrapped url answer:
// we first extract the url_encoded_fmt_stream_map parameter,
// and from each comma-separated entry in this value, we extract
// other parameters (url and type).
let key = "url_encoded_fmt_stream_map=";
let pos = xhr.responseText.indexOf(key);
if (pos == -1) {
return;
}
let streams = decodeURIComponent(xhr.responseText
.substring(pos + key.length)).split(",");
let uri;
let mimeType;
// itag is an undocumented value which maps to resolution and mimetype
// see https://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
// Ordered from least to most preferred
let recognizedItags = [
"17", // 144p 3GP
"36", // 240p 3GP
"43", // 360p WebM
#ifdef MOZ_WIDGET_GONK
"18", // 360p H.264
#endif
];
let bestItag = -1;
let extras = { }
streams.forEach(function(aStream) {
let params = extractParameters(aStream);
let url = params["url"];
let type = params["type"] ? params["type"].split(";")[0] : null;
let itag = params["itag"];
let index;
if (url && type && ((index = recognizedItags.indexOf(itag)) != -1) &&
index > bestItag) {
uri = url + '&signature=' + (params["sig"] ? params['sig'] : '');
mimeType = type;
bestItag = index;
}
for (let param in params) {
if (["thumbnail_url", "length_seconds", "title"].indexOf(param) != -1) {
extras[param] = decodeURIComponent(params[param]);
}
}
});
if (uri && mimeType) {
cpmm.sendAsyncMessage("content-handler", {
url: uri,
type: mimeType,
extras: extras
});
}
});
xhr.send(null);
throw Components.results.NS_ERROR_ILLEGAL_VALUE;
},
allowPort: function yt_phAllowPort(aPort, aScheme) {
return false;
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([YoutubeProtocolHandler]);