Skip to content

Commit

Permalink
Make requests Async Fixes #1 - TAG 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Apr 12, 2015
1 parent 48bb8f5 commit 9d6f8f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "GitHub Release Downloads",
"description": "This extension displays the number of downloads GitHub releases have on the releases page.",
"version": "0.1.2",
"version": "1.0.0",
"icons": {
"16": "icon16.png",
"48": "icon48.png",
Expand Down
47 changes: 29 additions & 18 deletions releases.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
//TODO async
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
switch (xmlHttp.readyState) {
case 0 : // UNINITIALIZED
case 1 : // LOADING
case 2 : // LOADED
case 3 : // INTERACTIVE
break;
case 4 : // COMPLETED
var releases = JSON.parse( xmlHttp.responseText );
var downloadMap = [];
for (var i in releases) {
for (var j in releases[i].assets) {
downloadMap[releases[i].assets[j].browser_download_url] = releases[i].assets[j].download_count;
}
}
var els = document.getElementsByTagName("a");
for (var i = 0, l = els.length; i < l; i++) {
var el = els[i];
if ( el.href in downloadMap ) {
var dwnCount = document.createElement( 'small' );
dwnCount.appendChild( document.createTextNode( ' - ' + downloadMap[el.href] + ' downloads' ) )
el.appendChild( dwnCount );
}
}
break;
default: console.log( 'Error: GitHub Release Donwload Count Request Errored.' );
}
};
xmlHttp.open( "GET",
document.URL.replace( '//github.com', '//api.github.com/repos' ).split( '/tag/' )[0],
false
true
);
xmlHttp.send( null );
var releases = JSON.parse( xmlHttp.responseText );
var downloadMap = [];
for (var i in releases) {
for (var j in releases[i].assets) {
downloadMap[releases[i].assets[j].browser_download_url] = releases[i].assets[j].download_count;
}
}
var els = document.getElementsByTagName("a");
for (var i = 0, l = els.length; i < l; i++) {
var el = els[i];
if ( el.href in downloadMap ) {
var dwnCount = document.createElement( 'small' );
dwnCount.appendChild( document.createTextNode( ' - ' + downloadMap[el.href] + ' downloads' ) )
el.appendChild( dwnCount );
}
}

0 comments on commit 9d6f8f8

Please sign in to comment.