Skip to content

Commit 7848df1

Browse files
committed
Upgrade to use versioned API with old fallback
1 parent e088eac commit 7848df1

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

api.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* api.js
3+
*
4+
* API For encoding and decoding URL hash objects
5+
*
6+
* Created by Jacob Strieb
7+
* July 2020
8+
*/
9+
10+
11+
/*******************************************************************************
12+
* Global Variables
13+
******************************************************************************/
14+
15+
const LATEST_API_VERSION = "0.0.1";
16+
// const LATEST_API_VERSION = "0.2.0";
17+
18+
var apiVersions = {};
19+
20+
21+
22+
/*******************************************************************************
23+
* API Version 0.2.0 (Latest)
24+
******************************************************************************/
25+
26+
apiVersions["0.2.0"] = {
27+
28+
VERSION: "0.2.0",
29+
30+
/* Return a link to view the page */
31+
getViewLink: function(pageData) {
32+
var urlData = {
33+
version: this.VERSION,
34+
};
35+
36+
const hashObject = b64.encode(JSON.stringify(urlData));
37+
return `http://jstrieb.github.io/urlpages/#${hashObject}`;
38+
},
39+
40+
}
41+
42+
43+
44+
/*******************************************************************************
45+
* API Version 0.0.1 (Original)
46+
******************************************************************************/
47+
48+
apiVersions["0.0.1"] = {
49+
50+
VERSION: "0.0.1",
51+
52+
/* Return a link to view the page */
53+
getViewLink: function(pageData) {
54+
return `http://jstrieb.github.io/urlpages/#${b64.encode(pageData)}`;
55+
},
56+
57+
}

editor/editor.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* in the documentation
44
*/
55

6+
api = apiVersions[LATEST_API_VERSION]
7+
68

79

810
/***
@@ -35,13 +37,6 @@ ${data["html"]}
3537
}
3638

3739

38-
/* Return a link to view the page */
39-
function getViewLink(pageData) {
40-
return `http://jstrieb.github.io/urlpages/#${b64.encode(pageData)}`;
41-
}
42-
43-
44-
4540
/***
4641
* Button press functions
4742
***/
@@ -58,7 +53,7 @@ function setViewUrl() {
5853
var html = encodeURIComponent(getHTML(data));
5954

6055
// Update the URL for the "Short Link" button
61-
document.getElementById("url").value = getViewLink(html);
56+
document.getElementById("url").value = api.getViewLink(html);
6257
}
6358

6459

@@ -129,7 +124,7 @@ function update() {
129124
window.location.hash = "#" + b64.encode(JSON.stringify(data));
130125

131126
// Update the URL for the "Get Link" button
132-
document.getElementById("getLinkLink").href = getViewLink(html);
127+
document.getElementById("getLinkLink").href = api.getViewLink(html);
133128

134129
// Update the download link
135130
document.getElementById("downloadLink").href = `data:text/html,${html}`

editor/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<!-- Scripts -->
1313
<script src="../b64.js" type="text/javascript"></script>
14+
<script src="../api.js" type="text/javascript"></script>
1415
<script src="editor.js" type="text/javascript"></script>
1516
</head>
1617

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
// Try to get page data from the URL if possible
1212
var hash = window.location.hash.slice(1);
1313
var data = b64.decode(hash);
14-
document.write(decodeURIComponent(data));
14+
15+
try {
16+
var urlDataObject = JSON.parse(data);
17+
var api = apiVersions[urlDataObject.version];
18+
document.write(api.decode(urlDataObject));
19+
} catch (err) {
20+
document.write(decodeURIComponent(data));
21+
}
1522
} else {
1623
// Otherwise redirect to the editor
1724
window.location.replace("./editor");

0 commit comments

Comments
 (0)