mirror of
https://github.com/jstrieb/urlpages.git
synced 2025-01-12 16:01:55 +01:00
Save and restore state from URL hash
This commit is contained in:
parent
78340d1622
commit
84af590ed7
1 changed files with 39 additions and 8 deletions
|
@ -33,36 +33,67 @@ textarea, iframe {
|
||||||
border: 0.5px solid;
|
border: 0.5px solid;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
/* Run once when the page is loaded */
|
||||||
|
function initialize() {
|
||||||
|
// Get page data from the URL and load it into the boxes
|
||||||
|
if (window.location.hash) {
|
||||||
|
var b64 = window.location.hash.slice(1);
|
||||||
|
var json = window.atob(b64);
|
||||||
|
var data = JSON.parse(json);
|
||||||
|
|
||||||
|
document.getElementById("css").value = data["css"];
|
||||||
|
document.getElementById("javascript").value = data["js"];
|
||||||
|
document.getElementById("html").value = data["html"];
|
||||||
|
}
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Run each time a key is pressed on a text box */
|
||||||
function update() {
|
function update() {
|
||||||
|
var data = {
|
||||||
|
"css" : document.getElementById("css").value,
|
||||||
|
"js" : document.getElementById("javascript").value,
|
||||||
|
"html" : document.getElementById("html").value
|
||||||
|
};
|
||||||
|
|
||||||
|
// Save page data to the URL
|
||||||
|
window.location.hash = "#" + window.btoa(JSON.stringify(data));
|
||||||
|
|
||||||
|
// Generate an HTML page from the contents of each <textarea>
|
||||||
var pageData =
|
var pageData =
|
||||||
`
|
`
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<head>
|
<head>
|
||||||
<style>
|
<style>
|
||||||
${document.getElementById("css").value}
|
${data["css"]}
|
||||||
</style>
|
</style>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
${document.getElementById("javascript").value}
|
${data["js"]}
|
||||||
</` +
|
</scr` +
|
||||||
// TODO: Figure out how to avoid breaking this up
|
// This has to be broken up because otherwise it is recognized as the main document's end script tag
|
||||||
`script>
|
`ipt>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
${document.getElementById("html").value}
|
${data["html"]}
|
||||||
</body>
|
</body>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
// Update the <iframe> to display the generated page
|
||||||
window.frames[0].location.replace(`data:text/html,${encodeURIComponent(pageData)}`);
|
window.frames[0].location.replace(`data:text/html,${encodeURIComponent(pageData)}`);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body onload="initialize()">
|
||||||
<div id="textboxes" onkeyup="update()">
|
<div id="textboxes" onkeyup="update()">
|
||||||
<!-- Unfortunately having these <textarea>s on one line is actually necessary to remove a tiny amount of horizontal space between them -->
|
<!-- Unfortunately having these <textarea>s on one line is actually necessary to remove a tiny amount of horizontal space between them -->
|
||||||
<textarea id="html" placeholder="HTML" rows="9"></textarea><textarea id="css" placeholder="CSS" rows="9"></textarea><textarea id="javascript" placeholder="JavaScript" rows="9"></textarea>
|
<textarea id="html" placeholder="HTML" rows="9"></textarea><textarea id="css" placeholder="CSS" rows="9"></textarea><textarea id="javascript" placeholder="JavaScript" rows="9"></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<iframe id="display"></iframe>
|
<iframe></iframe>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue