Skip to content

Commit

Permalink
added linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Sep 26, 2021
1 parent 812f06a commit 8ebeac3
Show file tree
Hide file tree
Showing 7 changed files with 1,658 additions and 83 deletions.
17 changes: 17 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true,
"webextensions": true
},
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018
},
"rules": {
"linebreak-style": ["error", "unix"]
}
}
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
109 changes: 61 additions & 48 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Aria2 from 'aria2';

function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(
value
);
}

function downloadAgent() {
Expand All @@ -17,16 +19,18 @@ function downloadAgent() {
}
if (!result.motrixapikey) {
// API KEY is not set, triggers an alert to the user
alert('API key not set, please set a random API key by clicking on the extension icon. Open Motrix ' +
'set the same API Key by visiting Preferences > Advanced > RPC Secret');
alert(
'API key not set, please set a random API key by clicking on the extension icon. Open Motrix ' +
'set the same API Key by visiting Preferences > Advanced > RPC Secret'
);
} else {
// API Key is set, Proceed with download
const options = {
host: '127.0.0.1',
port: 16800,
secure: false,
secret: result.motrixapikey,
path: '/jsonrpc'
path: '/jsonrpc',
};
const aria2 = new Aria2(options);
let downloadUrl = '';
Expand All @@ -43,59 +47,66 @@ function downloadAgent() {
let params = {};
// If the download have a specified path, ie user selected via file manager
if (downloadItem.filename) {
var directory, filename;
filename = downloadItem.filename.replace(/^.*[\\\/]/, '');
directory = downloadItem.filename.match(/(.*)[\/\\]/)[1] || '';
let directory, filename;
filename = downloadItem.filename.replace(/^.*[\\/]/, '');
directory = downloadItem.filename.match(/(.*)[/\\]/)[1] || '';
// Appends path to the options
params = {
dir: directory,
out: filename
out: filename,
};
}
if (downloadItem.referrer) {
params = {
...params,
referer: downloadItem.referrer,
}
};
}
await aria2.call("addUri", [downloadUrl], params).then(async () => {
// Added successfully: Cancels and removes the download from browser download manager
function onFinished() { }
await aria2
.call('addUri', [downloadUrl], params)
.then(async () => {
// Added successfully: Cancels and removes the download from browser download manager
function onFinished() {}

function onError(error) {
console.error(`Error: ${error}`);
}
function onError(error) {
console.error(`Error: ${error}`);
}

var removing = browser.downloads.removeFile(downloadItem.id);
removing.then(onFinished).catch(onError);
var canceling = browser.downloads.cancel(downloadItem.id);
canceling.then(onFinished).catch(onError);
var erasing = browser.downloads.erase({ id: downloadItem.id });
erasing.then(onFinished).catch(onError);
const removing = browser.downloads.removeFile(downloadItem.id);
removing.then(onFinished).catch(onError);
const canceling = browser.downloads.cancel(downloadItem.id);
canceling.then(onFinished).catch(onError);
const erasing = browser.downloads.erase({ id: downloadItem.id });
erasing.then(onFinished).catch(onError);

// Shows notification
if (result.enablenotifications) {
const notificationOptions = {
type: 'basic',
iconUrl: 'images/icon-large.png',
title: 'Motrix WebExtension',
message: 'Download started in Motrix download manger'
};
var notificationId = Math.round((new Date()).getTime() / 1000).toString();
browser.notifications.create(notificationId, notificationOptions);
browser.notifications.onClicked.addListener((id) => {
if (id == notificationId) {
browser.tabs.create({url:'motrix://'});
}
});
}
setTimeout(() => browser.runtime.reload(), 1000);
}).catch((err) => {
console.error(err);
// Failed: Show alert, Allows download to continue in browser
alert('Motrix not installed or configured properly, Open Motrix set a API Key by visiting Preferences' +
' > Advanced > RPC Secret');
});
// Shows notification
if (result.enablenotifications) {
const notificationOptions = {
type: 'basic',
iconUrl: 'images/icon-large.png',
title: 'Motrix WebExtension',
message: 'Download started in Motrix download manger',
};
const notificationId = Math.round(
new Date().getTime() / 1000
).toString();
browser.notifications.create(notificationId, notificationOptions);
browser.notifications.onClicked.addListener((id) => {
if (id === notificationId) {
browser.tabs.create({ url: 'motrix://' });
}
});
}
setTimeout(() => browser.runtime.reload(), 1000);
})
.catch((err) => {
console.error(err);
// Failed: Show alert, Allows download to continue in browser
alert(
'Motrix not installed or configured properly, Open Motrix set a API Key by visiting Preferences' +
' > Advanced > RPC Secret'
);
});
}
}

Expand All @@ -104,17 +115,19 @@ function downloadAgent() {
}

// Triggered whenever a new download event fires
let getResult = browser.storage.sync.get(['motrixapikey', 'extensionstatus', 'enablenotifications']);
let getResult = browser.storage.sync.get([
'motrixapikey',
'extensionstatus',
'enablenotifications',
]);
getResult.then(onGot, onError);

});
}

browser.runtime.onStartup.addListener(function () {
downloadAgent();
});


browser.runtime.onInstalled.addListener(function () {
downloadAgent();
});
});
49 changes: 26 additions & 23 deletions app/scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,48 @@ const keyInput = document.getElementById('motrixapikey');
const extensionStatus = document.getElementById('extensionstatus');
const enablenotifications = document.getElementById('enablenotifications');

function onGot(result) {
if (!result.motrixapikey) {
// Sets API key to null if it is not found else gets value of key and sets it to input box
// Gets extension status and sets default value: true
browser.storage.sync
.get(['motrixapikey', 'extensionstatus', 'enablenotifications'])
.then(
(result) => {
if (!result.motrixapikey) {
browser.storage.sync.set({ motrixapikey: null });
keyInput.value = '';
} else {
} else {
keyInput.value = result.motrixapikey;
}
if (typeof result.extensionstatus === 'undefined') {
}
if (typeof result.extensionstatus === 'undefined') {
browser.storage.sync.set({ extensionstatus: true });
extensionStatus.checked = true;
} else {
} else {
extensionStatus.checked = result.extensionstatus;
}
if (typeof result.enablenotifications === 'undefined') {
}
if (typeof result.enablenotifications === 'undefined') {
browser.storage.sync.set({ enablenotifications: true });
enablenotifications.checked = true;
} else {
} else {
enablenotifications.checked = result.enablenotifications;
}
},
(error) => {
console.error(`Error: ${error}`);
}
}

function onError(error) {
console.error(`Error: ${error}`);
}
// Sets API key to null if it is not found else gets value of key and sets it to input box
// Gets extension status and sets default value: true
let getResult = browser.storage.sync.get(['motrixapikey', 'extensionstatus', 'enablenotifications']);
getResult.then(onGot, onError);
);

// Saves the key to the storage
setButton.addEventListener('click', () => {
browser.storage.sync.set({ motrixapikey: keyInput.value ? keyInput.value : null });
window.close();
browser.storage.sync.set({
motrixapikey: keyInput.value ? keyInput.value : null,
});
window.close();
});

extensionStatus.addEventListener('click', function (e) {
browser.storage.sync.set({ extensionstatus: e.target.checked });
browser.storage.sync.set({ extensionstatus: e.target.checked });
});

enablenotifications.addEventListener('click', function (e) {
browser.storage.sync.set({ enablenotifications: e.target.checked });
});
browser.storage.sync.set({ enablenotifications: e.target.checked });
});
Loading

0 comments on commit 8ebeac3

Please sign in to comment.