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 onHTMLElement
) 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 ๐จโ๐ง๐