Last active
April 6, 2016 07:25
-
-
Save leegee/d2c1a9a270a8bf404c1a to your computer and use it in GitHub Desktop.
AJAX directory listing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AMD stub? | |
var define = define || function (deps,module){ return module } | |
define([], function () { | |
/** | |
Apache autoindex to hash | |
@param {string} uri - uri of `mod_autoindex` directory or similar HTML page | |
@param {RegExp} re - hyperlinks for files to select from directory listing | |
@param {function} [next] - optional callback | |
@param {function} [error] - optional callback | |
*/ | |
var Ls = function (properties) { | |
var self = this; | |
if (typeof properties !== 'object'){ | |
throw new TypeError('Ls requires an object as sole parameter.') | |
} | |
this.re = properties.re || /^.+\.(png|jpg|gif)$/; | |
this.next = properties.next || function () {}; | |
this.uris = []; | |
this.error = properties.error || function (e) { | |
throw new Error('Error getting '+self.uri); | |
}; | |
this.uri = properties.uri; | |
if (! this.hasOwnProperty('uri')){ | |
throw new TypeError('Ls requires a uri in its properties argument.') | |
} | |
var req = new XMLHttpRequest(); | |
req.open('GET', this.uri); | |
req.onload = function () { | |
if (req.status == 200) { | |
var html = document.createElement('div'); | |
html.innerHTML = req.response; | |
var anchorNodeList = html.querySelectorAll( 'a[href]' ); | |
for (var i = 0; i < anchorNodeList.length; ++i) { | |
var href = anchorNodeList[i].getAttribute('href'); | |
if (href.match( self.re )){ | |
self.uris.push( self.uri +'/'+ href ); | |
} | |
} | |
self.next( self.uris ); | |
} | |
else { | |
this.error(req.statusText); | |
} | |
}; | |
req.onerror = function() { | |
this.error(req.statusText); | |
}; | |
req.send(); | |
}; | |
return Ls; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment