Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit b9d9a19

Browse files
committed
Interrupt all chrome downloads
1 parent 1f99a5f commit b9d9a19

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

chrome-extension/background.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ try {
2828
chromeVersion = 33;
2929
}
3030
chromeVersion = parseInt(chromeVersion);
31-
sendMessageToHost({ version: "1.1.3" });
31+
sendMessageToHost({ version: "1.1.4" });
3232

3333

3434
// Message format to send the download information to the uget-chrome-wrapper
@@ -75,23 +75,60 @@ function postParams(source) {
7575
}
7676

7777
// Add to Chrome context menu
78-
chrome.contextMenus.create(
79-
{
80-
title: 'Download with uGet',
81-
id: "download_with_uget",
82-
contexts: ['link']
83-
}
84-
);
78+
chrome.contextMenus.create({
79+
title: 'Download with uGet',
80+
id: "download_with_uget",
81+
contexts: ['link']
82+
});
8583

86-
chrome.contextMenus.onClicked.addListener(function (info, tab) {
84+
chrome.contextMenus.onClicked.addListener(function(info, tab) {
8785
"use strict";
8886
if (info.menuItemId === "download_with_uget") {
89-
clearMessage();
90-
message.url = info['linkUrl'];
91-
message.referrer = info['pageUrl'];
92-
sendMessageToHost(message);
93-
clearMessage();
87+
clearMessage();
88+
message.url = info['linkUrl'];
89+
message.referrer = info['pageUrl'];
90+
sendMessageToHost(message);
91+
clearMessage();
92+
}
93+
});
94+
95+
// Interrupt Google Chrome download
96+
chrome.downloads.onCreated.addListener(function(downloadItem) {
97+
98+
if (ugetWrapperNotFound) { // uget-chrome-wrapper not installed
99+
return;
100+
}
101+
102+
var fileSize = downloadItem['fileSize'];
103+
104+
if (fileSize != -1 && fileSize < 300000) { // 300 kb
105+
return;
94106
}
107+
108+
var url = '';
109+
if (chromeVersion >= 54) {
110+
url = downloadItem['finalUrl'];
111+
} else {
112+
url = downloadItem['url'];
113+
}
114+
115+
if (!url) {
116+
return;
117+
}
118+
119+
if (url.includes("//docs.google.com/") || url.includes("googleusercontent.com/docs")) { // Cannot download from Google Docs
120+
return;
121+
}
122+
123+
chrome.downloads.cancel(downloadItem.id); // Cancel the download
124+
chrome.downloads.erase({ id: downloadItem.id }); // Erase the download from list
125+
126+
clearMessage();
127+
message.url = url;
128+
message.filename = downloadItem['filename'];
129+
message.filesize = fileSize;
130+
message.referrer = downloadItem['referrer'];
131+
sendMessageToHost(message);
95132
});
96133

97134
chrome.webRequest.onBeforeRequest.addListener(function(details) {
@@ -146,19 +183,19 @@ chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
146183
]);
147184
chrome.webRequest.onHeadersReceived.addListener(function(details) {
148185

149-
if (ugetWrapperNotFound) { // uget-chrome-wrapper not installed
186+
if (ugetWrapperNotFound) { // uget-chrome-wrapper not installed
150187
return {
151188
responseHeaders: details.responseHeaders
152189
};
153190
}
154191

155-
if (!details.statusLine.includes("200")) { // HTTP response is not OK
192+
if (!details.statusLine.includes("200")) { // HTTP response is not OK
156193
return {
157194
responseHeaders: details.responseHeaders
158195
};
159196
}
160197

161-
if (details.url.includes("//docs.google.com/") || details.url.includes("googleusercontent.com/docs")) { // Cannot download from Google Docs
198+
if (details.url.includes("//docs.google.com/") || details.url.includes("googleusercontent.com/docs")) { // Cannot download from Google Docs
162199
return {
163200
responseHeaders: details.responseHeaders
164201
};

chrome-extension/manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"webRequest",
3838
"webRequestBlocking",
3939
"nativeMessaging",
40-
"contextMenus"
40+
"contextMenus",
41+
"downloads"
4142
],
42-
"version": "1.1.3"
43+
"version": "1.1.4"
4344
}

0 commit comments

Comments
 (0)