ggu_userData

userdata do ggutils

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.greatest.deepsurf.us/scripts/487863/1359256/ggu_userData.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

const config = {
    "endpoint_user": "https://ggmax.com.br/api/user",
    "endpoint_auth": "https://ggmax.com.br/api/auth",
    "endpoint_orders": "https://ggmax.com.br/api/orders",
    "endpoint_announcements": "https://ggmax.com.br/api/announcements",
}
async function refreshToken(ref_token) {
    ref_token = ref_token.replace('Bearer ', '');
    let body = JSON.stringify({
        refresh_token: ref_token
    });
    var req = await fetch(`${config.endpoint_auth}/refresh-token`, {
        "headers": {
            "content-type": "application/json",
        },
        "body": body,
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    });

    let json = await req.json();
    return json;
}

class user {
    constructor() {
        this.data = () => {
            let cookies = document.cookie.split('; ');
            let authData = cookies.find(cookie => cookie.startsWith('auth='));
            authData = JSON.parse(decodeURIComponent(authData.split('=')[1]));
            return {
                token: authData.accessToken,
                refreshToken: authData.refreshToken
            }
        }
    }
    cookies() {
        return {
            get: (cname) => {
                let name = cname + "=";
                let decodedCookie = decodeURIComponent(document.cookie);
                let ca = decodedCookie.split(';');
                for (let i = 0; i < ca.length; i++) {
                    let c = ca[i];
                    while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                    }
                    if (c.indexOf(name) == 0) {
                        return c.substring(name.length, c.length);
                    }
                }
                return "";
            },
            set: (cname, cvalue) => {
                document.cookie = `${encodeURIComponent(cname)}=${encodeURIComponent(cvalue)}`;
            }
        }
    }

    async refresh() {
        let data = this.data();
        const json = await refreshToken(data.refreshToken);

        if (json.success) {
            this.cookies().set('auth._refresh_token.local', json.data.token);
        }
    }

    async data() {
        let data = this.data();
        return data;
    }


}