|
1 | 1 | /**
|
2 | 2 | * @fileoverview This file executes on load and is meant to check if the help
|
3 | 3 | * 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 |
7 | 4 | */
|
8 | 5 |
|
9 | 6 | 'use strict';
|
|
36 | 33 | };
|
37 | 34 |
|
38 | 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 | + } |
39 | 49 | // On the basis Mu doesn't run on XP, IE6 support is irrelavent
|
40 | 50 | var xhr = new XMLHttpRequest();
|
41 | 51 | // We want the lastest release (doesn't include draft or preview releases)
|
42 | 52 | xhr.open('GET', 'https://api.github.com/repos/mu-editor/mu/releases/latest');
|
43 | 53 | 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'); |
53 | 76 | }
|
54 |
| - } else { |
55 |
| - // Something went wrong. Fail quietly |
56 |
| - if (console && console.error) console.error('Unable to fetch release information'); |
57 | 77 | }
|
58 | 78 | };
|
| 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 | + }; |
59 | 83 | // Send the request
|
60 | 84 | xhr.send();
|
61 | 85 | };
|
|
86 | 110 | ' <p><a href="/help">View documentation for latest version of Mu.</a></p>',
|
87 | 111 | '</div>'].join('\n');
|
88 | 112 | }
|
89 |
| - }); |
| 113 | + }); |
90 | 114 | }
|
91 | 115 | });
|
92 | 116 | })();
|
0 commit comments