Skip to content

Commit 91c325d

Browse files
authored
Address comments by @carlosperate
1 parent e9ce35d commit 91c325d

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

js/helpinject.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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-
*
5-
* Changes:
6-
* - 2017/04/22: Use the GitHub Releases API to check if this is the lastest documentation
74
*/
85

96
'use strict';
@@ -36,26 +33,53 @@
3633
};
3734

3835
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+
}
3949
// On the basis Mu doesn't run on XP, IE6 support is irrelavent
4050
var xhr = new XMLHttpRequest();
4151
// We want the lastest release (doesn't include draft or preview releases)
4252
xhr.open('GET', 'https://api.github.com/repos/mu-editor/mu/releases/latest');
4353
xhr.onload = function() {
44-
// We got the data
45-
if (xhr.status === 200) {
46-
// IE6 & IE7 support is superfluous
47-
try {
48-
var lastest = JSON.parse(xhr.responseText);
49-
// Run the callback
50-
cb(semverRegex().test(lastest.tag_name) ? semverRegex().exec(lastest.tag_name)[0] : null);
51-
} catch (e) {
52-
if (console && console.error) console.error('Unable to parse release information', e);
54+
// The request compleated
55+
if (xhr.readyState == 4) {
56+
// We got the data
57+
if (xhr.status === 200) {
58+
var lastest = {};
59+
try {
60+
// IE6 & IE7 support is superfluous
61+
lastest = JSON.parse(xhr.responseText);
62+
} catch (e) {
63+
console.error('Unable to parse release information', e);
64+
}
65+
if (typeof lastest.tag_name !== "undefined") {
66+
// Drop the 'v' from the start of the tag name
67+
var version = lastest.tag_name.substr(0, 1) == 'v' ? lastest.tag_name.substr(1) : lastest.tag_name;
68+
// Run the callback
69+
cb(semverRegex().test(version) ? semverRegex().exec(version)[0] : null);
70+
} else {
71+
console.error('GitHub did not return the expected data');
72+
}
73+
} else {
74+
// Something went wrong. Fail quietly
75+
console.error('Unable to fetch release information');
5376
}
54-
} else {
55-
// Something went wrong. Fail quietly
56-
if (console && console.error) console.error('Unable to fetch release information');
5777
}
5878
};
79+
// Unable to fetch (Blocked by filter, CORs or network error)
80+
xhr.onerror = function (err) {
81+
console.error('Unable to fetch release information', err);
82+
};
5983
// Send the request
6084
xhr.send();
6185
};
@@ -86,7 +110,7 @@
86110
' <p><a href="/help">View documentation for latest version of Mu.</a></p>',
87111
'</div>'].join('\n');
88112
}
89-
});
113+
});
90114
}
91115
});
92116
})();

0 commit comments

Comments
 (0)