Skip to content

Instantly share code, notes, and snippets.

@driversti
Last active January 8, 2024 19:40
Show Gist options
  • Save driversti/eb91d3bf5ccc05bc9c1bbea808d6c7ce to your computer and use it in GitHub Desktop.
Save driversti/eb91d3bf5ccc05bc9c1bbea808d6c7ce to your computer and use it in GitHub Desktop.
Automatic activation 3 min PP boosters
// ==UserScript==
// @name 3min PP boosters activation
// @namespace https://www.erepublik.com/
// @version 1.1
// @description Activate all 3min PP boosters with one click.
// @copyright YOU ARE NOT ALLOWED TO DISCLOSE THIS SCRIPT TO OTHERS WITHOUT MY PERMISSION!!!
// @author driversti https://www.erepublik.com/en/citizen/profile/4690052
// @match https://www.erepublik.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
// @grant none
// ==/UserScript==
(function () {
'use strict';
const erpk = erepublik.settings.pomelo.authToken;
const _token = SERVER_DATA.csrfToken;
const timeoutBetweenActivations = 500; // in ms
// Create regular expressions for the URLs
//const inventoryRegex = new RegExp('/[a-z]{2}/main/inventory');
const inventoryNewRegex = new RegExp('/[a-z]{2}/main/inventory-new');
// Check if the current URL matches one of the regular expressions
if (!inventoryNewRegex.test(window.location.href)) {
// console.log("Not on inventory page, exiting");
return;
}
console.log("3min PP boosters activation script loaded");
function addEmojiToBoostersElement(boostersElement) {
const emoji = document.createElement('div');
emoji.textContent = '⚠️'; // Yellow triangle emoji
emoji.style.position = 'absolute';
emoji.style.cursor = 'pointer';
emoji.style.fontSize = '24px';
emoji.style.right = '5px';
emoji.style.top = '0';
emoji.style.zIndex = '1000';
getBoostersCount()
.then(boostersCount => {
if (boostersCount === 0) {
console.log('No boosters found');
return;
}
const confirmMessage = 'Do you want to activate them all?\n' +
'Please do not close the browser tab until all boosters are active. ' +
'The process will end in approximately ' + (boostersCount * timeoutBetweenActivations / 1000) + ' seconds.';
emoji.addEventListener('click', () => {
if (confirm(confirmMessage)) {
activateBoosters(boostersCount);
} else {
console.log('Boosters activation canceled');
}
});
})
.catch(error => {
console.error('Error in getBoostersCount:', error);
});
//boostersElement.style.position = 'relative';
boostersElement.appendChild(emoji);
}
function activateBoosters(boostersCount) {
updateActivationProgress(boostersCount);
activateBooster()
.then(data => {
const boostersLeft = data?.inventoryItems?.finalProducts?.items?.['100_prestige_points_1_180']?.amount ?? 0;
if (boostersLeft === 0) {
console.log('All boosters activated. Exiting...');
return;
}
setTimeout(() => activateBoosters(boostersLeft), timeoutBetweenActivations);
})
.catch(error => {
console.error('Error activating boosters:', error);
})
}
function activateBooster() {
return fetch('https://www.erepublik.com/en/economy/activateBooster', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json, text/plain, */*',
'Origin': 'https://www.erepublik.com',
'Referer': 'https://www.erepublik.com/en/main/inventory-new',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
},
body: `type=prestige_points&quality=1&duration=180&fromInventory=true&_token=${_token}`
}).then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}).catch(error => {
console.error('Error activating booster:', error);
});
}
async function getBoostersCount() {
try {
const data = await getInventoryJson();
return countSpecificBooster(data, '100_prestige_points_1_180_temporary');
} catch (error) {
console.error('Error in getBoostersCount:', error);
throw error; // re-throwing error so it can be handled in the caller
}
}
function countSpecificBooster(activationBoosterResponse, boosterId) {
let count = 0;
activationBoosterResponse.forEach(category => {
if (category.items && Array.isArray(category.items)) {
category.items.forEach(item => {
if (item.id === boosterId) {
count = item.amount;
}
});
}
});
console.log(`There are ${count} boosters of type ${boosterId}`);
return count;
}
function getInventoryJson() {
return fetch('https://www.erepublik.com/en/economy/inventory-json', {
method: 'GET',
headers: {
'Accept': 'application/json, text/plain, */*',
'Origin': 'https://www.erepublik.com',
'Referer': 'https://www.erepublik.com/en/main/inventory-new',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
});
}
function updateActivationProgress(boostersLeft) {
const timeLeft = boostersLeft * timeoutBetweenActivations / 1000; // in seconds
const progressText = `Boosters left: ${boostersLeft}, time left: ${timeLeft} seconds`;
console.log(progressText);
}
let boostersLookupCounter = 1;
const boostersLookupAttempts = 5;
async function boostersElementLookup() {
const boostersElement = document.getElementById("inventory_100_prestige_points_1_180_temporary_boosters");
if (boostersElement) {
addEmojiToBoostersElement(boostersElement)
return;
}
if (!boostersElement && boostersLookupCounter >= boostersLookupAttempts) {
console.log("Boosters element not found, exiting");
return;
}
boostersLookupCounter++;
console.log(`Boosters element not found, attempt ${boostersLookupCounter}/${boostersLookupAttempts} in 500ms`);
setTimeout(boostersElementLookup, 500);
}
boostersElementLookup()
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment