Skip to content

Instantly share code, notes, and snippets.

@Yasushi
Created February 5, 2009 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Yasushi/58540 to your computer and use it in GitHub Desktop.
Save Yasushi/58540 to your computer and use it in GitHub Desktop.
/**
* flvdl - FLV downloader.
* This is fork of
* http://gist.github.com/53229
*/
+function(flvdl_siteinfo){
CmdUtils.CreateCommand({
name: "flvdl",
description: "FLV Downloader.",
icon: "chrome://ubiquity/skin/icons/favicon.ico",
_video_title: function(x, doc){
return !x ? "" : jQuery(flvdl_siteinfo[x].vttl, doc.body).text();
},
_siteinfo_key : function(doc){
var uri = Utils.url(doc.documentURI);
for(var x in flvdl_siteinfo)
if(uri.host.match(flvdl_siteinfo[x].host))
return x;
return;
},
preview: function(pb, inp){
var doc = CmdUtils.getDocument();
var x = this._siteinfo_key(doc);
var title = this._video_title(x, doc);
pb.innerHTML = !title
? "<h3><img src='"+this.icon+"'> could'nt find FLV movie.</h3>"
: '<h3><img src="'+flvdl_siteinfo[x].icon+'"> Download \"'+title+'\"</h3>';
},
execute: function(){
var doc = CmdUtils.getDocumentInsecure();
var x = this._siteinfo_key(doc);
var title = this._video_title(x, doc);
if(!title){
displayMessage({icon: this.icon, title: this.name,
text: "could'nt find FLV movie."});
return;
}
var filepicker = Cc['@mozilla.org/filepicker;1']
.createInstance(Ci.nsIFilePicker);
filepicker.init(CmdUtils.getWindow(),
"download FLV movie",
Ci.nsIFilePicker.modeSave);
filepicker.defaultString = title + ".flv";
if(filepicker.show() == Ci.nsIFilePicker.returnCancel)
return;
var targetURI = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService)
.newURI(flvdl_siteinfo[x].vuri(doc), null, null);
var dm = Cc["@mozilla.org/download-manager;1"]
.getService(Ci.nsIDownloadManager);
var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
.createInstance(Ci.nsIWebBrowserPersist);
persist.progressListener = dm.addDownload(
0, targetURI,filepicker.fileURL, filepicker.file.leafName,
null, null, null, null, persist);
persist.saveURI(targetURI, null, null, null, null, filepicker.file);
},
contributors: ["powchin".link("http://tako3.com/search/powchin")]
});
}({
youtube: {
icon: "http://s.ytimg.com/yt/favicon-vfl1123.ico",
host: "youtube\\.com",
vttl: "#watch-vid-title h1",
vuri: function(doc){
var uri = Utils.url(doc.documentURI);
if(!uri.path.match("[?&]v=([^&]+)"))
return;
var vid = RegExp.$1;
var params = jQuery("#movie_player", doc.body).attr("flashvars").split("&");
var t,fmt;
for(var i in params) {
if(!t && params[i].match("^t=(.*)"))
t = RegExp.$1;
if(!fmt && params[i].match("^fmt_map=(\\d+)"))
fmt = RegExp.$1;
}
return "http://www.youtube.com/get_video?video_id="
+ vid + "&t=" + t + "&fmt=" + fmt;
},
},
niconico: {
icon: "http://www.nicovideo.jp/img/favicon.ico",
host: "nicovideo\\.jp",
vttl: "#des_2 h1 a",
vuri: function(doc){
if(!Utils.url(doc.documentURI)
.spec.match("nicovideo\\.jp/watch/(.*)$"))
return;
var api = "http://www.nicovideo.jp/api/getflv?as3=1&v=" + RegExp.$1;
return unescape(
/\&url=(.*?)\&/.exec(
jQuery.ajax({url: api, async: false})
.responseText)[1]);
},
}
/* templte
key: { // Set as you like.
icon: "", // This will be shown in preview.
host: "", // Host name.
vttl: "", // Css selector of this video's title.
vuri: function(){}, // Decides flv resource's uri.
}
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment