Skip to content
Snippets Groups Projects
Commit bc1e09fd authored by Phil Höfer's avatar Phil Höfer
Browse files

Refactor tokensPerSearch, refillTreshold and targetStashSize management

parent 3f2b90a3
No related branches found
No related tags found
No related merge requests found
......@@ -8,10 +8,6 @@ const APIBASEURL = "https://metager.de";
const sites = ["https://metager.de","https://metager.org","https://metager3.de"];
const tokensPerSearch = 3; // TODO: move to storage
const refillThresholdSize = 3*tokensPerSearch;
const targetStashSize = 7*tokensPerSearch;
function updateStorageFromCookies() {sites.forEach((site) => {
let siteURL = new URL(site);
......@@ -117,6 +113,36 @@ function refillTokenStash(count) { // get count (max 10) tokens and store them i
pubkeyReq.send();
}
/**
* Returns the current tokensPerSearch value
* @returns {number}
*/
async function getTokensPerSearch() {
let { tokensPerSearch: storedValue } = await browser.storage.sync.get(
{
"tokensPerSearch": 1
}
);
return storedValue;
}
/**
* Returns the refill threshold size based on the currently saved tokensPerSearch value
* @returns {number}
*/
async function getRefillThresholdSize() {
return getTokensPerSearch().then((x) => x*3);
}
/**
* Returns the target stash size based on the currently saved tokensPerSearch value
* @returns {number}
*/
async function getTargetStashSize() {
return getTokensPerSearch().then((x) => x*7);
}
/**
* Checks token stash and refills if possible
* @returns {boolean} True if stash now holds at least as many tokens as the current tokensPerSearch value.
......@@ -134,6 +160,9 @@ async function checkTokenStash() {
);
console.log("current stash size: "+currentStash.length);
console.log(activeKey);
let refillThresholdSize = getRefillThresholdSize();
let targetStashSize = getTargetStashSize();
console.log(refillThresholdSize);
if (currentStash > refillThresholdSize) {
return true;
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment