-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground-script.js
222 lines (193 loc) · 6.45 KB
/
background-script.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
var config = null;
// receive messages from the content script
function getMessage(message, sender, senderResponse) {
switch (message.type) {
case "reload_config":
reloadConfig(message.config);
break;
case "get_config":
getConfig().then( () => { senderResponse({config: config}) } );
return true; // tells the answer will come asynchronously
break;
case "submit":
submitURI(message).then( (response) => { senderResponse(response); });
return true;
break;
case "force_dl":
forceDownload(message).then( (response) => { senderResponse(response); });
return true;
break;
default:
console.log("MlDonkeyFox2: incorrect message type received in background script" + message.type);
break;
}
}
// Get the webextension configuration
function getConfig() {
return new Promise( (resolve, reject) => {
if ( config )
resolve(config);
else {
let myStorage = browser.storage.local;
myStorage.get('MlDonkeyFox').then( (items) => {
if (items.MlDonkeyFox) {
config = items.MlDonkeyFox;
if ( config )
resolve(config);
else
reject();
}
});
}
});
}
// signal a new config is available
function reloadConfig(newConfig) {
config = newConfig;
registerBittorrentContent();
browser.tabs.query( {} ).then( (tabs) => {
for ( let tab of tabs ) {
browser.tabs.sendMessage( tab.id, { type: "new_config", config: config } );
}
});
}
function forceDownload(target) {
return new Promise( (resolve, reject) => {
if ( !config.url ) {
resolve({title: "Please, first configure the MlDonkeyFox addon", level: "error"});
}
let mldonkeyUrl = getMldonkeyUri();
let fullUrl = mldonkeyUrl + "/submit?q=force_download";
let request = new XMLHttpRequest();
request.open("GET", fullUrl, true);
request.onreadystatechange = function() {
if ( request.readyState == 4 ) {
if ( request.status == 200 ) {
let resp = request.responseText;
if ( resp.search("Forced start of") != -1 ) {
resolve({title: "File download forced:", url: target.url, level: "info", action: "submitted"});
}
else {
resolve({title: "Unexpected response:" + request.responseText, url: target.url, level:"error"});
}
}
else if ( request.status != 0 ) {
resolve({title: "Error while submitting", url: description, bottom: "http status: " + request.statusText, level: "error"});
}
}
};
request.onerror = function (e) {
resolve({title: "Error while submitting", url: description, bottom: "error, cannot reach server: " + mldonkeyUrl, level: "error"});
};
request.send(null);
});
}
// submit the link to mldonkey
function submitURI(target) {
return new Promise( (resolve, reject) => {
if ( !config.url ) {
resolve({title: "Please, first configure the MlDonkeyFox addon", level: "error"});
}
let uri = target.uri;
let description = target.description;
if ( description == "" )
description = uri;
let mldonkeyUrl = getMldonkeyUri();
let fullUrl = mldonkeyUrl + "/submit?q=";
if ( target.type == "torrent_header" )
fullUrl += "startbt+";
else
fullUrl += "dllink+";
fullUrl += encodeURIComponent(uri);
let request = new XMLHttpRequest();
request.open("GET", fullUrl, true);
request.onreadystatechange = function() {
if ( request.readyState == 4 ) {
if ( request.status == 200 ) {
let resp = request.responseText;
if ( resp.search("File is already in download queue") != -1 ) {
resolve({title: "File already downloading:", url: description, level: "warning"});
}
else if ( resp.search("File already downloaded") != -1 ) {
resolve({title: "File has already been downloaded:", url: description, level: "warning", action: "force_download", label: "Force re-download"});
}
else if ( resp.search("File is already shared in") != -1 ) {
resolve({title: "File is already present in the incoming folder:", url: description, level: "warning"});
}
else if ( resp.search("Unable to match URL") != -1 ) {
resolve({title: "Internal error: Unable to match URL:", url: description, level: "error"});
}
else {
resolve({title: "File submitted:", url: description, level:"info", action: "submitted"});
}
}
else if ( request.status != 0 ) {
resolve({title: "Error while submitting", url: description, bottom: "http status: " + request.statusText, level: "error"});
}
}
};
request.onerror = function (e) {
resolve({title: "Error while submitting", url: description, bottom: "error, cannot reach server: " + mldonkeyUrl, level: "error"});
};
request.send(null);
});
}
function getMldonkeyUri() {
let uri = ""
if ( config.user == "" )
uri = config.protocol + "://" + config.url + ":" + config.port.toString();
else
uri = config.protocol + "://" + config.user + ":" + config.password + "@" + config.url + ":" + config.port.toString();
return uri;
}
function getHeaders(event) {
// redirect
if ( event.statusCode >= 300 && event.statusCode <= 399 ) {
let uri = null;
let location = findHeader(event.responseHeaders, "location");
if ( location.substring(0,7) == "ed2k://" ) {
if ( config.ed2k )
uri = location;
}
else {
if ( location.substring(0,7) == "magnet:" ) {
if ( config.magnet )
uri = location;
}
}
if ( uri ) {
submitURI( {type: "submit", uri: uri, description: uri} ).then( (response) => {
response.type = "display";
browser.tabs.sendMessage(event.tabId, response);
} );
return { redirectUrl: "about:blank" };
}
}
// check for torrent file type
else {
if ( config.bittorrent ) {
if ( findHeader(event.responseHeaders, "content-type") == "application/x-bittorrent" ) {
submitURI( {type: "torrent_header", uri: event.url, description: event.url} ).then( (response) => {
response.type = "display";
browser.tabs.sendMessage(event.tabId, response);
} );
return { redirectUrl: "about:blank" };
}
}
}
}
function checkRequest(event) {
}
function findHeader(headers, name) {
for ( let header of headers ) {
if ( header.name.toLowerCase() == name.toLowerCase() )
return header.value;
}
return null;
}
function registerWebrequests() {
browser.webRequest.onHeadersReceived.removeListener(getHeaders)
browser.webRequest.onHeadersReceived.addListener(getHeaders, {urls: ["http://*/*", "https://*/*"], types: ["main_frame", "sub_frame"]}, ["blocking", "responseHeaders"]);
}
getConfig().then( () => { registerWebrequests(); } );
browser.runtime.onMessage.addListener(getMessage);