-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rémi Rigal
committed
Sep 29, 2021
1 parent
1a96c2b
commit a241ce3
Showing
6 changed files
with
265 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}"]`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.