PERBARUI

Otomatis klik 'Perbarui' di Facebook Marketplace dengan fallback jika tombol tidak muncul karena error jaringan/render lambat.

// ==UserScript==
// @name         PERBARUI
// @namespace    https://www.facebook.com/behesty7
// @version      1.2
// @description  Otomatis klik 'Perbarui' di Facebook Marketplace dengan fallback jika tombol tidak muncul karena error jaringan/render lambat.
// @author       BEHESTY
// @match        https://www.facebook.com/marketplace/selling/renew*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';
    let running = true;

    const logBox = document.createElement('div');
    Object.assign(logBox.style, {
        position: 'fixed',
        bottom: '10px',
        right: '10px',
        background: 'rgba(0,0,0,0.8)',
        color: '#fff',
        padding: '10px',
        maxHeight: '300px',
        overflowY: 'auto',
        fontSize: '12px',
        zIndex: '9999',
        borderRadius: '8px',
    });
    logBox.innerText = '📢 Auto Update FB Marketplace\n';
    document.body.appendChild(logBox);

    function log(message) {
        const logBox = document.getElementById('terminator-log');
        if (logBox) {
            const timestamp = new Date().toLocaleTimeString();
            logBox.value += `[${timestamp}] ${message}\n`;
            logBox.scrollTop = logBox.scrollHeight;
        }
    }

    function delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function countdown(seconds) {
  for (let i = seconds; i > 0; i--) {
    if (!running) {
      log('⏹ Dihentikan saat countdown.');
      return;
    }
    log(`⏳ Menunggu ${i} detik...`);
    await delay(1000);
  }
}

    function randomDelay(min = 500, max = 2000) {
        return delay(Math.floor(Math.random() * (max - min + 1)) + min);
    }

    async function waitForVisibleElement(selector, maxTries = 60, interval = 500) {
        for (let i = 0; i < maxTries; i++) {
            const el = Array.from(document.querySelectorAll(selector))
                .find(e => e.offsetHeight > 0 && e.offsetWidth > 0);
            if (el) return el;
            await delay(interval);
        }
        return null;
    }

    async function clickToBeUpdatedIfAvailable() {
        log('🔎 Mencari tombol "Untuk diperbarui"...');
        for (let i = 0; i < 60; i++) {
            const anchors = Array.from(document.querySelectorAll('a[role="link"]'))
                .filter(a => a.innerText.includes('Untuk diperbarui') && a.offsetHeight > 0);
            const btn = anchors[0];
            if (btn) {
                const match = btn.innerText.match(/\d+/);
                const count = match ? parseInt(match[0]) : 0;
                if (count > 0) {
                    log(`📌 Klik "Untuk diperbarui" (${count} listing)`);
                    btn.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    btn.click();
                    return true;
                } else {
                    log('✅ Jumlah "Untuk diperbarui" = 0. Menghentikan proses.');
                    return false;
                }
            }
            await delay(500);
        }

        log('⚠️ Tidak menemukan tombol "Untuk diperbarui", mencoba ulang ke /selling/');
        window.location.href = 'https://www.facebook.com/marketplace/selling/';
        return false;
    }

    async function clickUpdateButtons() {
        log('🔎 Menunggu tombol "Perbarui"...');
        for (let i = 0; i < 60; i++) {
            const buttons = Array.from(document.querySelectorAll('div[aria-label="Perbarui"]'))
                .filter(btn => btn.offsetHeight > 0);
            if (buttons.length > 0) {
                log(`🔧 Menemukan ${buttons.length} tombol "Perbarui"`);
                for (const btn of buttons) {
                    btn.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    log('🔁 Klik tombol "Perbarui"...');
                    btn.click();
                    await randomDelay();
                }
                return true;
            }
            await delay(500);
        }

        log('⚠️ Tidak menemukan tombol "Perbarui", kembali ke /selling/ dan mulai ulang');
//        window.location.href = 'https://www.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog=true';
        return false;
    }

    async function clickDoneButtonIfExists() {
        log('🔎 Mencari tombol "Selesai"...');
        for (let i = 0; i < 30; i++) {
            const done = Array.from(document.querySelectorAll('div[aria-label="Selesai"]'))
                .find(el => el.offsetHeight > 0);
            if (done) {
                done.scrollIntoView({ behavior: 'smooth', block: 'center' });
                log('✅ Klik tombol "Selesai"');
                done.click();
                return true;
            }
            await delay(500);
        }
        log('⚠️ Tombol "Selesai" tidak ditemukan.');
        return false;
    }

    async function main() {
        if (!window.location.pathname.includes('/marketplace/selling')) {
            log('🔄 Navigasi ulang ke halaman utama...');
            window.location.href = 'https://www.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog=true';
            return;
        }

        while (true) {
            const hasUpdate = await clickUpdateButtons();
            if (!hasUpdate) break;
            await clickDoneButtonIfExists();

            log('🔄 Kembali ke halaman utama untuk batch selanjutnya...');
            await countdown(5);
            window.location.href = 'https://www.facebook.com/marketplace/selling/renew_listings/?is_routable_dialog=true';
            break; // reload loop saat load ulang halaman
        }

        log('🏁 Proses selesai.');
    }

    setTimeout(main, 2000);
})();