diff --git a/background.js b/background.js index 1a32fccb2e102a0cf4400de6d85b27c0b897397b..01e53febd0ccf40079b9f512bad79aa180587f32 100644 --- a/background.js +++ b/background.js @@ -45,8 +45,13 @@ function updateStorageFromCookies() {sites.forEach((site) => { // TODO: Collect token if unused -function refillTokenStash(count) { // get count (max 10) tokens and store them in the stash - //console.log("refilling tokens"); +/** + * Get count (max 10) tokens and store them in the stash + * @param {number} count + * @returns {boolean} True if successfull, false otherwise. + */ +function refillTokenStash(count) { // TODO: handle count >10 + console.log("refilling tokens"); let tokensToBlind = []; for (let i = 0; i <= Math.min(count,9); i++) { tokensToBlind.push(uuidv4()); @@ -131,7 +136,7 @@ async function getTokensPerSearch() { * Returns the refill threshold size based on the currently saved tokensPerSearch value * @returns {number} */ -async function getRefillThresholdSize() { +function getRefillThresholdSize() { return getTokensPerSearch().then((x) => x*3); } @@ -139,7 +144,7 @@ async function getRefillThresholdSize() { * Returns the target stash size based on the currently saved tokensPerSearch value * @returns {number} */ -async function getTargetStashSize() { +function getTargetStashSize() { return getTokensPerSearch().then((x) => x*7); } @@ -162,11 +167,12 @@ async function checkTokenStash() { console.log(activeKey); let refillThresholdSize = getRefillThresholdSize(); let targetStashSize = getTargetStashSize(); - console.log(refillThresholdSize); if (currentStash > refillThresholdSize) { return true; + } else if (activeKey) { + return await refillTokenStash(targetStashSize - currentStash.length); } else { - return refillTokenStash(targetStashSize - currentStash.length); + return false; } } setTimeout(checkTokenStash,10000);