Shein Size Filter

Displays shirt length on tab title

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Shein Size Filter
// @namespace    http://tampermonkey.net/
// @version      2025-04-23
// @description  Displays shirt length on tab title
// @author       Hammad Arif
// @match        *://au.shein.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    console.log("Tampermonkey script loaded");

    var sleepDurationMs=5000;
    var idealLengthMax=70;
    var sizeAttribute="M";

    let hasRun = false;

    function safeRunScript() {
        if (!hasRun) {
            hasRun = true;
            runScript();
        }
    }

      // Helper sleep function that returns a Promise
    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function getLengthValueWithRetries(sizeDiv, maxRetries = 3, sleepDurationMs = 2000) {

        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            console.log(`Attempt ${attempt}: finding gbRawData`);
            if (gbRawData != null) {
                var lengthItem= gbRawData?.modules?.saleAttr?.sizeInfo?.sizeInfo?.find(s=>s?.attr_value_name==sizeAttribute)["Length "]
                if (lengthItem!=null)
                    break;
            }
            await sleep(sleepDurationMs);
        }

        if (lengthItem) {
            const value = parseFloat(lengthItem);
            console.log(`✅ Found Length: ${value}`);
            return value;
        }

        console.error("🚫 Failed to find Length item after all attempts.");
        return null;
    }

    async function runScript() {

        console.log("runScript executing on load");

        const lengthValue = await getLengthValueWithRetries();

        if (lengthValue!=null)
        {
            const originalTitle = document.title;

            if (lengthValue <=idealLengthMax) {
                document.title = `💖️ Length ${lengthValue} | ${originalTitle}`;}
            else {
                document.title = `Length ${lengthValue} | ${originalTitle}`;}
        }
        else
        {
            console.warn("Length value not found!");
        }

    }

    // Run when page fully loads
    window.addEventListener('load', safeRunScript);

    // Or run after 10 seconds anyway
    setTimeout(safeRunScript, 10000);

})();