Application-owned storage transformations
The module’s
initialize(saver, { signal }) export configures
the optional beforeSave and
beforeRestore callbacks. For a script that
doesn’t configure storage hooks, see the
writing-help example.
Use invented details only. This example uses a public, predictable demo key, so anyone can decrypt its drafts. It demonstrates the hooks, not secure key management or GDPR compliance.
Type a name or message, wait briefly for autosave, then
reload to restore it. Inspect the
demo:encrypted localStorage entry to see the
version, algorithm, fresh IV, and ciphertext instead of
field values. The module
uses Web Crypto AES-GCM entirely in your browser and never
sends form values or keys over the network.
A real application must replace the demo key with one authorized by its backend session, never store the key alongside the draft, and revoke access on logout or session expiry. Encryption cannot protect against malicious same-origin scripts, or another person whose browser session still authorizes the key.
The retention checkbox controls which fields survive
submission, not whether drafts are saved while typing.
Asynchronous saves may not finish if you immediately close
or leave the page. Without JavaScript, or if the module
fails, the native form remains usable but drafts are not
saved. Keep a copy before leaving. The
/contact endpoint is a placeholder, not a
submission service. Web Crypto requires HTTPS or localhost.
<form-saver
script="./draft-storage.js"
storage-key="demo:encrypted"
retain="name email"
retain-choice
>
<form action="/contact" method="post">
<!-- Native labelled fields -->
</form>
</form-saver>