Skip to content

Commit

Permalink
Refactored API calls + option page
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Rigal committed Sep 29, 2021
1 parent 1a96c2b commit a241ce3
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 246 deletions.
131 changes: 131 additions & 0 deletions js/pyload-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
function getServerStatus(callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/statusServer`, true);
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);
}
}
}
xhr.timeout = 5000;
xhr.ontimeout = function() {
if (callback) callback(false, 'Server unreachable');
}
xhr.send();
}

function login(username, password, callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/login`, true);
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 {
if (callback) callback(false, 'Login failed, invalid credentials');
}
}
}
xhr.timeout = 5000;
xhr.ontimeout = function() {
if (callback) callback(false, 'Login failed, server unreachable');
}
xhr.send(`username=${username}&password=${password}`);
}

function getStatusDownloads(callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/statusDownloads`, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
const status = JSON.parse(xhr.responseText);
if (callback) callback(status);
}
}
xhr.send();
}

function getQueueData(callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/getQueueData`, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
const queueData = JSON.parse(xhr.responseText);
const urls = [];
queueData.forEach(pack => {
pack.links.forEach(link => {
urls.push(link.url);
});
});
if (callback) callback(urls);
}
}
xhr.send();
}

function getLimitSpeedStatus(callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/getConfigValue?category="download"&option="limit_speed"`, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
const limitSpeed = JSON.parse(xhr.responseText).toLowerCase() === 'true';
if (callback) callback(limitSpeed);
}
}
xhr.send();
}

function setLimitSpeedStatus(limitSpeed, callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/setConfigValue?category="download"&option="limit_speed"&value="${limitSpeed}"`, true);
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);
}
}
xhr.send();
}

function addPackage(name, url, callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/addPackage`, true);
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);
}
}
}
const safeName = name.replace(/[^a-z0-9._\-]/gi, '_');
xhr.send(`name="${encodeURIComponent(safeName)}"&links=["${encodeURIComponent(url)}"]`);
}

function checkURL(url, callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', `${origin}/api/checkURLs`, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
const response = JSON.parse(xhr.responseText);
if (callback) callback(!response.hasOwnProperty('BasePlugin') && !response.hasOwnProperty('error'));
}
}
xhr.send(`urls=["${encodeURIComponent(url)}"]`);
}
35 changes: 35 additions & 0 deletions js/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
let serverPort, serverIp, serverProtocol, origin;


function pullStoredData(callback) {
chrome.storage.sync.get(['serverIp', 'serverPort', 'serverProtocol'], function(data) {
serverIp = data.serverIp || '172.0.0.1';
serverPort = data.serverPort || 8001;
serverProtocol = data.serverProtocol || 'http';
origin = `${serverProtocol}://${serverIp}:${serverPort}`;
if (callback) callback(data);
});
}

function isLoggedIn(callback) {
getServerStatus(function(success) {
if (callback) {
if (success) callback(true);
else callback(false);
}
});
}

function setOrigin(ip, port, protocol, callback) {
serverIp = ip;
serverPort = port;
serverProtocol = protocol;
origin = `${serverProtocol}://${serverIp}:${serverPort}`;
chrome.storage.sync.set({
serverIp: serverIp,
serverPort: serverPort,
serverProtocol: serverProtocol
}, function () {
if (callback) callback();
});
}
26 changes: 17 additions & 9 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/all.min.css">
</head>
<body>
<div class="text-center h1 m-5">
PyLoad Assistant Options
Yape Options
</div>
<div class="d-flex justify-content-center m-4">
<div class="d-flex justify-content-center">
<form>
<div class="form-group">
<label for="serverIp">PyLoad server's IP address</label>
Expand All @@ -21,22 +22,24 @@
<input type="checkbox" class="form-check-input" id="useHTTPS">
<label class="form-check-label" for="useHTTPS">Use HTTPS</label>
</div>
<div class="d-flex justify-content-center">
<div id="loginStatusOK" class="alert alert-success mt-4" hidden>
<i class="fa fa-check small mr-3"></i>You are logged in
</div>
<div id="loginStatusKO" class="alert alert-danger mt-4" hidden>
<i class="fa fa-times small mr-3"></i>You are not logged in
</div>
</div>
</form>
</div>
<div class="d-flex justify-content-center align-items-center" id="spinnerDiv">
</div>
<div class="d-flex justify-content-center">
<div class="m-2">
<button id="saveButton" class="btn btn-primary m-1">Save</button>
<button id="loginButton" class="btn btn-primary m-1">Login</button>
<button id="loginButton" class="btn btn-primary m-1" hidden>Login</button>
</div>
</div>
<div class="d-flex justify-content-center">
<div id="alertSuccess" class="alert alert-success m-2" hidden></div>
</div>
<div class="d-flex justify-content-center">
<div id="alertDanger" class="alert alert-danger m-2" hidden></div>
</div>


<div id="loginModal" class="modal" tabindex="-1" role="dialog">
Expand All @@ -59,6 +62,9 @@ <h5 class="modal-title">Login</h5>
<label for="password">Password</label>
<input type="password" class="form-control" id="password">
</div>
<div class="d-flex justify-content-center">
<div id="alertDanger" class="alert alert-danger m-2" hidden></div>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -72,5 +78,7 @@ <h5 class="modal-title">Login</h5>

<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/pyload-api.js"></script>
<script type="text/javascript" src="js/storage.js"></script>
<script src="options.js"></script>
</html>
Loading

0 comments on commit a241ce3

Please sign in to comment.