Pumpkin Finder

It displays a list of uncollected pumpkins

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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');
})();