pub const GLOBAL_SCRIPT_CONTENTS: &str = "(function() {\n // Only initialize once, even if this script appears multiple times in the DOM\n if (window.__columbo) return;\n window.__columbo = true;\n\n // Track templates waiting to be swapped in\n const pendingIds = new Set();\n // Prevent scheduling multiple microtasks for the same batch\n let transitionScheduled = false;\n\n // Swap placeholder with template content and clean up\n const performSwap = (id) => {\n const p = document.querySelector(`[data-columbo-p-id=\"${id}\"]`);\n const r = document.querySelector(`[data-columbo-r-id=\"${id}\"]`);\n \n if (p && r) {\n p.replaceWith(...r.content.childNodes);\n r.remove();\n }\n };\n\n // Process all pending swaps in a single View Transition (if available)\n const processQueue = () => {\n transitionScheduled = false;\n \n // Snapshot and clear the queue\n const ids = Array.from(pendingIds);\n pendingIds.clear();\n\n // Batch swaps in a View Transition for smooth updates\n if (document.startViewTransition) {\n document.startViewTransition(() => ids.forEach(performSwap));\n } else {\n ids.forEach(performSwap);\n }\n };\n\n // Watch for new template elements being added to the DOM\n const observer = new MutationObserver((mutations) => {\n for (const mutation of mutations) {\n for (const node of mutation.addedNodes) {\n // Check if this is a columbo replacement template\n if (node.nodeType === 1 && node.hasAttribute(\'data-columbo-r-id\')) {\n pendingIds.add(node.getAttribute(\'data-columbo-r-id\'));\n }\n }\n }\n\n // Schedule processing if we have new templates and haven\'t already scheduled\n if (pendingIds.size > 0 && !transitionScheduled) {\n transitionScheduled = true;\n queueMicrotask(processQueue);\n }\n });\n\n // Observe the entire document to catch streamed chunks as they arrive\n observer.observe(document.documentElement, { childList: true, subtree: true });\n})();\n";Expand description
The contents of the script that performs the on-the-fly replacements.