Skip to content

Commit 53c3a91

Browse files
authored
Merge pull request #17 from ZanderBrown/patch-3
Use GitHub Releases API
2 parents 35152fd + 08bc799 commit 53c3a91

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

js/helpinject.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @fileoverview This file executes on load and is meant to check if the help
33
* page is the latest version and inject warnings into the page if not.
4-
* TODO: Need to use GitHub API to retrieve latest version published
54
*/
65

76
'use strict';
@@ -33,10 +32,47 @@
3332
return null;
3433
};
3534

36-
var getLatestReleaseVersion = function() {
37-
// TODO: This is a hard-coded version string, need to use GH API instead
38-
var version = "0.9.13";
39-
return semverRegex().test(version) ? semverRegex().exec(version)[0] : null;
35+
var getLatestReleaseVersion = function(cb) {
36+
// Setup a dummy console if window.console.error is unavaliable
37+
if (typeof console === "undefined") window.console = {};
38+
if (typeof console.error === "undefined") {
39+
console.error = function () {
40+
// Do nothing
41+
};
42+
}
43+
// Set a simple callback if none was provided
44+
if (typeof cb === "undefined") {
45+
cb = function (ver) {
46+
console.error('Latest Version is: ' + ver + '. No callback provided.');
47+
};
48+
}
49+
// On the basis Mu doesn't run on XP, IE6 support is irrelavent
50+
var xhr = new XMLHttpRequest();
51+
// We want the lastest release (doesn't include draft or preview releases)
52+
xhr.open('GET', 'https://api.github.com/repos/mu-editor/mu/releases/latest');
53+
xhr.onload = function() {
54+
// We got the data
55+
if (xhr.status === 200) {
56+
var lastest = {};
57+
try {
58+
// IE6 & IE7 support is superfluous
59+
lastest = JSON.parse(xhr.responseText);
60+
} catch (e) {
61+
console.error('Unable to parse release information', e);
62+
}
63+
// Run the callback
64+
cb(semverRegex().test(lastest.tag_name) ? semverNumbersOnly(semverRegex().exec(lastest.tag_name)[0]) : null);
65+
} else {
66+
// Something went wrong. Fail quietly
67+
console.error('Unable to fetch release information');
68+
}
69+
};
70+
// Unable to fetch (Blocked by filter, CORs or network error)
71+
xhr.onerror = function (err) {
72+
console.error('Unable to fetch release information', err);
73+
};
74+
// Send the request
75+
xhr.send();
4076
};
4177

4278
var isVersionLowerThan = function(baseVersion, compareVersion) {
@@ -52,19 +88,20 @@
5288
var helpInjectDiv = document.getElementById('mu-help-header-inject');
5389
if (helpInjectDiv) {
5490
var urlVersion = getUrlVersion();
55-
var latestVersion = getLatestReleaseVersion();
56-
if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) {
57-
helpInjectDiv.innerHTML = [
58-
'<div class="alert alert-danger" role="alert">',
59-
' <h2><strong>Update Mu!</strong></h2>',
60-
' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version</a>.</p>',
61-
'</div>',
62-
'<div role="alert" class="alert alert-warning">',
63-
' <h2><strong>This is not the latest Documentation!</strong></h2>',
64-
' <p>This documentation corresponds to the Mu version you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>',
65-
' <p><a href="/help">View documentation for newest versions of Mu.</a></p>',
66-
'</div>'].join('\n');
67-
}
91+
getLatestReleaseVersion(function (latestVersion) {
92+
if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) {
93+
helpInjectDiv.innerHTML = [
94+
'<div class="alert alert-danger" role="alert">',
95+
' <h2><strong>Update Mu!</strong></h2>',
96+
' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version (' + latestVersion + ')</a>.</p>',
97+
'</div>',
98+
'<div role="alert" class="alert alert-warning">',
99+
' <h2><strong>This is not the latest Documentation!</strong></h2>',
100+
' <p>This documentation corresponds to Mu version (' + urlVersion + ') which seems to be what you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>',
101+
' <p><a href="/help">View documentation for latest version of Mu.</a></p>',
102+
'</div>'].join('\n');
103+
}
104+
});
68105
}
69106
});
70107
})();

0 commit comments

Comments
 (0)