Changeset 8555

Show
Ignore:
Timestamp:
03/31/08 12:30:36 (8 months ago)
Author:
trapezoid
Message:

lang/javascript/vimperator-plugins/trunk/ldrize_cooperation.js: add niconico downloader, add help

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/ldrize_cooperation.js

    r8532 r8555  
    11// Vimperator plugin: 'Cooperation LDRize Mappings' 
    2 // Version: 0.10 
    3 // Last Change: 30-Mar-2008. Jan 2008 
     2// Version: 0.11 
     3// Last Change: 31-Mar-2008. Jan 2008 
    44// License: Creative Commons 
    55// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid 
    66// 
    77// Cooperation LDRize Mappings for vimperator0.6.* 
     8// 
     9// Mappings: 
     10//      replace for captureMapppings 
     11//      default: 'j','k','p','o' 
     12// Commands: 
     13//  'm' or 'mb' or 'minibuffer': 
     14//      Execute args as Minibuffer Command 
     15//      usage: :minibuffer pinned-link | open | clear-pin 
     16//  'pin': 
     17//      View pinned link list 
     18//      usage: :pin 
     19//  'pindownload': 
     20//      Download View pinned link by handler function or outer promgram. please see 'handlerInfo' also 
     21//      usage: :pindownload 
     22//  'ldrc' or 'toggleldrizecooperation': 
     23//      Toggle LDRize Cooperation 
     24//      usage: :toggleldrizecooperation 
     25// Options: 
     26//  'ldrc' 
     27//      Enable LDRize Cooperation 
     28//      usage: :set ldrc 
     29//  'noldrc' 
     30//      Disable LDRize Cooperation 
     31//      usage: :set noldrc 
    832(function(){ 
    9     var scrollCount = 3; 
     33    var captureMappings = ['j','k','p','o']; 
    1034    //pattern: wildcard 
    1135    //include: [regexp, option] or regexp 
     
    1741        //    wait: 5000 
    1842        //}, 
     43        //Niconico Downloader 
     44        { 
     45            pattern: 'http://www.nicovideo.jp/watch/*', 
     46            handler: function(url,title){ 
     47                const nicoApiEndPoint = "http://www.nicovideo.jp/api/getflv?v="; 
     48                const nicoWatchEndPoint = "http://www.nicovideo.jp/watch/"; 
     49                plugins.title = title; 
     50                var videoId = url.match(/\wm\d+/)[0]; 
     51                httpGET(nicoApiEndPoint + videoId,function(apiResult){ 
     52                    var flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]); 
     53 
     54                    httpGET(nicoWatchEndPoint + videoId,function(watchPage){ 
     55                        try{ 
     56                            var DownloadManager = Cc["@mozilla.org/download-manager;1"] 
     57                                .getService(Ci.nsIDownloadManager); 
     58                            var WebBrowserPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] 
     59                                .createInstance(Ci.nsIWebBrowserPersist); 
     60 
     61                            var sourceUri = makeURI(flvUrl,null,null); 
     62                            var file = DownloadManager.userDownloadsDirectory; 
     63                            file.appendRelativePath(title + ".flv"); 
     64                            var fileUri = makeFileURI(file); 
     65 
     66                            var download = DownloadManager.addDownload(0, sourceUri, fileUri, title + ".flv", 
     67                                    null, null, null, null, WebBrowserPersist); 
     68                            WebBrowserPersist.progressListener = download; 
     69                            WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file); 
     70                        }catch(e){log(e);liberator.echoerr(e)} 
     71                    }); 
     72                }); 
     73            }, 
     74            wait: 5000 
     75        }, 
    1976        //{ 
    2077        //    handler: ['C:\\usr\\irvine\\irvine.exe',['%URL%']], 
    2178        //}, 
    2279    ]; 
    23     var captureMappings = ['j','k','p','o']; 
    2480 
    2581    handlerInfo.forEach(function(x){ 
     
    115171    function getPinnedItems(){ 
    116172        var linkXpath = LDRize.getSiteinfo()['link']; 
    117         var viewXpath = LDRize.getSiteinfo()['view']; 
    118         return LDRize.getPinnedItems().map(function(i){return [i.XPath(linkXpath),i.XPath(viewXpath)]}); 
     173        var viewXpath = LDRize.getSiteinfo()['view'] || linkXpath + "/text()"; 
     174        return LDRize.getPinnedItems().map(function(i){return [i.XPath(linkXpath),i.XPath(viewXpath).textContent]}); 
    119175    } 
    120176    function downloadLinksByProgram(links){ 
    121177        var count = 0; 
    122         links.forEach(function([link,title]){ 
     178        links.forEach(function([url,title]){ 
    123179            for each(let x in handlerInfo){ 
    124                 if(x.include.test(link)){ 
     180                if(x.include.test(url)){ 
    125181                    setTimeout(function(){ 
    126182                        if(typeof x.handler == "object"){ 
    127                             var args = x.handler[1].map(function(s){ return s.replace(/%URL%/g,link).replace(/%TITLE%/g,title); }); 
     183                            var args = x.handler[1].map(function(s){ return s.replace(/%URL%/g,url).replace(/%TITLE%/g,title); }); 
    128184                            liberator.io.run(x.handler[0],args,false); 
    129185                        }else if(typeof x.handler == "string"){ 
    130                             liberator.io.run(x.handler,[link],false); 
     186                            liberator.io.run(x.handler,[url],false); 
    131187                        }else if(typeof x.handler == "function"){ 
    132                             x.handler(link,title); 
     188                            x.handler(url.toString(),title); 
    133189                        } 
    134190                    },x.wait != undefined ? x.wait * count++ : 0); 
     
    138194            liberator.echoerr("LDRize Cooperation: download pattern not found!!"); 
    139195        }); 
     196    } 
     197 
     198    function httpGET(uri,callback){ 
     199        var xhr = new XMLHttpRequest(); 
     200        xhr.onreadystatechange = function(){ 
     201            if(xhr.readyState == 4){ 
     202                if(xhr.status == 200) 
     203                    callback.call(this,xhr.responseText); 
     204                else 
     205                    throw new Error(xhr.statusText) 
     206            } 
     207        }; 
     208        xhr.open("GET",uri,true); 
     209        xhr.send(null); 
    140210    } 
    141211