-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
237 lines (214 loc) · 8.33 KB
/
background.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
/*
* Copyright (c) {2021} torikulhabib (https://github.com/torikulhabib)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: torikulhabib <torik.habib@Gmail.com>
*/
var ResponGdm = false;
var InterruptDownloads = true;
var PortSet = "";
var CustomPort = false;
var DownloadVideo = false;
load_conf ();
setInterval (function () {
fetch (get_host (), {requiredStatus: 'ok'}).then(function() {
ResponGdm = false;
}).catch(function() {
ResponGdm = true;
});
icon_load ();
}, 2000);
async function RunScript (tabId, callback) {
let existid = false;
let scripts = await chrome.scripting.getRegisteredContentScripts();
for (let scrid of scripts.map((script) => script.id)) {
existid = true;
}
callback (existid);
}
async function StopScript (tabId) {
let scripts = await chrome.scripting.getRegisteredContentScripts();
for (let scrid of scripts.map((script) => script.id)) {
await chrome.scripting.unregisterContentScripts ({ids: [scrid],}).catch(function() {});
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab)=> {
if (DownloadVideo) {
if (changeInfo.status == 'loading') {
RunScript (tabId, function (existid) {
if (!existid) {
chrome.scripting.registerContentScripts([{id: `${tabId}`, allFrames: false, matches: ['<all_urls>'], js: ['content-script.js'], css: ['content-script.css']}]);
}
});
chrome.webRequest.onResponseStarted.removeListener (WebContent);
chrome.tabs.sendMessage(tabId, {message: 'gdmclean'}).then (function () {}).catch(function() {});
chrome.webRequest.onResponseStarted.addListener (WebContent, {urls: ['<all_urls>']}, ['responseHeaders']);
}
} else {
StopScript (tabId);
chrome.webRequest.onResponseStarted.removeListener (WebContent);
}
});
function WebContent (content) {
if (content.tabId === -1) {
return;
}
let length = content.responseHeaders.filter (cont => cont.name.toUpperCase () === 'CONTENT-LENGTH').map (lcont => lcont.value).shift ();
if (length > 1) {
let gdmtype = content.responseHeaders.filter (cont => cont.name.toUpperCase () === 'CONTENT-TYPE').map (lcont => lcont.value).shift ();
if (gdmtype != 'undefined') {
if (`${gdmtype}`.startsWith ('video')) {
if (length > 10000000) {
chrome.tabs.sendMessage(content.tabId, {message: 'gdmvideo', urls: content.url, size: length, mimetype: gdmtype}).then (function () {}).catch(function() {});
}
} else if (`${gdmtype}`.startsWith ('audio')) {
chrome.tabs.sendMessage(content.tabId, {message: 'gdmaudio', urls: content.url, size: length, mimetype: gdmtype}).then (function () {}).catch(function() {});
}
}
}
}
chrome.downloads.onCreated.addListener (function (downloadItem) {
if (!InterruptDownloads || ResponGdm) {
return;
}
if(chrome.runtime.lastError) {
if (!downloadItem['finalUrl'].includes ("blob:")) {
chrome.downloads.cancel (downloadItem.id);
}
} else {
console.clear ();
}
});
chrome.downloads.onDeterminingFilename.addListener (function (downloadItem) {
if (!InterruptDownloads || ResponGdm) {
return;
}
if (downloadItem['finalUrl'].includes ("blob:")) {
return;
}
SendToOniDM (get_downloader (downloadItem));
queueMicrotask (function () {
chrome.downloads.cancel (downloadItem.id, function () {
chrome.downloads.erase ({ id: downloadItem.id });
});
});
});
SendToOniDM = function (downloadItem) {
fetch (get_host (), { method: 'post', body: downloadItem }).then (function (r) { return r.text (); }).catch (function () {});
}
function get_downloader (downloadItem) {
return`link:${downloadItem['finalUrl']},filename:${downloadItem['filename']},referrer:${downloadItem['referrer']},mimetype:${downloadItem['mime']},filesize:${downloadItem['fileSize']},resumable:${downloadItem['canResume']},`;
}
async function chromeStorageGetter (key) {
return new Promise (resolve => {
chrome.storage.sync.get (key, (obj)=> {
return resolve (obj[key] || '');
})
});
}
async function load_conf () {
InterruptDownloads = await chromeStorageGetter ('interrupt-download');
DownloadVideo = await chromeStorageGetter ('video-download');
CustomPort = await chromeStorageGetter ('port-custom');
PortSet = await chromeStorageGetter ('port-input');
icon_load ();
}
async function setPortCustom (interrupt) {
await SavetoStorage('port-custom', interrupt);
}
async function setVideoMenu (download) {
await SavetoStorage('video-download', download);
}
async function setPortInput (interrupt) {
if (CustomPort) {
await SavetoStorage('port-input', interrupt);
}
}
async function setInterruptDownload (interrupt) {
await SavetoStorage('interrupt-download', interrupt);
}
async function SavetoStorage(key, value) {
return new Promise(resolve => {
chrome.storage.sync.set({[key]: value}, resolve);
});
}
chrome.commands.onCommand.addListener((command) => {
if (command == "Ctrl+Shift+Y") {
setInterruptDownload (!InterruptDownloads);
load_conf ();
} else if (command == "Ctrl+Shift+E") {
DownloadVideo = !DownloadVideo;
chrome.tabs.query ({}, function(tab){
chrome.tabs.reload (tab.Id, null, function () {});
});
setVideoMenu (DownloadVideo);
load_conf ();
}
});
chrome.runtime.onMessage.addListener((request, sender, callback) => {
if (request.extensionId == "interuptopen") {
chrome.runtime.sendMessage({ message: InterruptDownloads, extensionId: "popintrup" }).catch(function() {});
} else if (request.extensionId == "customopen") {
chrome.runtime.sendMessage({ message: CustomPort, extensionId: "popcust" }).catch(function() {});
} else if (request.extensionId == "portopen") {
chrome.runtime.sendMessage({ message: PortSet, extensionId: "popport" }).catch(function() {});
} else if (request.extensionId == "videoopen") {
chrome.runtime.sendMessage({ message: DownloadVideo, extensionId: "popvideo" }).catch(function() {});
} else if (request.extensionId == "videochecked") {
if (DownloadVideo != request.message) {
DownloadVideo = request.message;
chrome.tabs.query ({}, function(tab){
chrome.tabs.reload (tab.Id, null, function () {});
});
}
setVideoMenu (request.message);
load_conf ();
} else if (request.extensionId == "interuptchecked") {
setInterruptDownload (request.message);
load_conf ();
} else if (request.extensionId == "customchecked") {
setPortCustom (request.message);
load_conf ();
} else if (request.extensionId == "portval") {
setPortInput (request.message);
load_conf ();
} else if (request.extensionId == "gdmurl") {
if (!InterruptDownloads || ResponGdm) {
downloadchrome (request.message);
return;
}
SendToOniDM (request.message);
}
});
async function downloadchrome (urls) {
let url = urls.substring (5, urls.lastIndexOf(",filename:"));
await chrome.downloads.download({url: url});
}
get_host = function () {
if (CustomPort) {
return `http://127.0.0.1:${PortSet}`;
} else {
return "http://127.0.0.1:2021";
}
}
icon_load = function () {
if (InterruptDownloads && !ResponGdm) {
chrome.action.setIcon({path: "./icons/icon_32.png"});
} else {
chrome.action.setIcon({path: "./icons/icon_disabled_32.png"});
}
}