Pumpkin Finder

It displays a list of uncollected pumpkins

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         Pumpkin Finder
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  It displays a list of uncollected pumpkins
// @author       Diramix
// @match        https://wplace.live/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wplace.live
// @grant        none
// ==/UserScript==

(() => {
  const targetSelector = 'body > div > dialog:nth-child(20) > div > div:nth-child(5)';
  const iframeUrl = 'https://wplace.samuelscheit.com/#pumpkins=1';
  let iframe = null;
  let cleanupInterval = null;

  // --- Extract pumpkin numbers from data-tip ---
  function getClaimedNumbers() {
    const el = document.querySelector(`${targetSelector} > div[data-tip]`);
    if (!el) return [];
    const text = el.getAttribute('data-tip') || '';
    const matches = text.match(/#(\\d+)/g) || [];
    return matches.map(n => parseInt(n.replace('#', ''), 10));
  }

  // --- Remove pumpkins inside iframe ---
  function removePumpkins() {
    if (!iframe?.contentDocument) return;

    const doc = iframe.contentDocument;
    const container = doc.querySelector('#pumpkins-modal');
    if (!container) return; // modal not yet loaded

    const claimed = getClaimedNumbers();
    if (!claimed.length) return;

    let removed = 0;
    claimed.forEach(num => {
      container.querySelectorAll('div').forEach(div => {
        const txt = div.innerText || '';
        if (txt.match(new RegExp(`\\b${num}\\b`))) {
          div.remove();
          removed++;
        }
      });
    });

    if (removed > 0) console.log(`[pumpkins] Removed ${removed} pumpkins`);
  }

  // --- Add iframe into the modal ---
  function injectIframe() {
    const container = document.querySelector(targetSelector);
    if (!container) return;
    if (container.querySelector('iframe[src*="wplace.samuelscheit.com"]')) return;

    iframe = document.createElement('iframe');
    iframe.src = iframeUrl;
    iframe.style.width = '100%';
    iframe.style.height = '600px';
    iframe.style.border = '1px solid #444';
    container.appendChild(iframe);

    console.log('[pumpkins] iframe added');

    iframe.addEventListener('load', () => {
      console.log('[pumpkins] iframe loaded');
      const tryRemove = () => {
        if (!document.body.contains(iframe)) {
          clearInterval(cleanupInterval);
          cleanupInterval = null;
          return;
        }
        removePumpkins();
      };

      // check every second until pumpkins-modal appears
      const waitForModal = setInterval(() => {
        if (iframe?.contentDocument?.querySelector('#pumpkins-modal')) {
          clearInterval(waitForModal);
          tryRemove(); // first cleanup
          cleanupInterval = setInterval(tryRemove, 5000); // every 5s
        }
      }, 1000);
    });
  }

  // --- Watch for the dialog to appear ---
  const observer = new MutationObserver(() => {
    const container = document.querySelector(targetSelector);
    if (container) injectIframe();
  });

  observer.observe(document.body, { childList: true, subtree: true });
  console.log('[pumpkins] observer started');
})();