PERBARUI

Auto perbarui Marketplace dengan flow baru & tombol start/stop

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         PERBARUI
// @namespace    https://www.facebook.com/behesty7
// @version      2.8
// @description  Auto perbarui Marketplace dengan flow baru & tombol start/stop
// @author       BEHESTY
// @match        https://web.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog*
// @match        https://www.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    let running = false;

    /* ==========================
       UI LOG + START/STOP BUTTON
       ========================== */

    const box = document.createElement('div');
    Object.assign(box.style, {
        position: 'fixed',
        bottom: '10px',
        right: '10px',
        width: '260px',
        background: 'rgba(0,0,0,0.85)',
        padding: '10px',
        borderRadius: '10px',
        color: '#fff',
        zIndex: 999999,
        fontSize: '12px',
        fontFamily: 'monospace'
    });

    box.innerHTML = `
        <button id="btnToggle"
            style="width:100%; padding:6px; margin-bottom:8px; background:#28a745; border:none; color:white; font-weight:bold; cursor:pointer; border-radius:5px;">
            START
        </button>
        <textarea id="logTerm" style="width:100%; height:200px; background:#111; color:#0f0; padding:5px; border:none; border-radius:5px; resize:none;"></textarea>
    `;
    document.body.appendChild(box);

    const logTerm = document.getElementById('logTerm');
    const btnToggle = document.getElementById('btnToggle');

    function log(msg) {
        const t = new Date().toLocaleTimeString();
        logTerm.value += `[${t}] ${msg}\n`;
        logTerm.scrollTop = logTerm.scrollHeight;
    }

    btnToggle.onclick = () => {
        running = !running;
        btnToggle.style.background = running ? "#dc3545" : "#28a745";
        btnToggle.textContent = running ? "STOP" : "START";

        if (running) {
            log("▶ START ditekan, memulai proses...");
            startMain();
        } else {
            log("⏹ STOP ditekan, menghentikan proses...");
        }
    };

    /* ==========================
       UTIL
       ========================== */
    const delay = (ms) => new Promise(res => setTimeout(res, ms));
    const randomDelay = () => delay(300 + Math.random() * 1000);

    /* ==========================
       STEP 1: CARI "Perbarui"
       ========================== */

    async function findUpdateButtons(maxTries = 90, interval = 1000) {
        log(`🔎 Mencari tombol "Perbarui" (max ${maxTries} percobaan)...`);

        for (let i = 1; i <= maxTries; i++) {
            if (!running) return [];

            const btns = [...document.querySelectorAll('div[aria-label="perbarui" i]')]
                .filter(b => b.offsetHeight > 0);

            if (btns.length) {
                log(`✅ Menemukan ${btns.length} tombol Perbarui`);
                return btns;
            }

            log(`⌛ Percobaan ${i}/${maxTries} - belum muncul`);
            await delay(interval);
        }

        log("⚠️ Tidak menemukan tombol Perbarui.");
        return [];
    }

    /* ==========================
       STEP 2: KLIK SEMUA PERBARUI
       ========================== */

    async function clickAllUpdateButtons(buttons) {
        for (let btn of buttons) {
            if (!running) return;
            btn.scrollIntoView({ behavior: "smooth", block: "center" });
            log("🔁 Klik Perbarui...");
            btn.click();
            await randomDelay();
        }
    }

    /* ==========================
       STEP 3: CARI TOMBOL SELESAI
       ========================== */

    async function clickDoneIfExists() {
        log("🔎 Mencari tombol Selesai...");

        for (let i = 0; i < 30; i++) {
            if (!running) return;

            const done = [...document.querySelectorAll('div[aria-label="Selesai"]')]
                .find(b => b.offsetHeight > 0);

            if (done) {
                done.scrollIntoView({ behavior: "smooth", block: "center" });
                log("✔ Klik tombol Selesai");
                done.click();
                return;
            }

            await delay(500);
        }

        log("⚠️ Tombol Selesai tidak ditemukan.");
    }

    /* ==========================
       STEP 4: COUNTDOWN + RELOAD
       ========================== */

    async function countdown(sec = 3) {
        for (let i = sec; i > 0; i--) {
            if (!running) return;
            log(`⏳ Reload dalam ${i} detik...`);
            await delay(1000);
        }
    }

    /* ==========================
       MAIN LOOP
       ========================== */

    async function startMain() {
        if (!running) return;

        log("⏱ Menunggu 2 detik sebelum mulai...");
        await delay(2000);

        while (running) {
            /* 1. Cari tombol perbarui */
            const buttons = await findUpdateButtons(90, 1000);

            if (!running) break;
            if (buttons.length === 0) {
                log("🏁 Tidak ada tombol Perbarui. Proses dihentikan.");
                break;
            }

            /* 2. Klik semuanya */
            await clickAllUpdateButtons(buttons);

            if (!running) break;

            /* 3. Klik Selesai jika ada */
            await clickDoneIfExists();

            if (!running) break;

            /* 4. Countdown */
            await countdown(20);

            /* 5. Reload halaman */
            log("🔄 Reload halaman untuk batch berikutnya...");
            window.location.href = 'https://web.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog=true';
            break;
        }
    }

    /* AUTO START */
    running = true;
    btnToggle.textContent = "STOP";
    btnToggle.style.background = "#dc3545";
    log("▶ AUTO START aktif. Memulai proses...");
    startMain();

})();