Bookmarklet Integration Modes – Executable, Display, Hybrid

High‑level overview of the three modes

When you embed a large diagnostic bookmarklet into an HTML document, you must decide whether the page should execute the code, only show the code, or preserve the code while keeping the page static. That is exactly what the three modes capture.

1. Executable Mode

The page becomes a live, self‑profiling runtime shell.

  • Behavior: The bookmarklet runs automatically via <script>.
  • Use when: You want introspection, observers, and JSON streaming to be active by default.
  • Risk surface: Any change to the bookmarklet changes page behavior.

2. Display Mode

The code is visible and study‑ready, but never runs.

  • Behavior: Code appears as text inside <pre><code> only.
  • Use when: Documentation, teaching, or code review is the primary goal.
  • Risk surface: Minimal, the page is effectively static.

3. Hybrid Mode

One copy to learn from, one copy to copy.

  • Behavior: Code is shown and also stored raw in a non‑executing script tag.
  • Use when: You want accurate copy‑paste and high‑quality docs on one page.
  • Risk surface: Static by default, still safe for most doc sites.
Design principle: Treat the bookmarklet as an independent runtime component. Your HTML document is the host shell that decides whether this component is live, display‑only, or preserved for tooling.

Executable Mode: building a live diagnostic shell

Executable Mode is for when you want the page itself to behave like a self‑profiling environment. As soon as the document loads, your bookmarklet runs as if a user had invoked it manually.

Decide what “live” means for your page
Identify which behaviors the bookmarklet adds:
  • Introspection: Scanning DOM, measuring layout, collecting metrics.
  • Observers: PerformanceObserver, MutationObserver, and possibly event listeners for user flows.
  • Diagnostics UI: Any overlay, sidecar pane, or JSON stream window your code opens.
In Executable Mode, these behaviors are not optional: they are part of page identity.
Embed the bookmarklet as executable JavaScript
Place your bookmarklet inside a normal script tag near the end of <body>:
<!-- Executable Mode: runs on page load -->
<script>
/**
 * Diagnostic bookmarklet – Executable Mode
 * NOTE: Replace the next line with your full bookmarklet body.
 */
(function(){ 
  // <!-- BEGIN BOOKMARKLET PAYLOAD -->
  // ... PASTE YOUR LONG BOOKMARKLET CODE HERE ...
  // <!-- END BOOKMARKLET PAYLOAD -->
})(); 
</script>
This pattern wraps the bookmarklet in an IIFE so that globals are minimized and the payload executes once on load.
Respect the page’s performance and UX envelope
Since PerformanceObserver and MutationObserver can be chatty, structure the code so that:
  • Observers are debounced: Aggregate changes before logging or streaming.
  • JSON stream windows are lazily opened: For example, on a keyboard shortcut:
// Example: only open the diagnostic window on Ctrl+Shift+D
window.addEventListener("keydown", (ev) => {
  if (ev.ctrlKey && ev.shiftKey && ev.key.toLowerCase() === "d") {
    // open or focus your JSON stream window
  }
});
That way, Executable Mode is powerful but not intrusive: the runtime shell is there, but users can “opt in” to the visible overlay.

Display Mode: building a safe, static documentation page

Display Mode is for training, peer review, and publishing. The code is treated as a primary text artifact, not as an active runtime component.

Escape the bookmarklet for presentation
Inside <pre><code>, you must escape HTML‑sensitive characters:
  • < becomes &lt;
  • > becomes &gt;
  • & becomes &amp;
Example skeleton:
<!-- Display Mode: code as text only -->
<pre><code class="language-javascript">
javascript:(function(){
  // Your bookmarklet, escaped for HTML:
  // &lt;!-- BEGIN BOOKMARKLET PAYLOAD --&gt;
  // ...
  // &lt;!-- END BOOKMARKLET PAYLOAD --&gt;
})();
</code></pre>
Add explanatory commentary around the code
Treat the code as an academic object: annotate its structure, assumptions, and extension points.
  • Structure: Top‑level IIFE, configuration, observers, UI overlay.
  • Assumptions: Browser APIs, CSP expectations, third‑party dependencies.
  • Extension points: Places to plug in other observers or data sinks.
That commentary turns your bookmarklet into something that is not only usable, but also teachable.

Hybrid Mode: combining readability with raw, tooling‑ready code

Hybrid Mode is often the most “production‑ready documentation” pattern: one copy of the code is optimized for humans, another is optimized for programmatic consumption, and neither executes.

Create the human‑readable presentation block
As in Display Mode, you render an escaped, syntax‑highlightable version:
<section aria-label="Bookmarklet (readable version)">
  <h3>Readable bookmarklet</h3>
  <pre><code class="language-javascript">
  javascript:(function(){
    // Escaped, formatted version for human readers
  })();
  </code></pre>
</section>
Preserve the exact, raw bookmarklet in a non‑executing script
To make the exact code easily extractable by scripts or build tooling, you add:
<!-- Hybrid Mode: non-executing raw payload -->
<script type="text/plain" id="bookmarklet-raw">
javascript:(function(){
  // <!-- BEGIN BOOKMARKLET PAYLOAD (RAW, UNESCAPED) -->
  // ... PASTE YOUR EXACT BOOKMARKLET HERE ...
  // <!-- END BOOKMARKLET PAYLOAD -->
})();
</script>
The browser ignores type="text/plain" as executable JavaScript, but your own tooling (or a small on‑page helper) can read and export this string verbatim.
Optionally expose a “copy raw bookmarklet” helper
You can add a small script that reads text from the non‑executing block and offers a one‑click copy:
<script>
function copyBookmarkletRaw(){
  const el = document.getElementById("bookmarklet-raw");
  if(!el) return;
  const text = el.textContent || el.innerText || "";
  navigator.clipboard.writeText(text.trim()).catch(console.error);
}
</script>
Bound to a button, this makes the page feel like a polished distribution hub instead of just a static gist.
Summary: Use Executable Mode for live shells, Display Mode for pure docs, and Hybrid Mode when you want both a publication‑quality explanation and an automation‑ready source of truth in a single HTML artifact.

Comments

Popular posts from this blog