Improved token handling, CSRF expiry check

This commit is contained in:
Abhorrent_Anger 2026-08-19 18:30:25 +03:00
parent f424787db7
commit 3fd55c5214

View File

@ -1,10 +1,10 @@
// ==UserScript==
// @name Mann Co. Store Giveaway Autojoiner
// @namespace http://tampermonkey.net/
// @version 2026-08-21
// @version 2026-08-22
// @description Tries to join giveaways on page load
// @author Abhorrent_Anger
// @include /^https?:\/\/mannco\.store\/giveaways.?$/
// @include /^https?:\/\/mannco\.store\/giveaways.*?/
// @icon https://www.google.com/s2/favicons?sz=64&domain=mannco.store
// @grant GM_xmlhttpRequest
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
@ -15,22 +15,34 @@
setTimeout(function () { location.reload(); }, 90 * 60 * 1000);
const OPEN_GIVEAWAY_SELECTOR = '.giveaways-list-item:not(.giveaways-list-item--joined)';
const NUXT_DATA_SELETOR = '#__NUXT_DATA__';
var bearerToken = '';
var csrfToken = '';
waitForKeyElements('.giveaways__content', () => {
if ($(OPEN_GIVEAWAY_SELECTOR).len == 0) {
return true;
}
updateTokensandJoin();
if (updateTokens() && bearerToken && csrfToken) {
joinGiveaways(bearerToken, csrfToken);
} else {
console.log('Couldn\'t fetch tokens for joining the giveaways!');
}
console.log('Done with giveaway join attempts');
});
function updateTokensandJoin() {
var bearerToken = getTokenFromCookie('auth');
var csrfToken = getTokenFromCookie('csrf');
function updateTokens() {
bearerToken = getTokenFromCookie('auth');
if (bearerToken) {
console.log('Fetched the Bearer token from cookies: ', bearerToken);
} else {
console.log('Couldn\'t fetch the Bearer token from cookies: ', bearerToken);
return false;
}
csrfToken = getTokenFromCookie('csrf');
if (csrfToken) {
console.log('Fetched the CSRF token from cookies: ', csrfToken)
joinGiveaways(bearerToken, csrfToken);
return true;
}
let xCSRF = new GM_xmlhttpRequest({
@ -42,20 +54,25 @@ function updateTokensandJoin() {
if (response.status == 200) {
let array = JSON.parse(response.response);
csrfToken = array['content']['csrf_token'];
console.log('Successfully updated the CSRF token: ', csrfToken)
joinGiveaways(bearerToken, csrfToken);
console.log('Successfully updated the CSRF token from API: ', csrfToken)
return true;
} else {
console.log('Couldn\'t get a CSRF token!')
}
}
}
});
return false;
}
function joinGiveaways(bearerToken, csrfToken) {
$(OPEN_GIVEAWAY_SELECTOR).each(function () {
let giveawayURL = $(this).attr('href');
let giveaway = /[^/]*$/.exec(giveawayURL)[0];
if (!giveaway || giveaway == 'undefined') {
console.log('Couldn\'t detect the giveaway ID!', $(this), giveawayURL, giveaway);
return;
}
let giveawayJoinURL = 'https://api.mannco.store/giveaways/join/' + giveaway;
let xJoin = new GM_xmlhttpRequest({
method: 'POST',
@ -84,9 +101,12 @@ function getTokenFromCookie(tokenName) {
let cookieArray = getCookie(tokenName);
let decodedTokenArray = decodeURIComponent(cookieArray);
if (typeof decodedTokenArray !== 'string' || !decodedTokenArray) {
if (typeof decodedTokenArray !== 'string' || !decodedTokenArray || decodedTokenArray == undefined) {
return false;
}
let array = JSON.parse(decodedTokenArray, true);
if (array['expiresAt'] === undefined || array['expiresAt'] > Date.now()) {
return array['token'];
}
return false;
}