From 7daea84b0e397d5959d2927ece6c788c105411a7 Mon Sep 17 00:00:00 2001 From: trapezoid Date: Sun, 6 Apr 2008 01:46:41 +0000 Subject: lang/javascript/vimperator-plugins/trunk/ldrize_cooperation_fetch_flv.js: add :fetchflv command git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@8967 d0d07461-0603-4401-acd4-de1884942a52 --- ldrize_cooperation_fetch_flv.js | 77 +++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 33 deletions(-) (limited to 'ldrize_cooperation_fetch_flv.js') diff --git a/ldrize_cooperation_fetch_flv.js b/ldrize_cooperation_fetch_flv.js index d4b61bc..b8bec88 100644 --- a/ldrize_cooperation_fetch_flv.js +++ b/ldrize_cooperation_fetch_flv.js @@ -1,43 +1,45 @@ -// Vimperator plugin: 'Cooperation LDRize Mappings - Niconico Flv Fetcher' -// Version: 0.3 -// Last Change: 03-Apr-2008. Jan 2008 +// Vimperator plugin: 'Cooperation LDRize Mappings - Niconico Flv Fetchearg || liberator.buffer.URLr' +// Version: 0.4 +// Last Change: 06-Apr-2008. Jan 2008 // License: Creative Commons // Maintainer: Trapezoid - http://unsigned.g.hatena.ne.jp/Trapezoid // // Cooperation LDRize Mappings - Niconico Flv Fetcher for vimperator0.6.* // Require LDRize Cooperation ver 0.14 (function(){ - function LDRizeCooperationNiconicoFlvFetcher(){ - var NiconicoFlvFetcher = { - pattern: 'http://www.nicovideo.jp/watch/*', - handler: function(url,title){ - const nicoApiEndPoint = "http://www.nicovideo.jp/api/getflv?v="; - const nicoWatchEndPoint = "http://www.nicovideo.jp/watch/"; - var videoId = url.match(/\wm\d+/)[0]; - var fileName = title.replace(/[?\\\*\/:<>\|\"]/g,'_') + ".flv"; - httpGET(nicoApiEndPoint + videoId,function(apiResult){ - var flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]); + function NiconicoFlvHandler(url,title){ + const nicoApiEndPoint = "http://www.nicovideo.jp/api/getflv?v="; + const nicoWatchEndPoint = "http://www.nicovideo.jp/watch/"; + var videoId = url.match(/\wm\d+/)[0]; + var fileName = title.replace(/[?\\\*\/:<>\|\"]/g,'_') + ".flv"; + httpGET(nicoApiEndPoint + videoId,function(apiResult){ + var flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]); + + httpGET(nicoWatchEndPoint + videoId,function(watchPage){ + try{ + var DownloadManager = Cc["@mozilla.org/download-manager;1"] + .getService(Ci.nsIDownloadManager); + var WebBrowserPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] + .createInstance(Ci.nsIWebBrowserPersist); - httpGET(nicoWatchEndPoint + videoId,function(watchPage){ - try{ - var DownloadManager = Cc["@mozilla.org/download-manager;1"] - .getService(Ci.nsIDownloadManager); - var WebBrowserPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] - .createInstance(Ci.nsIWebBrowserPersist); + var sourceUri = makeURI(flvUrl,null,null); + var file = DownloadManager.userDownloadsDirectory; + file.appendRelativePath(fileName); + var fileUri = makeFileURI(file); - var sourceUri = makeURI(flvUrl,null,null); - var file = DownloadManager.userDownloadsDirectory; - file.appendRelativePath(fileName); - var fileUri = makeFileURI(file); + var download = DownloadManager.addDownload(0, sourceUri, fileUri, fileName, + null, null, null, null, WebBrowserPersist); + WebBrowserPersist.progressListener = download; + WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file); + }catch(e){log(e);liberator.echoerr(e)} + }); + }); + } - var download = DownloadManager.addDownload(0, sourceUri, fileUri, fileName, - null, null, null, null, WebBrowserPersist); - WebBrowserPersist.progressListener = download; - WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file); - }catch(e){log(e);liberator.echoerr(e)} - }); - }); - }, + function setupLDRizeCooperationNiconicoFlvFetcher(){ + var NiconicoFlvFetcher = { + pattern: 'http://www.nicovideo.jp/watch/*', + handler: NiconicoFlvHandler, wait: 5000 } this.convertHandlerInfo([NiconicoFlvFetcher]); @@ -46,9 +48,9 @@ if(liberator.plugins.LDRizeCooperation == undefined){ liberator.plugins.LDRizeCooperationPlugins = liberator.plugins.LDRizeCooperationPlugins || []; - liberator.plugins.LDRizeCooperationPlugins.push(LDRizeCooperationNiconicoFlvFetcher); + liberator.plugins.LDRizeCooperationPlugins.push(setupLDRizeCooperationNiconicoFlvFetcher); }else{ - LDRizeCooperationNiconicoFlvFetcher.apply(liberator.plugins.LDRizeCooperation); + setupLDRizeCooperationNiconicoFlvFetcher.apply(liberator.plugins.LDRizeCooperation); } function httpGET(uri,callback){ @@ -64,4 +66,13 @@ xhr.open("GET",uri,true); xhr.send(null); } + liberator.commands.addUserCommand(['fetchflv'],'Download flv file from Nicovideo', + function(arg){ + httpGET(arg || liberator.buffer.URL,function(responseText){ + var [,title] = responseText.match(/(.*?)<\/title>/i); + liberator.log(title); + NiconicoFlvHandler(arg || liberator.buffer.URL,title); + }); + },{} + ); })(); -- cgit v1.2.3