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

Commit e5fd048

Browse files
committed
Add download all links option
1 parent b0635c9 commit e5fd048

File tree

7 files changed

+148
-18
lines changed

7 files changed

+148
-18
lines changed

extension/background.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ var message = {
111111
filename: '',
112112
filesize: '',
113113
referrer: '',
114-
postdata: ''
114+
postdata: '',
115+
batch: false
115116
};
116117

117118
// Listen to the key press
@@ -136,12 +137,29 @@ current_browser.contextMenus.create({
136137
contexts: ['link']
137138
});
138139

140+
current_browser.contextMenus.create({
141+
title: 'Download all links with uGet',
142+
id: "download_all_links_with_uget",
143+
contexts: ['page']
144+
});
145+
139146
current_browser.contextMenus.onClicked.addListener(function(info, tab) {
140147
"use strict";
141148
if (info.menuItemId === "download_with_uget") {
142149
message.url = info['linkUrl'];
143150
message.referrer = info['pageUrl'];
144151
current_browser.cookies.getAll({ 'url': extractRootURL(info.pageUrl) }, parseCookies);
152+
} else if (info.menuItemId === "download_all_links_with_uget") {
153+
var dataToWebPage = {text: 'test', foo: 1, bar: false};
154+
current_browser.tabs.executeScript(null, {file: 'extract.js'}, function(results) {
155+
// Do nothing
156+
if(results[0].success) {
157+
message.url = results[0].urls;
158+
message.referrer = info['pageUrl'];
159+
message.batch = true;
160+
current_browser.cookies.getAll({ 'url': extractRootURL(info.pageUrl) }, parseCookies);
161+
}
162+
});
145163
}
146164
});
147165

@@ -371,15 +389,15 @@ function sendMessageToHost(message) {
371389
}
372390

373391
/**
374-
* Create a meaningful message of the internal state.
392+
* Return the internal state.
375393
*/
376-
function getInfo() {
394+
function getState() {
377395
if (ugetWrapperNotFound || !ugetChromeWrapperVersion) {
378-
return "Error: Unable to connect to the uget-chrome-wrapper";
396+
return 2;
379397
} else if (!ugetChromeWrapperVersion.startsWith("2.")) {
380-
return "Warning: Please update the uget-chrome-wrapper to the latest version";
398+
return 1;
381399
} else {
382-
return "Info: Found uGet: " + ugetVersion + " and uget-chrome-wrapper: " + ugetChromeWrapperVersion;
400+
return 0;
383401
}
384402
}
385403

@@ -393,6 +411,7 @@ function clearMessage() {
393411
message.filesize = '';
394412
message.referrer = '';
395413
message.useragent = '';
414+
message.batch = false;
396415
}
397416

398417
/**

extension/extract.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* uget-chrome-wrapper is an extension to integrate uGet Download manager
3+
* with Google Chrome, Chromium, Vivaldi and Opera in Linux and Windows.
4+
*
5+
* Copyright (C) 2017 Gobinath
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
extract();
22+
function extract() {
23+
var txt = '';
24+
25+
for (var i = 0; i < document.links.length; i++) {
26+
url = document.links[i].href;
27+
var valid = /^(ftp|http|https):\/\/[^ "]+$/.test(url);
28+
if(valid && txt.indexOf(url) == -1) {
29+
txt += url + '\n';
30+
}
31+
}
32+
33+
if(txt !== '') {
34+
return {success: true, urls: txt};
35+
}
36+
return {success: false, urls: ""};
37+
}

extension/manifest.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"strict_min_version": "52.0"
66
}
77
},
8+
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnI/ECgghIqV+/wQ7b6N+m5tupHZlieWd6nbqqSoqmhUlFg3DmqOi2G/LpVhxoSGHnbRBnemoO2Hs+6aLnlpHKyJ1/DQUHwTjPnvGc9sjTvks1L1dKWTFGcJLiQZPMsjs5g1b8ZNQCH8nGE4uqeSzVK7Hey61gwsS5+OwZYw5/3C/HfBZNNzQfphm7j+eMTuXJTBowA3A3I2ywPRTSOt8n/CxZUIL9O44QOs/WiZFjtERnZocvbHmkWLproj1MIcJVJd0OI11VAfqYznuwsCTIe7annsBpWL1UJUNK+EwVW8Jpj4CUeauC5c0HUWdAecLyx01yKtfokBvwX5fldB1YQIDAQAB",
9+
"offline_enabled": true,
810
"background": {
911
"scripts": [
1012
"background.js"
@@ -18,7 +20,8 @@
1820
"content_scripts": [{
1921
"all_frames": true,
2022
"js": [
21-
"content.js"
23+
"content.js",
24+
"extract.js"
2225
],
2326
"matches": [
2427
"http://*/*",
@@ -38,7 +41,6 @@
3841
"minimum_chrome_version": "19.0",
3942
"name": "uGet Integration",
4043
"short_name": "uGet",
41-
"offline_enabled": true,
4244
"permissions": [
4345
"<all_urls>",
4446
"webRequest",
@@ -47,7 +49,8 @@
4749
"contextMenus",
4850
"downloads",
4951
"storage",
50-
"cookies"
52+
"cookies",
53+
"activeTab"
5154
],
5255
"version": "2.0.8"
5356
}

extension/popup.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ hr {
6060
padding: 0;
6161
}
6262

63+
#footer {
64+
display: table;
65+
text-align: center;
66+
margin: 0 auto;
67+
line-height: 50%;
68+
color: #aaa;
69+
}
70+
6371
#info {
6472
display: table;
6573
text-align: center;

extension/popup.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<img id="img-icon" src="icon_32.png" /> <span>uGet Integration 2.0.8</span>
3232
</div>
3333
<hr>
34-
<label id="info"></label><label id="error"></label><label id="warn"></label>
34+
<label id="info">Info: Found uGet and uget-chrome-wrapper</label><label id="error">Error: Unable to connect to the uget-chrome-wrapper</label><label id="warn">Warning: Please update the uget-chrome-wrapper to the latest version</label>
3535
<hr>
3636
<div id="controls-container" class="inline">
3737
<label id="label" title="Ctrl + Shift + U" for="switch">Interrupt downloads:&nbsp;</label>
@@ -59,6 +59,8 @@
5959
<div id="button-container">
6060
<input type="button" value="Save" id="save" />
6161
</div>
62+
<br><br>
63+
<label id="footer">For more details and installation guide, visit the <a href="https://slgobinath.github.io/uget-chrome-wrapper/">Home Page</a></label>
6264
</div>
6365
</body>
6466

extension/popup.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,32 @@ function saveChanges() {
6262
window.addEventListener('load', function(evt) {
6363
// Show the system status
6464
current_browser.runtime.getBackgroundPage(function(backgroundPage) {
65-
var message = backgroundPage.getInfo().replace(/<[^>]*>?/g, '');
66-
var label = 'error';
67-
if (message.toLowerCase().startsWith("info")) {
68-
label = 'info';
69-
} else if (message.toLowerCase().startsWith("warn")) {
70-
label = 'warn';
65+
var state = backgroundPage.getState();
66+
// if (state == 0) {
67+
// document.getElementById('info').innerHTML = "Info: Found uGet and uget-chrome-wrapper";
68+
// } else if (state == 1) {
69+
// document.getElementById('warn').innerHTML = "Warning: Please update the uget-chrome-wrapper to the latest version";
70+
// } else {
71+
// document.getElementById('error').innerHTML = "Error: Unable to connect to the uget-chrome-wrapper";
72+
// }
73+
if (state == 0) {
74+
// document.getElementById('info').innerHTML = "Info: Found uGet and uget-chrome-wrapper";
75+
document.getElementById('info').style.display = 'block';
76+
document.getElementById('warn').style.display = 'none';
77+
document.getElementById('error').style.display = 'none';
78+
var element = document.getElementById("element-id");
79+
element.parentNode.removeChild(element);
80+
} else if (state == 1) {
81+
// document.getElementById('warn').innerHTML = "Warning: Please update the uget-chrome-wrapper to the latest version";
82+
document.getElementById('info').style.display = 'none';
83+
document.getElementById('warn').style.display = 'block';
84+
document.getElementById('error').style.display = 'none';
85+
} else {
86+
// document.getElementById('error').innerHTML = "Error: Unable to connect to the uget-chrome-wrapper";
87+
document.getElementById('info').style.display = 'none';
88+
document.getElementById('warn').style.display = 'none';
89+
document.getElementById('error').style.display = 'block';
7190
}
72-
document.getElementById(label).innerHTML = message;
7391
});
7492

7593
let interrupt = (localStorage["uget-interrupt"] == "true");

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ from os.path import splitext, basename, join, expanduser
2626
from mimetypes import guess_extension
2727

2828
cookie_filepath = join(tempfile.gettempdir(), 'uget_cookie')
29+
urls_filepath = join(tempfile.gettempdir(), 'uget_urls')
2930
UGET_COMMAND = "uget-gtk"
3031
VERSION = "2.0.4"
3132

@@ -79,6 +80,45 @@ def send_message(message):
7980
except Exception as e:
8081
logger.error('Error in sending message: ' + str(e))
8182

83+
def download_all_links(data):
84+
"""
85+
Extract all links from the html page and download them.
86+
"""
87+
urls = data['url']
88+
cookie = data['cookies']
89+
try:
90+
logger.debug('Writing links to file')
91+
with open(urls_filepath, 'w') as url_file:
92+
url_file.write(urls)
93+
94+
command = [UGET_COMMAND, "--input-file=" + urls_filepath]
95+
use_cookie_file = False
96+
if cookie:
97+
try:
98+
with open(cookie_filepath, 'w') as cookie_file:
99+
cookie_file.write(cookie)
100+
use_cookie_file = True
101+
except Exception as e:
102+
pass
103+
104+
if use_cookie_file:
105+
command.append("--http-cookie-file=" + cookie_filepath)
106+
# Pass the parameters to uGet
107+
subprocess.call(command)
108+
109+
if use_cookie_file:
110+
try:
111+
os.remove(cookie_filepath)
112+
except Exception as e:
113+
pass
114+
115+
try:
116+
os.remove(urls_filepath)
117+
except Exception as e:
118+
pass
119+
except Exception as e:
120+
pass
121+
82122
# Read messages from the webapp.
83123
def read_message():
84124

@@ -110,7 +150,10 @@ def read_message():
110150
data = json.loads(text)
111151
url = data['url']
112152

113-
if url:
153+
if url != "":
154+
if data['batch']:
155+
download_all_links(data)
156+
return
114157
use_cookie_file = False
115158
filename = data['filename']
116159
cookie = data['cookies']

0 commit comments

Comments
 (0)