Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active July 14, 2017 06:38
Show Gist options
  • Save rjcorwin/966525c209402320931dfd8b04dfd997 to your computer and use it in GitHub Desktop.
Save rjcorwin/966525c209402320931dfd8b04dfd997 to your computer and use it in GitHub Desktop.
A document that live edits itself for all those beaker browsers watching it with live reloading turned on.
<html id="content">
<head>
<script>
var selfArchive = new DatArchive('' + window.location)
var state = (document.getElementById('content')).outerHTML
// Counter for the number of state changes thus commits.
var i = 0
async function saveIt() {
// Check if state is stale.
if (state !== (document.getElementById('content')).outerHTML) {
state = (document.getElementById('content')).outerHTML
await selfArchive.writeFile('/index.html', state, 'utf8')
console.log('write')
await selfArchive.commit()
console.log('commit')
i++
console.log(i)
}
// Debounce as to not overload network on very fast key strokes.
setTimeout(saveIt, 500)
}
saveIt()
</script>
</head>
<body>
<div contenteditable="true">click here to edit</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment