/
utils.js
263 lines (228 loc) · 6.28 KB
/
utils.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
var bg_utils = {
getFileSize: function (message, cb) {
"use strict";
var url = message.url;
var response = {
fileSize: 0,
fileType: '',
status: 0
};
mono.request({
url: url,
type: 'HEAD'
}, function (err, resp) {
if (err) {
return cb(response);
}
response.status = resp.statusCode;
response.fileSize = parseInt(resp.headers['content-length']) || 0;
var contentType = resp.headers['content-type'];
if (contentType) {
response.fileType = contentType;
}
cb(response);
});
},
chromeDownload: {
inProgress: {},
addDownloadId: function (id, details) {
if (!this.inProgress[id]) {
this.inProgress[id] = details;
}
},
removeDownloadId: function (id) {
if (this.inProgress[id]) {
delete this.inProgress[id];
}
},
canFixNetworkFiled: function (details, downloadDelta) {
var time = parseInt(Date.now() / 1000);
var resumeDownload = false;
if (!details.lastFix) {
details.lastFix = 0;
}
if (details.lastFix + 5 < time) {
details.lastFix = time;
resumeDownload = true;
}
var isInterrupted = downloadDelta.state && downloadDelta.state.current === 'interrupted';
var networkFailed = downloadDelta.error && downloadDelta.error.current === 'NETWORK_FAILED';
var canResume = downloadDelta.canResume && downloadDelta.canResume.current;
if (!isInterrupted || !networkFailed || !canResume) {
resumeDownload = false;
}
return resumeDownload;
},
onChange: function (downloadDelta) {
var _this = this;
var details = _this.inProgress[downloadDelta.id];
if (!details) {
return;
}
var resumeDownload = false;
if (details.fixNetworkFiled) {
resumeDownload = _this.canFixNetworkFiled(details, downloadDelta);
}
if (resumeDownload) {
chrome.downloads.resume(downloadDelta.id);
} else if (downloadDelta.state && ['complete'].indexOf(downloadDelta.state.current) !== -1) {
if(details.quality && parseInt(details.quality) >= 720){
window.localStorage.successDownload = +(window.localStorage.successDownload || 0) + 1;
if ((
window.localStorage.successDownload == 3
||
parseInt(window.localStorage.successDownload) % 10 == 0
)
&&
typeof window.localStorage.popupRate == 'undefined'
) {
var counter = 1;
var interval = setInterval(function () {
counter++;
if (counter % 2) {
var icon = {"16": chrome.extension.getURL('/img/icon_16.png')}
} else {
icon = {"16": chrome.extension.getURL('/img/icon_star_16.png')}
}
chrome.browserAction.setIcon({"path": icon});
chrome.browserAction.setPopup({popup: "rate_popup.html"});
}, 500);
chrome.runtime.onMessage.addListener(ratePopupOpened);
function ratePopupOpened(message) {
if(!message){
return;
}
if (message == 'ratePopupOpened') {
clearInterval(interval);
chrome.browserAction.setIcon({"path": {"16": chrome.extension.getURL('/img/icon_16.png')}});
chrome.browserAction.setPopup({popup: ""});
chrome.runtime.onMessage.removeListener(ratePopupOpened);
}
}
}
}
_this.removeDownloadId(downloadDelta.id);
if (Object.keys(_this.inProgress).length === 0) {
_this.removeListener();
}
} else if (downloadDelta.state && ['interrupted'].indexOf(downloadDelta.state.current) !== -1) {
_this.removeDownloadId(downloadDelta.id);
if (Object.keys(_this.inProgress).length === 0) {
_this.removeListener();
}
}
},
listener: null,
isListen: false,
addListener: function () {
if (!this.listener) {
this.listener = this.onChange.bind(this);
}
if (!this.isListen) {
this.isListen = true;
chrome.downloads.onChanged.addListener(this.listener);
}
},
removeListener: function () {
this.isListen = false;
chrome.downloads.onChanged.removeListener(this.listener);
},
download: function (details) {
var _this = this;
var url = details.url;
var filename = details.filename;
details.fixNetworkFiled = /vk\.me\/.+\.mp4/i.test(url);
chrome.downloads.download({
url: url,
filename: filename
}, function (downloadId) {
_this.addDownloadId(downloadId, details);
_this.addListener();
// if (details.fixNetworkFiled) {
// _this.addDownloadId(downloadId, details);
// _this.addListener();
// }
});
}
},
downloadFile: function (message) {
"use strict";
var _this = bg_utils;
var url = message.options.url;
var filename = message.options.filename;
if (mono.isFF) {
return mono.sendMessage({action: 'download', url: url, filename: filename}, null, 'service');
} else if (mono.isChrome) {
var onHasPermission = function () {
return _this.chromeDownload.download(message.options);
};
if (chrome.downloads && chrome.downloads.download) {
return onHasPermission();
} else {
return chrome.permissions.request({
permissions: ['downloads']
}, function (granted) {
if (granted) {
return onHasPermission();
}
});
}
} else if (mono.isGM) {
return GM_download(url, filename);
}
},
chromeListDownload: function (list, folder) {
var waitDownloadId = null;
list = list.map(function (item) {
return {url: item.url, filename: folder + item.filename};
});
var addListener = function () {
chrome.downloads.onChanged.addListener(onChange);
};
var removeListener = function () {
chrome.downloads.onChanged.removeListener(onChange);
};
var onChange = function (downloadDelta) {
if (downloadDelta.id !== waitDownloadId || !downloadDelta.state) {
return;
}
if (['interrupted', 'complete'].indexOf(downloadDelta.state.current) !== -1) {
waitDownloadId = null;
return onSuccess();
}
};
addListener();
var index = -1;
var onSuccess = function () {
index++;
var item = list[index];
if (!item) {
removeListener();
return;
}
return chrome.downloads.download({
url: item.url,
filename: item.filename
}, function (downloadId) {
waitDownloadId = downloadId;
});
};
return onSuccess();
},
getData: function (message, cb) {
"use strict";
var url = message.url;
if (!url) {
return cb();
}
mono.request({
url: url
}, function (err, resp, data) {
if (err) {
return cb();
}
cb(data);
});
}
};
engine.utils = bg_utils;