Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created January 17, 2011 09:13
Show Gist options
  • Save ynkdir/782642 to your computer and use it in GitHub Desktop.
Save ynkdir/782642 to your computer and use it in GitHub Desktop.
ニコニコ動画ダウンロードブックマークレット
(function(){
var http_get = function(url) {
var async = false;
var req = new XMLHttpRequest();
req.open("GET", url, async);
req.withCredentials = true;
req.send();
return req.responseText;
};
var http_post = function(url, params) {
var s = encode_params(params);
var async = false;
var req = new XMLHttpRequest();
req.open("POST", url, async);
req.withCredentials = true;
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-Length", s.length);
req.send(s);
return req.responseText;
};
var encode_params = function(params) {
var s = "";
for (var key in params) {
s += encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
}
return s;
};
var decode_params = function(params) {
var o = {};
var a = params.split("&");
for (var i = 0; i < a.length; i++) {
var b = a[i].split("=");
var key = decodeURIComponent(b[0]);
var value = decodeURIComponent(b[1]);
o[key] = value;
}
return o;
};
var download = function(url) {
var div = document.createElement("div");
div.innerHTML = '右クリックで保存 <a href="' + url + '">' + url + '</a>';
document.body.insertBefore(div, document.body.firstChild);
};
var getflv = function(id) {
var url = "http://flapi.nicovideo.jp/api/getflv?v=" + id;
var t = http_get(url);
var o = decode_params(t);
download(o.url);
};
getflv(Video.v);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment