Skip to content

Instantly share code, notes, and snippets.

@AgtLucas
Forked from addyosmani/LICENSE.txt
Created May 23, 2014 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgtLucas/260f67099e491fcf43f0 to your computer and use it in GitHub Desktop.
Save AgtLucas/260f67099e491fcf43f0 to your computer and use it in GitHub Desktop.
(function (d) { // Alias for current document
l = localStorage, // Alias for localStorage, used for storing text content
k = 'c', // namespace for storage
q = d.body; // Alias for document body
q.contentEditable = true; // Switch on contentEditable
q.innerHTML = l[k] || ''; // Set content to localStorage value or empty string
q.onkeyup = function () { // On keyUp, update localStorage value to textContent of your contentEditable area
l[k] = q.innerHTML;
}
})(document);
(function(d){l=localStorage,k='c',q=d.body;q.contentEditable=true;q.innerHTML=l[k]||'';q.onkeyup=function(){l[k]=q.innerHTML;}})(document);

I use this as a JavaScript snippet or bookmarklet in Chrome to turn the current blank tab into an offline text-editor which stores what I write in localStorage. The < 140 character version can be run via console.

The simplest version that you can add as a bookmarklet:

(function(d){l=localStorage,k='c',q=d.body;q.contentEditable=true;q.innerHTML=l[k]||'';q.onkeyup=function(){l[k]=q.innerHTML;}})(document);

A demo version of this is available on JSBin.

And this time actually including var:

javascript:(function(d){var l=localStorage,k='c',q=d.body;q.contentEditable=true;q.innerHTML=l[k]||'';q.onkeyup=function(){l[k]=q.innerHTML;}})(document);

Or using oninput, which also works fine and should catch paste etc:

javascript: (function(d){l=localStorage,k='c',q=d.body;q.contentEditable=true;q.innerHTML=l[k]||'';q.oninput=function(){l[k]=q.innerHTML;}})(document);

Pretty version

Prettier version of the implementation using document.write (hah) to write over any existing tab content, gives you some styling too:

javascript:(function(d){d.write('<body contenteditable style="font: 2rem/1.5 monospace;max-width:60rem;margin:0 auto;padding:4rem;">');var k = 'c'; var q = d.querySelector('body');q.innerHTML=localStorage[k];q.onkeyup=function(){localStorage[k]=q.innerHTML;}})(document);

Preview:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Copyright (C) 2014 ADDY OSMANI <addyosmani.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "offlineTextEditor",
"description": "ContentEditable-powered Offline text editor",
"keywords": [
"text",
"edit",
"editor",
"localStorage",
"offline"
]
}
<!DOCTYPE html>
<title>Offline Text Editor</title>
<style>
body { font: 2rem/1.5 monospace;max-width:60rem;margin:0 auto;padding:4rem;}
</style>
<body>
<script>
(function(d){l=localStorage,k='c',q=d.body;q.contentEditable=true;q.innerHTML=l[k]||'';q.onkeyup=function(){l[k]=q.innerHTML;}})(document);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment