Saturday, November 29, 2025

From GreaseMonkey Hacks to AI-Powered Userscripts

The Old Model: Static Hacks

Books like GreaseMonkey Hacks (O’Reilly, 2005) offered ingenious recipes for DOM manipulation and automation [O’Reilly overview], [Google Books], [Goodreads]. They were snapshots in time — useful, but quickly outdated as web layouts evolved.

The Current Ecosystem

Today, sites like GreasyFork, Github, and OpenUserJS host complete userscripts. Popular managers include Tampermonkey [Documentation] and Violentmonkey [Overview]. These repositories provide one‑off solutions, but few shared function libraries exist that can be imported into multiple scripts.

The AI Advantage

AI can generate generic utilities on demand:

  • **DOM helpers**: getColumnValues(), sortRows()
  • **UI injectors**: addButton(), createFloatingPanel()
  • **Network wrappers**: fetchJSON(), postFormData()

Instead of searching for a static hack, users ask AI: “Give me a function to add a sort icon to any grid.” AI adapts code to tables, div grids, or custom layouts instantly.

Reusable Function Libraries

Examples of generic utilities AI can generate:

  • Sorting utilities: addSortIcon(element, comparator)
  • Storage utilities: GM_setValue/GM_getValue [StackOverflow]
  • Mutation observers: onElementAdded(selector, callback) [MDN]
  • UI helpers: injectCSS(styles), createToolbar(buttons)

Essential Modern Topics

AI Coding Improvements

Recent advances in AI coding assistants can be quantified:

  • Error reduction: Syntax errors dropped from ~20–30% to single digits; >90% correctness on first pass.
  • Larger context windows: From ~2–4k tokens to 100k+, enabling multi‑file reasoning.
  • Complex task handling: Full‑stack scaffolding (React frontend, Express backend, SQL schema) in one request.
  • Refactoring & debugging: AI spots inefficiencies and walks through stack traces.
  • Integration with IDEs: Inline suggestions, unit test generation, project‑wide consistency.

AI Generated Examples: Table vs. Div Sorting

Classic table header sort icon:


document.querySelectorAll("table th").forEach(th => {
  const icon = document.createElement("span");
  icon.textContent = "⇅";
  icon.style.cursor = "pointer";
  icon.style.marginLeft = "5px";

  icon.addEventListener("click", () => {
    const table = th.closest("table");
    const rows = Array.from(table.querySelectorAll("tbody tr"));
    const colIndex = Array.from(th.parentNode.children).indexOf(th);

    rows.sort((a, b) => {
      const aText = a.children[colIndex].textContent.trim();
      const bText = b.children[colIndex].textContent.trim();
      return aText.localeCompare(bText);
    });

    rows.forEach(r => table.querySelector("tbody").appendChild(r));
  });

  th.appendChild(icon);
});
  

Modern div‑based grid sort icon:


document.querySelectorAll(".grid-header .column").forEach(col => {
  const icon = document.createElement("span");
  icon.textContent = "⇅";
  icon.style.cursor = "pointer";
  icon.style.marginLeft = "5px";

  icon.addEventListener("click", () => {
    const grid = col.closest(".grid");
    const rows = Array.from(grid.querySelectorAll(".grid-row"));
    const colIndex = Array.from(col.parentNode.children).indexOf(col);

    rows.sort((a, b) => {
      const aText = a.querySelectorAll(".cell")[colIndex].textContent.trim();
      const bText = b.querySelectorAll(".cell")[colIndex].textContent.trim();
      return aText.localeCompare(bText);
    });

    rows.forEach(r => grid.appendChild(r));
  });

  col.appendChild(icon);
});
  

Notes

Conclusion

Books gave us patterns. Script repositories give us solutions. AI gives us living libraries — adaptable, modular, and teachable. The future isn’t “GreaseMonkey Hacks 2.0,” it’s AI as your co‑author of reusable script utilities.