Utility Functions and Helpers - dormouseroared/dom-inspector-plus GitHub Wiki

Absolutely, letโ€™s keep this engineering train rolling. Here comes Chapter 7 of the DOM Inspector Plus Guide โ€” a focused look at the utility functions and helpers that glue the system together efficiently.


๐Ÿงฉ Chapter 7: Utility Functions & Helpers

While the core of Inspector Plus lives in a single IIFE, there are a few purpose-built helper functions that make the logic cleaner and more expressive. This chapter documents the key utilities, what they do, and how they contribute to the overlayโ€™s intelligence.


๐Ÿ”— getLink(prop): MDN Documentation Links

function getLink(prop) {
  return `https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/${prop}`;
}
  • Generates a clickable URL for each inspected property.
  • Used to wrap property names in anchor tags:
    <a href="..." target="_blank">property</a>
    
  • Helps users learn about properties directly from the inspector.

๐Ÿ“š Consider evolving this to handle special cases (like classList, dataset, or attributes not on HTMLElement) in the future.


๐Ÿ“‹ copyToClipboard(str)

function copyToClipboard(str) {
  navigator.clipboard.writeText(str).then(
    () => alert("Copied!"),
    () => alert("Failed to copy.")
  );
}
  • Handles cross-browser, asynchronous clipboard access.
  • Called when users hit the "Copy" button inside the overlay.
  • Exports properties in a plain-text, human-readable format.

Example copied output:

input
  value: hello โ† local
  checked: true โ† attribute

โœ… tryParse(json): Safe JSON Parsing

This unnamed helper ensures that malformed localStorage entries donโ€™t crash the inspector:

function tryParse(text) {
  try {
    return JSON.parse(text);
  } catch {
    return null;
  }
}
  • Used when reading per-tag config from localStorage.
  • Returns null safely if the string is not valid JSON.

๐ŸŽ›๏ธ renderOverlay(html)

Encapsulates the DOM injection and UI rendering logic:

function renderOverlay(html) {
  const existing = document.getElementById("dom-inspector-overlay");
  if (existing) existing.remove();

  const box = document.createElement("div");
  box.id = "dom-inspector-overlay";
  box.style.position = "fixed";
  box.style.bottom = "20px";
  // ... more styling ...
  box.innerHTML = html;

  document.body.appendChild(box);
}
  • First removes any existing overlay so only one instance exists.
  • Applies all styling via element.style inline.
  • Appends the overlay to document.body.

๐Ÿง  Why These Helpers Matter

These utilities:

  • Keep main functions (inspect, readProps, etc.) focused
  • Help with extensibility down the road (e.g. richer clipboard exports, deeper linking)
  • Maintain graceful error handling and a clean separation of concerns

Up next is Chapter 8: UI Behavior & Interactivity โ€” where weโ€™ll walk through how each button works (Edit, Reset, Copy), consider possible new features (toggles, modes), and talk about accessibility.

Shall I keep it going? Youโ€™re turning this from a clever script into a fully documented dev tool โ€” something future you (and contributors) will thank you for ๐Ÿ‘จโ€๐Ÿ”ง๐Ÿ“˜