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

Commit 8526253

Browse files
committed
Bug fixes in extracting cookie
1 parent 26f8ad1 commit 8526253

File tree

4 files changed

+122
-104
lines changed

4 files changed

+122
-104
lines changed

extension/background.js

Lines changed: 105 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -129,71 +129,6 @@ current_browser.runtime.onMessage.addListener(function(request, sender, sendResp
129129
}
130130
});
131131

132-
// Send message to the uget-chrome-wrapper
133-
function sendMessageToHost(message) {
134-
current_browser.runtime.sendNativeMessage(hostName, message, function(response) {
135-
ugetWrapperNotFound = (response == null);
136-
if (!ugetWrapperNotFound && !ugetChromeWrapperVersion) {
137-
ugetChromeWrapperVersion = response.version;
138-
ugetVersion = response.uget;
139-
}
140-
});
141-
}
142-
143-
function getInfo() {
144-
if (ugetWrapperNotFound || !ugetChromeWrapperVersion) {
145-
return "Error: Unable to connect to the uget-chrome-wrapper";
146-
} else if (!ugetChromeWrapperVersion.startsWith("2.")) {
147-
return "Warning: Please update the uget-chrome-wrapper to the latest version";
148-
} else {
149-
return "Info: Found uGet: " + ugetVersion + " and uget-chrome-wrapper: " + ugetChromeWrapperVersion;
150-
}
151-
}
152-
153-
function clearMessage() {
154-
message.url = '';
155-
message.cookies = '';
156-
message.filename = '';
157-
message.filesize = '';
158-
message.referrer = '';
159-
message.useragent = '';
160-
}
161-
162-
function postParams(source) {
163-
var array = [];
164-
for (var key in source) {
165-
array.push(encodeURIComponent(key) + '=' + encodeURIComponent(source[key]));
166-
}
167-
return array.join('&');
168-
}
169-
170-
function extractRootURL(url) {
171-
var domain;
172-
173-
if (url.indexOf("://") > -1) {
174-
domain = url.split('/')[0] + '/' + url.split('/')[1] + '/' + url.split('/')[2];
175-
} else {
176-
domain = url.split('/')[0];
177-
}
178-
179-
return domain;
180-
}
181-
182-
function parseCookies(cookies_arr) {
183-
cookies = '';
184-
185-
for (var i in cookies_arr) {
186-
cookies += cookies_arr[i].domain + '\t';
187-
cookies += (cookies_arr[i].httpOnly ? "FALSE" : "TRUE") + '\t';
188-
cookies += cookies_arr[i].path + '\t';
189-
cookies += (cookies_arr[i].secure ? "TRUE" : "FALSE") + '\t';
190-
cookies += Math.round(cookies_arr[i].expirationDate) + '\t';
191-
cookies += cookies_arr[i].name + '\t';
192-
cookies += cookies_arr[i].value;
193-
cookies += '\n';
194-
}
195-
}
196-
197132
// Add to Chrome context menu
198133
current_browser.contextMenus.create({
199134
title: 'Download with uGet',
@@ -204,13 +139,9 @@ current_browser.contextMenus.create({
204139
current_browser.contextMenus.onClicked.addListener(function(info, tab) {
205140
"use strict";
206141
if (info.menuItemId === "download_with_uget") {
207-
clearMessage();
208-
chrome.cookies.getAll({ 'url': extractRootURL(info.pageUrl) }, parseCookies);
209142
message.url = info['linkUrl'];
210143
message.referrer = info['pageUrl'];
211-
message.cookies = cookies;
212-
sendMessageToHost(message);
213-
clearMessage();
144+
current_browser.cookies.getAll({ 'url': extractRootURL(info.pageUrl) }, parseCookies);
214145
}
215146
});
216147

@@ -236,23 +167,17 @@ current_browser.downloads.onCreated.addListener(function(downloadItem) {
236167
return;
237168
}
238169
// Cancel the download
239-
current_browser.downloads.cancel(downloadItem.id).then(
240-
function() {
241-
// Erase the download from list
242-
current_browser.downloads.erase({
243-
id: downloadItem.id
244-
}); // ignore callbacks
245-
}
246-
);
170+
current_browser.downloads.cancel(downloadItem.id);
171+
// Erase the download from list
172+
current_browser.downloads.erase({
173+
id: downloadItem.id
174+
});
247175

248-
clearMessage();
249-
chrome.cookies.getAll({ 'url': extractRootURL(info.pageUrl) }, parseCookies);
250176
message.url = url;
251177
message.filename = downloadItem['filename'];
252178
message.filesize = fileSize;
253179
message.referrer = downloadItem['referrer'];
254-
message.cookies = cookies;
255-
sendMessageToHost(message);
180+
current_browser.cookies.getAll({ 'url': extractRootURL(url) }, parseCookies);
256181
});
257182

258183
current_browser.webRequest.onBeforeRequest.addListener(function(details) {
@@ -275,7 +200,6 @@ current_browser.webRequest.onBeforeRequest.addListener(function(details) {
275200
'requestBody'
276201
]);
277202
current_browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
278-
clearMessage();
279203
currRequest++;
280204
if (currRequest > 2)
281205
currRequest = 2;
@@ -379,10 +303,7 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
379303
if (details.method != "POST") {
380304
message.postdata = '';
381305
}
382-
chrome.cookies.getAll({ 'url': extractRootURL(message.url) }, parseCookies);
383-
message.cookies = cookies;
384-
sendMessageToHost(message);
385-
message.postdata = '';
306+
current_browser.cookies.getAll({ 'url': extractRootURL(message.url) }, parseCookies);
386307
var scheme = /^https/.test(details.url) ? 'https' : 'http';
387308
if (chromeVersion >= 35 || firefoxVersion >= 51) {
388309
return {
@@ -411,8 +332,9 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
411332
return {
412333
cancel: true
413334
};
335+
} else {
336+
clearMessage();
414337
}
415-
clearMessage();
416338
return {
417339
responseHeaders: details.responseHeaders
418340
};
@@ -429,26 +351,109 @@ current_browser.webRequest.onHeadersReceived.addListener(function(details) {
429351
'blocking'
430352
]);
431353

354+
432355
/**
433-
* Update the include & exclude keywords.
434-
* Is called from the popup.js.
435-
*/
356+
* Send message to the uget-chrome-wrapper
357+
*/
358+
function sendMessageToHost(message) {
359+
current_browser.runtime.sendNativeMessage(hostName, message, function(response) {
360+
clearMessage();
361+
ugetWrapperNotFound = (response == null);
362+
if (!ugetWrapperNotFound && !ugetChromeWrapperVersion) {
363+
ugetChromeWrapperVersion = response.version;
364+
ugetVersion = response.uget;
365+
}
366+
});
367+
}
368+
369+
/**
370+
* Create a meaningful message of the internal state.
371+
*/
372+
function getInfo() {
373+
if (ugetWrapperNotFound || !ugetChromeWrapperVersion) {
374+
return "Error: Unable to connect to the uget-chrome-wrapper";
375+
} else if (!ugetChromeWrapperVersion.startsWith("2.")) {
376+
return "Warning: Please update the uget-chrome-wrapper to the latest version";
377+
} else {
378+
return "Info: Found uGet: " + ugetVersion + " and uget-chrome-wrapper: " + ugetChromeWrapperVersion;
379+
}
380+
}
381+
382+
/**
383+
* Clear the message.
384+
*/
385+
function clearMessage() {
386+
message.url = '';
387+
message.cookies = '';
388+
message.filename = '';
389+
message.filesize = '';
390+
message.referrer = '';
391+
message.useragent = '';
392+
}
393+
394+
/**
395+
* Extract the POST parameters from a form data.
396+
*/
397+
function postParams(source) {
398+
var array = [];
399+
for (var key in source) {
400+
array.push(encodeURIComponent(key) + '=' + encodeURIComponent(source[key]));
401+
}
402+
return array.join('&');
403+
}
404+
405+
/**
406+
* Extract the root of a URL.
407+
*/
408+
function extractRootURL(url) {
409+
var domain;
410+
if (url.indexOf("://") > -1) {
411+
domain = url.split('/')[0] + '/' + url.split('/')[1] + '/' + url.split('/')[2];
412+
} else {
413+
domain = url.split('/')[0];
414+
}
415+
return domain;
416+
}
417+
418+
/**
419+
* Parse the cookies and send the message to the native host.
420+
*/
421+
function parseCookies(cookies_arr) {
422+
cookies = '';
423+
for (var i in cookies_arr) {
424+
cookies += cookies_arr[i].domain + '\t';
425+
cookies += (cookies_arr[i].httpOnly ? "FALSE" : "TRUE") + '\t';
426+
cookies += cookies_arr[i].path + '\t';
427+
cookies += (cookies_arr[i].secure ? "TRUE" : "FALSE") + '\t';
428+
cookies += Math.round(cookies_arr[i].expirationDate) + '\t';
429+
cookies += cookies_arr[i].name + '\t';
430+
cookies += cookies_arr[i].value;
431+
cookies += '\n';
432+
}
433+
message.cookies = cookies;
434+
sendMessageToHost(message);
435+
}
436+
437+
/**
438+
* Update the include & exclude keywords.
439+
* Is called from the popup.js.
440+
*/
436441
function updateKeywords(include, exclude) {
437442
keywordsToInclude = include.split(/[\s,]+/);
438443
keywordsToExclude = exclude.split(/[\s,]+/);
439444
}
440445

441446
/**
442-
* Update the minimum file size to interrupt.
443-
* Is called from the popup.js.
444-
*/
447+
* Update the minimum file size to interrupt.
448+
* Is called from the popup.js.
449+
*/
445450
function updateMinFileSize(size) {
446451
minFileSizeToInterrupt = size;
447452
}
448453

449454
/**
450-
* Check whether not to interrupt the given url.
451-
*/
455+
* Check whether not to interrupt the given url.
456+
*/
452457
function isBlackListed(url) {
453458
if (!url) {
454459
return;
@@ -465,11 +470,10 @@ function isBlackListed(url) {
465470
}
466471

467472
/**
468-
* Check whether to interrupt the given url or not.
469-
*/
473+
* Check whether to interrupt the given url or not.
474+
*/
470475
function isWhiteListed(url) {
471476
for (var keyword of keywordsToInclude) {
472-
console.log(keyword + " " + url);
473477
if (url.includes(keyword)) {
474478
return true;
475479
}
@@ -478,8 +482,8 @@ function isWhiteListed(url) {
478482
}
479483

480484
/**
481-
* Enable/Disable the plugin and update the plugin icon based on the state.
482-
*/
485+
* Enable/Disable the plugin and update the plugin icon based on the state.
486+
*/
483487
function setInterruptDownload(interrupt, writeToStorage) {
484488
interruptDownloads = interrupt;
485489
if (interrupt) {

extension/popup.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21+
2122
body {
2223
overflow-x: hidden;
24+
padding: 10px;
2325
}
2426

2527
input#save {

extension/popup.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<html>
2121

2222
<head>
23+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
2324
<script src="popup.js"></script>
2425
<link rel="stylesheet" type="text/css" href="popup.css">
2526
</head>
@@ -42,14 +43,17 @@
4243
<br>
4344
<div id="controls-container">
4445
<label id="label" for="keywords">Don't interrupt URLs containing these keywords:</label>
46+
<br>
4547
<input title="Separate values by comma (Eg: github.com, .jpg)" type="text" id="keywordsToExclude" name="keywordsToExclude" size="50" value="" />
4648
</div>
4749
<div id="controls-container">
4850
<label id="label" for="keywords">Interrupt downloads with minium file size:</label>
51+
<br>
4952
<input title="Size in kb" type="text" id="fileSize" name="fileSize" size="50" value=""/>
5053
</div>
5154
<div id="controls-container">
5255
<label id="label" for="keywords">Interrupt URLs containing these keywords, regardless of the file size:</label>
56+
<br>
5357
<input title="Separate values by comma (Eg: github.com, .jpg)" type="text" id="keywordsToInclude" name="keywordsToInclude" size="50" value="" />
5458
</div>
5559
<div id="button-container">

uget-chrome-wrapper/bin/uget-chrome-wrapper

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,23 @@ def read_message():
124124
pass
125125
if not filename:
126126
filename = extract_file_name(url)
127+
128+
# Add the referer url
127129
if referer:
128130
command.append("--http-referer=" + referer)
129-
# subprocess.call([UGET_COMMAND, "--http-referer=" + referer, "--filename=" + filename, url])
130-
# else:
131-
# subprocess.call([UGET_COMMAND, "--filename=" + filename, url])
131+
132+
# Add the file name
133+
if filename:
134+
command.append("--filename=" + filename)
135+
136+
# Add the cookie file
132137
if use_cookie_file:
133138
command.append("--http-cookie-file=" + cookie_filepath)
139+
140+
# Add the url
134141
command.append(url)
135142

143+
# Pass the parameters to uGet
136144
subprocess.call(command)
137145

138146
if use_cookie_file:

0 commit comments

Comments
 (0)