Skip to content

Commit

Permalink
Added context menu downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Rigal committed Sep 29, 2021
1 parent a241ce3 commit f8b89da
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Features:
- One-click download
- Monitor current downloads
- Monitor global bandwidth usage & one-click speed limiter
- Context menu downloads

## Usage

Expand All @@ -15,6 +16,7 @@ Go to the option page by clicking on the `settings` icon and fill the IP address
The current downloads are always displayed.
If the current active tab has a downloadable file, an extra panel will be displayed with a button to start the download.
A download can also be added by right-clicking on a link and selecting `Download with Yape`.


## Screenshots
Expand Down
119 changes: 119 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
importScripts('js/storage.js');

const notify = function(title, message) {
return chrome.notifications.create('', {
type: 'basic',
title: title || 'Yape',
message: message || '',
iconUrl: './images/icon.png',
});
}

const loadToastr = function(tab, callback) {
chrome.scripting.insertCSS({
target: {tabId: tab.id},
files: ['css/toastr.min.css']
}, function() {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['js/lib/jquery-3.5.1.min.js', 'js/lib/toastr.min.js']
}, function() {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => {
toastr.options = {
closeButton: false,
newestOnTop: false,
progressBar: false,
positionClass: 'toastr-top-right',
containerId: 'toastr-container',
toastClass: 'toastr',
iconClasses: {
error: 'toastr-error',
info: 'toastr-info',
success: 'toastr-success',
warning: 'toastr-warning'
},
iconClass: 'toastr-info',
titleClass: 'toastr-title',
messageClass: 'toastr-message',
closeClass: 'toastr-close-button',
timeOut: 8000
};
}
}, function() {
callback();
});
});
});
}

const sendToast = function(tab, type, message) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: (type, message) => {
toastr.remove();
toastr[type](message);
},
args: [type, message]
});
}

const downloadLink = function(info, tab) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000)
fetch(`${origin}/api/statusServer`, { signal: controller.signal })
.then(response => response.json())
.then(json => {
clearTimeout(timeoutId);
if (json.hasOwnProperty('error')) {
if (json.error === 'Forbidden') sendToast(tab, 'error', `Invalid credentials, make sure you are logged in`);
else sendToast(tab, 'error', `Server unreachable`);
return;
}
fetch(`${origin}/api/checkURLs?urls=["${encodeURIComponent(info.linkUrl)}"]`)
.then(response => response.json())
.then(json => {
if (json.hasOwnProperty('error')) {
sendToast(tab, 'error', `Error checking url: ${json}`);
return;
}
const safeName = encodeURIComponent(info.linkUrl.replace(/[^a-z0-9._\-]/gi, '_'));
fetch(`${origin}/api/addPackage?name="${safeName}"&links=["${encodeURIComponent(info.linkUrl)}"]`)
.then(response => response.json())
.then(json => {
if (json.hasOwnProperty('error')) {
sendToast(tab, 'error', `Error requesting download: ${json}`);
return;
}
sendToast(tab, 'success', 'Download added successfully');
});
});
})
.catch(e => sendToast(tab, 'error', `Server unreachable`));
}

chrome.runtime.onInstalled.addListener( () => {
chrome.contextMenus.create({
id: 'yape',
title: 'Download with Yape',
contexts:['link']
});
});

chrome.runtime.onMessage.addListener( data => {
if (data.type === 'notification') {
notify(data.title, data.message);
}
});

chrome.contextMenus.onClicked.addListener( ( info, tab ) => {
if ('yape' === info.menuItemId) {
loadToastr(tab, function() {
pullStoredData(function() {
sendToast(tab, 'info', 'Requesting download...');
downloadLink(info, tab);
});
});
}
} );
6 changes: 6 additions & 0 deletions css/toastr.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions js/lib/toastr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions js/pyload-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ function getServerStatus(callback) {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
const response = JSON.parse(xhr.responseText);
if (response.hasOwnProperty('error')) {
if (callback) callback(false, response.error);
} else {
if (callback) callback(true, null, response);
try {
const response = JSON.parse(xhr.responseText);
if (response.hasOwnProperty('error')) {
if (callback) callback(false, response.error);
} else {
if (callback) callback(true, null, response);
}
} catch {
if (callback) callback(false, 'Server unreachable');
}
}
}
Expand All @@ -25,7 +29,6 @@ function login(username, password, callback) {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
if (JSON.parse(xhr.responseText) !== false) {
if (callback) callback(true);
} else {
Expand Down Expand Up @@ -91,7 +94,6 @@ function setLimitSpeedStatus(limitSpeed, callback) {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
const success = JSON.parse(xhr.responseText);
if (callback) callback(success);
}
Expand Down
11 changes: 9 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Yape",
"version": "0.0.4",
"version": "0.0.5",
"description": "Extension for PyLoad to easily monitor and add downloads",
"permissions": ["activeTab", "storage"],
"permissions": ["activeTab", "storage", "contextMenus", "scripting"],
"host_permissions": ["http://*/", "https://*/"],
"action": {
"default_popup": "popup.html",
Expand All @@ -20,5 +20,12 @@
"128": "images/icon.png"
},
"options_page": "options.html",
"background": {
"service_worker": "background.js"
},
"web_accessible_resources": [{
"resources": ["js/lib/toastr.min.js", "js/lib/jquery-3.5.1.min.js", "css/toastr.min.css"],
"matches": ["<all_urls>"]
}],
"manifest_version": 3
}
Loading

0 comments on commit f8b89da

Please sign in to comment.