You've already forked uutils.github.io
mirror of
https://github.com/uutils/uutils.github.io.git
synced 2026-06-10 16:12:28 -07:00
965c898e51
- Clone WASM response before compileStreaming so fallback doesn't re-fetch - Guard against concurrent ensureRuntime() calls with a promise cache - Replace inline onclick with event delegation for CSP compatibility - Add SRI note for WASI shim dynamic import - Skip l10n tests that require French translations in WASM binary - Replace uninteresting fmt example with printf sheep | nl - Hide Playground link from top navigation for now
57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
/**
|
|
* Inline "Run" button handler for uutils code examples.
|
|
* Lightweight alternative to the full terminal playground.
|
|
*/
|
|
|
|
(function () {
|
|
let runtimePromise = null;
|
|
|
|
function ensureRuntime() {
|
|
if (runtimePromise) return runtimePromise;
|
|
runtimePromise = new Promise((resolve, reject) => {
|
|
if (document.querySelector('script[src="/js/wasm-terminal.js"]')) {
|
|
resolve();
|
|
return;
|
|
}
|
|
const script = document.createElement("script");
|
|
script.src = "/js/wasm-terminal.js";
|
|
script.onload = resolve;
|
|
script.onerror = reject;
|
|
document.head.appendChild(script);
|
|
});
|
|
return runtimePromise;
|
|
}
|
|
|
|
async function runExample(button) {
|
|
const container = button.closest(".wasm-example");
|
|
if (!container) return;
|
|
|
|
const command = container.dataset.command;
|
|
const outputEl = container.querySelector(".wasm-example-output");
|
|
const preEl = outputEl.querySelector("pre");
|
|
|
|
// Show loading state
|
|
button.disabled = true;
|
|
button.textContent = "Running...";
|
|
outputEl.style.display = "block";
|
|
preEl.textContent = "Loading...";
|
|
|
|
try {
|
|
await ensureRuntime();
|
|
const output = await window.uutilsExecute(command);
|
|
preEl.textContent = output || "(no output)";
|
|
} catch (e) {
|
|
preEl.textContent = "Error: " + e.message;
|
|
}
|
|
|
|
button.disabled = false;
|
|
button.textContent = "Run";
|
|
}
|
|
|
|
// Use event delegation instead of inline onclick for CSP compatibility
|
|
document.addEventListener("click", function (e) {
|
|
const btn = e.target.closest(".wasm-run-btn");
|
|
if (btn) runExample(btn);
|
|
});
|
|
})();
|