Upgrade to use versioned API with old fallback

This commit is contained in:
Jacob Strieb 2020-07-14 23:01:17 -04:00
parent e088eac8a0
commit 7848df1b1b
4 changed files with 70 additions and 10 deletions

57
api.js Normal file
View file

@ -0,0 +1,57 @@
/**
* api.js
*
* API For encoding and decoding URL hash objects
*
* Created by Jacob Strieb
* July 2020
*/
/*******************************************************************************
* Global Variables
******************************************************************************/
const LATEST_API_VERSION = "0.0.1";
// const LATEST_API_VERSION = "0.2.0";
var apiVersions = {};
/*******************************************************************************
* API Version 0.2.0 (Latest)
******************************************************************************/
apiVersions["0.2.0"] = {
VERSION: "0.2.0",
/* Return a link to view the page */
getViewLink: function(pageData) {
var urlData = {
version: this.VERSION,
};
const hashObject = b64.encode(JSON.stringify(urlData));
return `http://jstrieb.github.io/urlpages/#${hashObject}`;
},
}
/*******************************************************************************
* API Version 0.0.1 (Original)
******************************************************************************/
apiVersions["0.0.1"] = {
VERSION: "0.0.1",
/* Return a link to view the page */
getViewLink: function(pageData) {
return `http://jstrieb.github.io/urlpages/#${b64.encode(pageData)}`;
},
}

View file

@ -3,6 +3,8 @@
* in the documentation
*/
api = apiVersions[LATEST_API_VERSION]
/***
@ -35,13 +37,6 @@ ${data["html"]}
}
/* Return a link to view the page */
function getViewLink(pageData) {
return `http://jstrieb.github.io/urlpages/#${b64.encode(pageData)}`;
}
/***
* Button press functions
***/
@ -58,7 +53,7 @@ function setViewUrl() {
var html = encodeURIComponent(getHTML(data));
// Update the URL for the "Short Link" button
document.getElementById("url").value = getViewLink(html);
document.getElementById("url").value = api.getViewLink(html);
}
@ -129,7 +124,7 @@ function update() {
window.location.hash = "#" + b64.encode(JSON.stringify(data));
// Update the URL for the "Get Link" button
document.getElementById("getLinkLink").href = getViewLink(html);
document.getElementById("getLinkLink").href = api.getViewLink(html);
// Update the download link
document.getElementById("downloadLink").href = `data:text/html,${html}`

View file

@ -11,6 +11,7 @@
<!-- Scripts -->
<script src="../b64.js" type="text/javascript"></script>
<script src="../api.js" type="text/javascript"></script>
<script src="editor.js" type="text/javascript"></script>
</head>

View file

@ -11,7 +11,14 @@ if (window.location.hash) {
// Try to get page data from the URL if possible
var hash = window.location.hash.slice(1);
var data = b64.decode(hash);
try {
var urlDataObject = JSON.parse(data);
var api = apiVersions[urlDataObject.version];
document.write(api.decode(urlDataObject));
} catch (err) {
document.write(decodeURIComponent(data));
}
} else {
// Otherwise redirect to the editor
window.location.replace("./editor");