Skip to content

Instantly share code, notes, and snippets.

@umbrant
Last active September 26, 2017 10:48
Show Gist options
  • Save umbrant/5289176 to your computer and use it in GitHub Desktop.
Save umbrant/5289176 to your computer and use it in GitHub Desktop.
Bookmarklet to play audio files right from the Namenode web UI!
javascript:(function(){
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return uri;
};
parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
function makeUrl(p) {
var path = "/streamFile" + decodeURIComponent(p.queryKey["dir"]);
return p.protocol + "://" + p.authority + path + "?nnaddr=" + p.queryKey["nnaddr"];
};
var elements = document.getElementsByTagName("a");
for(var i=0; i<elements.length; i++) {
var e = elements[i];
if (e.href.indexOf("browseDirectory.jsp") !== -1) {
var p = parseUri(e.href);
var dir = p.queryKey["dir"];
var type = "";
if (dir.indexOf(".ogg") !== -1) {
type = "audio/ogg";
}
else if (dir.indexOf(".mp3") !== -1) {
type = "audio/mpeg";
}
else if (dir.indexOf(".wav") !== -1) {
type = "audio/wav";
}
if (type == "") {
continue;
}
var url = makeUrl(p);
var audio = new Audio();
audio.setAttribute("controls", "");
audio.setAttribute("style", "float:right");
audio.load();
var source = document.createElement("source");
source.setAttribute("src", url);
source.setAttribute("type", type);
audio.appendChild(source);
e.parentNode.appendChild(audio);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment