Developers / window.s4m
Apps that remember
An S4mple app is a complete HTML document rendered in a hard sandbox, like an AI chat artifact, except it lives in your workspace and keeps its data. Write ordinary web code; the runtime makes storage work and keeps the sandbox honest. This page is the whole contract.
The sandbox
Every app runs in an opaque-origin iframe with scripts enabled and nothing else. It cannot see the note, other apps, or the S4mple UI. Network access allows passive resources only — scripts, styles, fonts, images — and only from sites the person viewing has allowed in Settings → Interactions. An app never declares a site of its own. A fresh device allows this preset, and any entry can be removed:
https://cdnjs.cloudflare.comhttps://unpkg.comhttps://cdn.jsdelivr.nethttps://fonts.googleapis.comhttps://fonts.gstatic.com data:An app the person has allowed to read their data gets no network access at all: its frame is composed with no site on any directive, so nothing it reads can leave the device. That is not a setting an app can change; it is how the frame is built. An app that reads notes must inline everything.
fetch,
XHR, and WebSockets are blocked to every host,
including the allowed ones. An app can load a charting library;
it can never call an API, phone home, or exfiltrate your data. That
is the point. Inline scripts and styles are allowed, and images
beyond the allowed sites work as data: URIs.
Storage that just works
Native localStorage throws in an opaque-origin sandbox. S4mple replaces it with a full shim
backed by per-app storage, so code written for the open web runs
unchanged:
// The localStorage shim persists both styles:
localStorage.setItem("count", "5")
localStorage.count = "5" // property style works too
// Values are strings, exactly like real localStorage.
// Overflow throws QuotaExceededError, exactly like real storage.
// sessionStorage exists but is in-memory only.There is also a direct state API for structured data:
// One JSON-serializable state slot per app
window.s4m.state.get() // current value, or null
window.s4m.state.set(value) // persists (throws TypeError on non-JSON values)
// Fires once with the restored value (or null) when state hydrates
window.s4m.onHydrate((restored) => { /* ... */ })No ready-callbacks needed
App scripts are deferred until state hydrates. A synchronous localStorage.getItem at the top of your script already sees restored data, so there is no
"wait for ready" dance and no flash of empty state. Two caveats
follow from the deferral: document.write inside scripts is unsupported, and load-time inline handlers like <img onerror> fire before your script runs, so they cannot call script-defined
functions.
Limits
- 16 KB per write, 256 KB total per app.
Exceeding a cap throws
QuotaExceededError, the same error real storage throws when full. - 30 accepted writes per 10s. Writes are debounced, so normal UI code never notices; a runaway loop does.
These limits are enforced by the runtime and rendered here from the same constants, so this page cannot disagree with the sandbox.
Persistence is on, and yours to turn off
App state survives reloads. On the Mac and iPad, Settings → Interaction memory turns that off; then everything still works but resets when the note closes, so well-built apps treat an empty start as a valid state. Saved state lives on your device, keyed to the app, and never syncs: what you type into an app on one device stays on that device. That is a privacy decision, not a missing feature.
A complete app
This is the whole thing: a counter that survives reloads. Ask Claude for it over MCP with "make me a counter app", or paste it into an HTML note yourself.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Counter</title>
<style>
body { font-family: system-ui; display: grid; place-items: center; height: 100vh; margin: 0; }
button { font-size: 2rem; padding: 1rem 2rem; border-radius: 12px; cursor: pointer; }
</style>
</head>
<body>
<button id="b"></button>
<script>
// Ordinary synchronous code: scripts run after state hydrates,
// so this read already sees the restored value.
const b = document.getElementById("b");
b.textContent = localStorage.getItem("count") ?? "0";
b.onclick = () => {
localStorage.count = String(Number(b.textContent) + 1);
b.textContent = localStorage.count;
};
</script>
</body>
</html>The same state channel powers inline interaction fences in markdown notes, with one difference: fragments have no
network at all, not even the allowed sites. Everything inlines.