Use versioned API with (hopefully) shorter URLs

This commit is contained in:
Jacob Strieb 2020-07-19 15:11:40 -04:00
parent 7848df1b1b
commit 17937b2db6
3 changed files with 12 additions and 5 deletions

10
api.js
View File

@ -12,8 +12,7 @@
* Global Variables * Global Variables
******************************************************************************/ ******************************************************************************/
const LATEST_API_VERSION = "0.0.1"; const LATEST_API_VERSION = "0.2.0";
// const LATEST_API_VERSION = "0.2.0";
var apiVersions = {}; var apiVersions = {};
@ -31,12 +30,19 @@ apiVersions["0.2.0"] = {
getViewLink: function(pageData) { getViewLink: function(pageData) {
var urlData = { var urlData = {
version: this.VERSION, version: this.VERSION,
compressed: false,
body: pageData,
}; };
const hashObject = b64.encode(JSON.stringify(urlData)); const hashObject = b64.encode(JSON.stringify(urlData));
return `http://jstrieb.github.io/urlpages/#${hashObject}`; return `http://jstrieb.github.io/urlpages/#${hashObject}`;
}, },
/* Return the page data from the object */
decode: function(urlData) {
return urlData.body;
},
} }

View File

@ -50,7 +50,7 @@ function setViewUrl() {
"html" : document.getElementById("html").value "html" : document.getElementById("html").value
}; };
var html = encodeURIComponent(getHTML(data)); var html = getHTML(data);
// Update the URL for the "Short Link" button // Update the URL for the "Short Link" button
document.getElementById("url").value = api.getViewLink(html); document.getElementById("url").value = api.getViewLink(html);
@ -118,7 +118,7 @@ function update() {
"html" : document.getElementById("html").value "html" : document.getElementById("html").value
}; };
var html = encodeURIComponent(getHTML(data)); var html = getHTML(data);
// Save encoded page data to the URL // Save encoded page data to the URL
window.location.hash = "#" + b64.encode(JSON.stringify(data)); window.location.hash = "#" + b64.encode(JSON.stringify(data));
@ -130,5 +130,5 @@ function update() {
document.getElementById("downloadLink").href = `data:text/html,${html}` document.getElementById("downloadLink").href = `data:text/html,${html}`
// Update the <iframe> to display the generated page // Update the <iframe> to display the generated page
window.frames[0].location.replace(`data:text/html;charset=utf-8;base64,${b64.encode(decodeURIComponent(html))}`); window.frames[0].location.replace(`data:text/html;charset=utf-8;base64,${b64.encode(html)}`);
} }

View File

@ -6,6 +6,7 @@
<noscript>JavaScript is required to convert the URL into a usable web page.</noscript> <noscript>JavaScript is required to convert the URL into a usable web page.</noscript>
<script type="text/javascript" src="b64.js"></script> <script type="text/javascript" src="b64.js"></script>
<script type="text/javascript" src="api.js"></script>
<script type="text/javascript"> <script type="text/javascript">
if (window.location.hash) { if (window.location.hash) {
// Try to get page data from the URL if possible // Try to get page data from the URL if possible