Commit d52d6b57 authored by Michael Murtaugh's avatar Michael Murtaugh

saveable code from old makeserver hook

parents
window.addEventListener("DOMContentLoaded", function () {
// http://blog.garstasio.com/you-dont-need-jquery/ajax/
function $post(url, data, success) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
console.log(xhr.status);
if (xhr.status === 200) {
// var response = JSON.parse(xhr.responseText);
var response = xhr.responseText;
success.call(xhr, response)
}
};
//xhr.send(JSON.stringify(data));
xhr.send(data);
}
function save () {
// http://stackoverflow.com/questions/6088972/get-doctype-of-an-html-as-string-with-javascript
var node = document.doctype,
html = "";
if (node) {
html = "<!DOCTYPE "
+ node.name
+ (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
+ (!node.publicId && node.systemId ? ' SYSTEM' : '')
+ (node.systemId ? ' "' + node.systemId + '"' : '')
+ ">\n";
}
html += document.documentElement.outerHTML;
$post(this.window.location, "text="+encodeURIComponent(html), function (data) {
console.log("saved", data);
});
}
// console.log("ready");
document.addEventListener("keypress", function (e) {
// console.log("keypress", e);
if (e.ctrlKey && e.key == "s") {
e.preventDefault();
// e.stopPropagation();
// console.log("save");
save();
}
// var request = new XMLHttpRequest();
// request.open('POST', '/my/url', true);
// request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
// request.send(data);
})
})
<!DOCTYPE html>
<html><head>
<title></title>
<meta charset="utf-8">
<script src="src/saveable.js"></script>
</head>
<body contenteditable="">
<h1>edit me please<br></h1>
<p>
THis is a paragraph <br></p><p>And this is another!<br></p>
</body></html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment