Changeset 24657

Show
Ignore:
Timestamp:
11/22/08 23:44:10 (7 weeks ago)
Author:
janus_wel
Message:

change regular expression (for SunRed?)
cosmetic change

Files:
1 modified

Legend:

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

    r21797 r24657  
    11// Vimperator plugin: 'Cooperation LDRize Mappings - Niconico Flv Fetchearg || liberator.buffer.URLr' 
    22// Version: 0.4 
    3 // Last Change: 21-Oct-2008. Jan 2008 
     3// Last Change: 22-Nov-2008. Jan 2008 
    44// License: Creative Commons 
    55// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid 
     
    77// Cooperation LDRize Mappings - Niconico Flv Fetcher for vimperator0.6.* 
    88// Require LDRize Cooperation ver 0.14 
    9 (function(){ 
    10     function NiconicoFlvHandler(url,title){ 
    11         const nicoApiEndPoint = "http://www.nicovideo.jp/api/getflv?v="; 
    12         const nicoWatchEndPoint = "http://www.nicovideo.jp/watch/"; 
    13         var videoId = url.match(/\wm\d+/)[0]; 
    14         var fileName = title.replace(/[?\\\*\/:<>\|\"]/g,'_') + ".flv"; 
    15         httpGET(nicoApiEndPoint + videoId,function(apiResult){ 
    16             var flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]); 
    179 
    18             httpGET(nicoWatchEndPoint + videoId,function(watchPage){ 
    19                 try{ 
    20                     var DownloadManager = Cc["@mozilla.org/download-manager;1"] 
    21                         .getService(Ci.nsIDownloadManager); 
    22                     var WebBrowserPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] 
    23                         .createInstance(Ci.nsIWebBrowserPersist); 
     10( function () { 
    2411 
    25                     var sourceUri = makeURI(flvUrl,null,null); 
    26                     var file = DownloadManager.userDownloadsDirectory; 
    27                     file.appendRelativePath(fileName); 
    28                     var fileUri = makeFileURI(file); 
     12function NiconicoFlvHandler(url, title) { 
     13    const nicoApiEndPoint = 'http://www.nicovideo.jp/api/getflv?v='; 
     14    const nicoWatchEndPoint = 'http://www.nicovideo.jp/watch/'; 
     15    let videoId = url.match(/\w{2}\d+/)[0]; 
     16    let fileName = title.replace(/[?\\\*\/:<>\|\"]/g, '_') + '.flv'; 
    2917 
    30                     var download = DownloadManager.addDownload(0, sourceUri, fileUri, fileName, 
    31                             null, null, null, null, WebBrowserPersist); 
    32                     WebBrowserPersist.progressListener = download; 
    33                     WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file); 
    34                 }catch(e){log(e);liberator.echoerr(e)} 
    35             }); 
    36         }); 
    37     } 
     18    httpGET( 
     19        nicoApiEndPoint + videoId, 
     20        function (apiResult) { 
     21            let flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]); 
    3822 
    39     function setupLDRizeCooperationNiconicoFlvFetcher(){ 
    40         var NiconicoFlvFetcher = { 
    41             pattern: 'http://www.nicovideo.jp/watch/*', 
    42             handler: NiconicoFlvHandler, 
    43             wait: 5000 
     23            httpGET( 
     24                nicoWatchEndPoint + videoId, 
     25                function (watchPage) { 
     26                    try { 
     27                        let DownloadManager = Cc['@mozilla.org/download-manager;1'] 
     28                            .getService(Ci.nsIDownloadManager); 
     29                        let WebBrowserPersist = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'] 
     30                            .createInstance(Ci.nsIWebBrowserPersist); 
     31 
     32                        let sourceUri = makeURI(flvUrl, null, null); 
     33                        let file = DownloadManager.userDownloadsDirectory; 
     34                        file.appendRelativePath(fileName); 
     35                        let fileUri = makeFileURI(file); 
     36 
     37                        let download = DownloadManager.addDownload( 
     38                            0, sourceUri, fileUri, fileName, 
     39                            null, null, null, null, WebBrowserPersist 
     40                        ); 
     41                        WebBrowserPersist.progressListener = download; 
     42                        WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file); 
     43                    } 
     44                    catch (e) { 
     45                        log(e); 
     46                        liberator.echoerr(e); 
     47                    } 
     48                } // function (watchPage) 
     49            ); // httpGET 
     50        } // function (apiResult) 
     51    ); // httpGET 
     52} 
     53 
     54function setupLDRizeCooperationNiconicoFlvFetcher() { 
     55    let NiconicoFlvFetcher = { 
     56        pattern: 'http://www.nicovideo.jp/watch/*', 
     57        handler: NiconicoFlvHandler, 
     58        wait:    5000, 
     59    }; 
     60    this.convertHandlerInfo([NiconicoFlvFetcher]); 
     61    this.handlerInfo.unshift(NiconicoFlvFetcher); 
     62} 
     63 
     64if (liberator.plugins.LDRizeCooperation === undefined) { 
     65    liberator.plugins.LDRizeCooperationPlugins = liberator.plugins.LDRizeCooperationPlugins || []; 
     66    liberator.plugins.LDRizeCooperationPlugins.push(setupLDRizeCooperationNiconicoFlvFetcher); 
     67} 
     68else { 
     69    setupLDRizeCooperationNiconicoFlvFetcher.apply(liberator.plugins.LDRizeCooperation); 
     70} 
     71 
     72function httpGET(uri, callback) { 
     73    let xhr = new XMLHttpRequest(); 
     74    xhr.onreadystatechange = function () { 
     75        if (xhr.readyState === 4) { 
     76            if (xhr.status === 200) callback.call(this,xhr.responseText); 
     77            else throw new Error(xhr.statusText) 
    4478        } 
    45         this.convertHandlerInfo([NiconicoFlvFetcher]); 
    46         this.handlerInfo.unshift(NiconicoFlvFetcher); 
    47     } 
     79    }; 
     80    xhr.open('GET', uri, true); 
     81    xhr.send(null); 
     82} 
    4883 
    49     if(liberator.plugins.LDRizeCooperation == undefined){ 
    50         liberator.plugins.LDRizeCooperationPlugins = liberator.plugins.LDRizeCooperationPlugins || []; 
    51         liberator.plugins.LDRizeCooperationPlugins.push(setupLDRizeCooperationNiconicoFlvFetcher); 
    52     }else{ 
    53         setupLDRizeCooperationNiconicoFlvFetcher.apply(liberator.plugins.LDRizeCooperation); 
    54     } 
     84liberator.modules.commands.addUserCommand( 
     85    ['fetchflv'], 
     86    'Download flv file from Nicovideo', 
     87    function (arg) { 
     88        httpGET( 
     89            arg.string || liberator.modules.buffer.URL, 
     90            function (responseText) { 
     91                let [, title] = responseText.match(/<title>(.*?)<\/title>/i); 
     92                liberator.log(title); 
     93                NiconicoFlvHandler(arg.string || liberator.modules.buffer.URL, title); 
     94            } 
     95        ); 
     96    }, 
     97    {} 
     98); 
    5599 
    56     function httpGET(uri,callback){ 
    57         var xhr = new XMLHttpRequest(); 
    58         xhr.onreadystatechange = function(){ 
    59             if(xhr.readyState == 4){ 
    60                 if(xhr.status == 200) 
    61                     callback.call(this,xhr.responseText); 
    62                 else 
    63                     throw new Error(xhr.statusText) 
    64             } 
    65         }; 
    66         xhr.open("GET",uri,true); 
    67         xhr.send(null); 
    68     } 
    69     liberator.modules.commands.addUserCommand(['fetchflv'],'Download flv file from Nicovideo', 
    70         function(arg){ 
    71             httpGET(arg.string || liberator.modules.buffer.URL,function(responseText){ 
    72                 var [,title] = responseText.match(/<title>(.*?)<\/title>/i); 
    73                 liberator.log(title); 
    74                 NiconicoFlvHandler(arg.string || liberator.modules.buffer.URL,title); 
    75             }); 
    76         },{} 
    77     ); 
    78100})();