Shadowlister(Torn Auto-Anon Toggle)

Toggle all item listings anonymous ON/OFF while keeping manual control

// ==UserScript==
// @name         Shadowlister(Torn Auto-Anon Toggle)
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Toggle all item listings anonymous ON/OFF while keeping manual control
// @match        https://www.torn.com/page.php?sid=ItemMarket*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  let autoAnon = true;

  // Create toggle button
  const toggleBtn = document.createElement('button');
  toggleBtn.textContent = 'Auto-Anon: ON';
  Object.assign(toggleBtn.style, {
    position: 'fixed',
    top: '50%',
    left: '10px',
    transform: 'translateY(-50%)',
    zIndex: '9999',
    padding: '8px 12px',
    backgroundColor: '#222',
    color: '#0f0',
    border: '2px solid #0f0',
    borderRadius: '6px',
    fontSize: '14px',
    cursor: 'pointer',
    opacity: 0.9,
  });

  toggleBtn.onclick = () => {
    autoAnon = !autoAnon;
    toggleBtn.textContent = `Auto-Anon: ${autoAnon ? 'ON' : 'OFF'}`;
    toggleBtn.style.color = autoAnon ? '#0f0' : '#f00';
    toggleBtn.style.borderColor = autoAnon ? '#0f0' : '#f00';
    applyToAllVisible();
  };

  document.body.appendChild(toggleBtn);

  // Tick/Untick all visible checkboxes
  function applyToAllVisible() {
    const allBoxes = document.querySelectorAll('input[id^="itemRow-incognitoCheckbox-"]');
    allBoxes.forEach(box => {
      if (autoAnon && !box.checked) {
        box.click();
      } else if (!autoAnon && box.checked) {
        box.click();
      }
    });
  }

  // Observer for new rows
  const observer = new MutationObserver(mutations => {
    for (let mutation of mutations) {
      for (let node of mutation.addedNodes) {
        if (!(node instanceof HTMLElement)) continue;
        const checkboxes = node.querySelectorAll?.('input[id^="itemRow-incognitoCheckbox-"]') || [];
        checkboxes.forEach(checkbox => {
          if (autoAnon && !checkbox.checked) {
            checkbox.click();
          } else if (!autoAnon && checkbox.checked) {
            checkbox.click();
          }
        });
      }
    }
  });

  function initObserver() {
    if (location.hash.startsWith('#/addListing')) {
      observer.observe(document.body, { childList: true, subtree: true });
      applyToAllVisible(); // First batch
    }
  }

  window.addEventListener('hashchange', initObserver);
  window.addEventListener('load', initObserver);
  initObserver();
})();