Changeset 25714

Show
Ignore:
Timestamp:
12/02/08 23:47:13 (7 months ago)
Author:
suVene
Message:

*SITEINFOと共にkeymap指定
*複数リクエスト時定義順に出力
*property名変更とか。

Location:
lang/javascript/vimperator-plugins
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/branches/1.2/multi_requester.js

    r25648 r25714  
    55 * @description-ja   リクエストの結果をバッファに出力する。 
    66 * @author           suVene suvene@zeromemory.info 
    7  * @version          0.4.0 
     7 * @version          0.4.1 
    88 * @minVersion       1.2 
    99 * @maxVersion       1.2 
    10  * Last Change:      01-Dec-2008. 
     10 * Last Change:      02-Dec-2008. 
    1111 * ==/VimperatorPlugin== 
    1212 * 
     
    3636 *   liberator.globalVariables.multi_requester_siteinfo = [ 
    3737 *       { 
    38  *           name:          'ex',                           // required 
    39  *           description:   'example',                      // required 
    40  *           url:           'http://example.com/?%s',       // required, %s <-- replace string 
    41  *           xpath:         '//*',                          // optional(default all) 
    42  *           srcEncode:     'SHIFT_JIS',                    // optional(default UTF-8) 
    43  *           urlEncode:     'SHIFT_JIS',                    // optional(default srcEncode) 
    44  *           ignoreTags:    'img'                           // optional(default script), syntax 'tag1,tag2,……' 
    45  *           extractLink:   '//xpath'                       // optional extract permalink' 
     38 *           map:           ',me',                          // optional: keymap for this siteinfo call 
     39 *           bang:          true,                           // optional: 
     40 *           args:          'any'                           // optional: 
     41 *           name:          'ex',                           // required: subcommand name 
     42 *           description:   'example',                      // required: commandline short help 
     43 *           url:           'http://example.com/?%s',       // required: %s <-- replace string 
     44 *           xpath:         '//*',                          // optional: default all 
     45 *           srcEncode:     'SHIFT_JIS',                    // optional: default UTF-8 
     46 *           urlEncode:     'SHIFT_JIS',                    // optional: default srcEncode 
     47 *           ignoreTags:    'img',                          // optional: default script, syntax 'tag1,tag2,……' 
     48 *           extractLink:   '//xpath'                       // optional: extract permalink 
    4649 *       }, 
    4750 *   ]; 
     
    185188    }, 
    186189    addUserMaps: function(prefix, mapdef) { 
    187         mapdef.forEach(function([key, command, special, args]) { 
    188             var cmd = prefix + (special ? '! ' : ' ') + command + ' '; 
    189             liberator.mappings.addUserMap( 
    190                 [liberator.modes.NORMAL, liberator.modes.VISUAL], 
     190        mapdef.forEach(function([key, command, bang, args]) { 
     191            var cmd = prefix + (bang ? '! ' : ' ') + command + ' '; 
     192            mappings.addUserMap( 
     193                [modes.NORMAL, modes.VISUAL], 
    191194                [key], 
    192195                'user defined mapping', 
     
    330333}; 
    331334Response.prototype = { 
    332     initialize: function(request) { 
    333         this.request = request; 
    334         this.transport = request.transport; 
    335         this.isSuccess = request.isSuccess(); 
     335    initialize: function(req) { 
     336        this.req = req; 
     337        this.transport = req.transport; 
     338        this.isSuccess = req.isSuccess(); 
    336339        this.readyState = this.transport.readyState; 
    337340 
     
    357360            this.htmlFragmentstr = this.responseText.replace(/^[\s\S]*?<html(?:[ \t\n\r][^>]*)?>|<\/html[ \t\r\n]*>[\S\s]*$/ig, '').replace(/[\r\n]+/g, ' '); 
    358361            let ignoreTags = ['script']; 
    359             if (this.request.options.siteinfo.ignoreTags) { 
    360                 ignoreTags.concat(this.request.options.siteinfo.ignoreTags.split(',')); 
     362            if (this.req.options.siteinfo.ignoreTags) { 
     363                ignoreTags.concat(this.req.options.siteinfo.ignoreTags.split(',')); 
    361364            } 
    362365            this.htmlStripScriptFragmentstr = $U.stripTags(this.htmlFragmentstr, 'script'); 
     
    406409    getSiteInfo: function() { 
    407410 
     411        var self = this; 
    408412        var useWedata = typeof liberator.globalVariables.multi_requester_use_wedata == 'undefined' ? 
    409413                        true : $U.eval(liberator.globalVariables.multi_requester_use_wedata); 
     414 
     415        if (liberator.globalVariables.multi_requester_siteinfo) { 
     416            liberator.globalVariables.multi_requester_siteinfo.forEach(function(site) { 
     417                if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
     418                $U.extend(mergedSiteinfo[site.name], site); 
     419                if (site.map) { 
     420                    CommandRegister.addUserMaps(MultiRequester.name[0], 
     421                        [[site.map, site.name, site.bang, site.args]]); 
     422                } 
     423            }); 
     424        } 
     425 
     426        SITEINFO.forEach(function(site) { 
     427            if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
     428            $U.extend(mergedSiteinfo[site.name], site); 
     429            if (site.map) { 
     430                CommandRegister.addUserMaps(MultiRequester.name[0], 
     431                    [[site.map, site.name, site.bang, site.args]]); 
     432            } 
     433        }); 
    410434 
    411435        if (useWedata) { 
     
    417441            }); 
    418442        } 
    419  
    420         if (liberator.globalVariables.multi_requester_siteinfo) { 
    421             liberator.globalVariables.multi_requester_siteinfo.forEach(function(site) { 
    422                 if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
    423                 $U.extend(mergedSiteinfo[site.name], site); 
    424             }); 
    425         } 
    426  
    427         SITEINFO.forEach(function(site) { 
    428             if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
    429             $U.extend(mergedSiteinfo[site.name], site); 
    430         }); 
    431443 
    432444        return $U.A(mergedSiteinfo); 
     
    453465// main controller {{{ 
    454466var MultiRequester = { 
    455     doProcess: false, 
    456     requestCount: 0, 
    457     echoList: [], 
    458467    name: DataAccess.getCommand(), 
    459468    description: 'request, and display to the buffer', 
    460     cmdAction: function(args, bang, count) { 
     469    doProcess: false, 
     470    requestNames: '', 
     471    requestCount: 0, 
     472    echoHash: {}, 
     473    cmdAction: function(args, bang, count) { // {{{ 
    461474 
    462475        if (MultiRequester.doProcess) return; 
     
    466479 
    467480        MultiRequester.doProcess = true; 
     481        MultiRequester.requestNames = parsedArgs.names; 
    468482        MultiRequester.requestCount = 0; 
    469         MultiRequester.echoList = []; 
     483        MultiRequester.echoHash = {}; 
    470484        var siteinfo = parsedArgs.siteinfo; 
    471485        for (let i = 0, len = parsedArgs.count; i < len; i++) { 
     
    549563        }); 
    550564        return ret; 
    551     }, 
    552     extractLink: function(res, extractLink) { 
     565    }, // }}} 
     566    extractLink: function(res, extractLink) { //{{{ 
    553567 
    554568        var el = res.getHTMLDocument(extractLink); 
     
    556570        var a = el.firstChild; 
    557571        var url = $U.pathToURL((a.href || a.action || a.value)); 
    558         var req = new Request(url, null, $U.extend(res.request.options, {extractLink: true})); 
     572        var req = new Request(url, null, $U.extend(res.req.options, {extractLink: true})); 
    559573        req.addEventListener('onException', $U.bind(this, this.onException)); 
    560574        req.addEventListener('onSuccess', $U.bind(this, this.onSuccess)); 
     
    564578        MultiRequester.doProcess = true; 
    565579 
    566     }, 
    567     onSuccess: function(res) { 
     580    }, //}}} 
     581    onSuccess: function(res) { //{{{ 
    568582 
    569583        if (!MultiRequester.doProcess) { 
     
    572586        } 
    573587 
    574         $U.log('success!!!' + res.request.url); 
     588        $U.log('success!!!:' + res.req.url); 
    575589        MultiRequester.requestCount--; 
    576590        if (MultiRequester.requestCount == 0) { 
     
    584598            if (!res.isSuccess || res.responseText == '') throw 'response is fail or null'; 
    585599 
    586             url = res.request.url; 
     600            url = res.req.url; 
    587601            escapedUrl = liberator.util.escapeHTML(url); 
    588             xpath = res.request.options.siteinfo.xpath; 
    589             extractLink = res.request.options.siteinfo.extractLink; 
    590  
    591             if (extractLink && !res.request.options.extractLink) { 
     602            xpath = res.req.options.siteinfo.xpath; 
     603            extractLink = res.req.options.siteinfo.extractLink; 
     604 
     605            if (extractLink && !res.req.options.extractLink) { 
    592606                this.extractLink(res, extractLink); 
    593607                return; 
     
    598612 
    599613            html = '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
    600                    (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, 
    601                             function(all) all.toLowerCase()); 
    602  
    603             MultiRequester.echoList.push(html); 
     614                   (new XMLSerializer()).serializeToString(doc) 
     615                            .replace(/<[^>]+>/g, function(all) all.toLowerCase()) 
     616                            .replace(/<!--[^>]*-->/g, ''); 
     617 
     618            MultiRequester.echoHash[res.req.options.siteinfo.name] = html; 
    604619 
    605620        } catch (e) { 
    606621            $U.log('error!!: ' + e); 
    607             MultiRequester.echoList.push('<span style="color: red;">error!!:' + e + '</span>'); 
     622            MultiRequester.echoHash[res.req.options.siteinfo.name] = 
     623                            '<span style="color: red;">error!!: ' + e + '</span>'; 
    608624        } 
    609625 
    610626        if (MultiRequester.requestCount == 0) { 
    611             let html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
    612                         MultiRequester.echoList.join('') + 
    613                         '</div>'; 
     627            let echoList = []; 
     628            MultiRequester.requestNames.split(',').forEach(function(name) { 
     629                echoList.push(MultiRequester.echoHash[name]) 
     630            }); 
     631            html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
     632                   echoList.join('') + 
     633                   '</div>'; 
    614634            try { $U.echo(new XMLList(html)); } catch (e) { $U.log(e); $U.echo(html); } 
    615635        } 
     
    623643        MultiRequester.doProcess = false; 
    624644        $U.echoerr('exception!!: ' + e); 
    625     } 
     645    }//}}} 
    626646}; 
    627647//}}} 
  • lang/javascript/vimperator-plugins/trunk/multi_requester.js

    r25647 r25714  
    55 * @description-ja   リクエストの結果をバッファに出力する。 
    66 * @author           suVene suvene@zeromemory.info 
    7  * @version          0.4.0 
     7 * @version          0.4.1 
    88 * @minVersion       2.0pre 
    99 * @maxVersion       2.0pre 
    10  * Last Change:      01-Dec-2008. 
     10 * Last Change:      02-Dec-2008. 
    1111 * ==/VimperatorPlugin== 
    1212 * 
     
    3636 *   liberator.globalVariables.multi_requester_siteinfo = [ 
    3737 *       { 
    38  *           name:          'ex',                           // required 
    39  *           description:   'example',                      // required 
    40  *           url:           'http://example.com/?%s',       // required, %s <-- replace string 
    41  *           xpath:         '//*',                          // optional(default all) 
    42  *           srcEncode:     'SHIFT_JIS',                    // optional(default UTF-8) 
    43  *           urlEncode:     'SHIFT_JIS',                    // optional(default srcEncode) 
    44  *           ignoreTags:    'img'                           // optional(default script), syntax 'tag1,tag2,……' 
    45  *           extractLink:   '//xpath'                       // optional extract permalink' 
     38 *           map:           ',me',                          // optional: keymap for this siteinfo call 
     39 *           bang:          true,                           // optional: 
     40 *           args:          'any'                           // optional: 
     41 *           name:          'ex',                           // required: subcommand name 
     42 *           description:   'example',                      // required: commandline short help 
     43 *           url:           'http://example.com/?%s',       // required: %s <-- replace string 
     44 *           xpath:         '//*',                          // optional: default all 
     45 *           srcEncode:     'SHIFT_JIS',                    // optional: default UTF-8 
     46 *           urlEncode:     'SHIFT_JIS',                    // optional: default srcEncode 
     47 *           ignoreTags:    'img',                          // optional: default script, syntax 'tag1,tag2,……' 
     48 *           extractLink:   '//xpath'                       // optional: extract permalink 
    4649 *       }, 
    4750 *   ]; 
     
    186189    }, 
    187190    addUserMaps: function(prefix, mapdef) { 
    188         mapdef.forEach(function([key, command, special, args]) { 
    189             var cmd = prefix + (special ? '! ' : ' ') + command + ' '; 
     191        mapdef.forEach(function([key, command, bang, args]) { 
     192            var cmd = prefix + (bang ? '! ' : ' ') + command + ' '; 
    190193            mappings.addUserMap( 
    191194                [modes.NORMAL, modes.VISUAL], 
     
    331334}; 
    332335Response.prototype = { 
    333     initialize: function(request) { 
    334         this.request = request; 
    335         this.transport = request.transport; 
    336         this.isSuccess = request.isSuccess(); 
     336    initialize: function(req) { 
     337        this.req = req; 
     338        this.transport = req.transport; 
     339        this.isSuccess = req.isSuccess(); 
    337340        this.readyState = this.transport.readyState; 
    338341 
     
    358361            this.htmlFragmentstr = this.responseText.replace(/^[\s\S]*?<html(?:[ \t\n\r][^>]*)?>|<\/html[ \t\r\n]*>[\S\s]*$/ig, '').replace(/[\r\n]+/g, ' '); 
    359362            let ignoreTags = ['script']; 
    360             if (this.request.options.siteinfo.ignoreTags) { 
    361                 ignoreTags.concat(this.request.options.siteinfo.ignoreTags.split(',')); 
     363            if (this.req.options.siteinfo.ignoreTags) { 
     364                ignoreTags.concat(this.req.options.siteinfo.ignoreTags.split(',')); 
    362365            } 
    363366            this.htmlStripScriptFragmentstr = $U.stripTags(this.htmlFragmentstr, 'script'); 
     
    407410    getSiteInfo: function() { 
    408411 
     412        var self = this; 
    409413        var useWedata = typeof liberator.globalVariables.multi_requester_use_wedata == 'undefined' ? 
    410414                        true : $U.eval(liberator.globalVariables.multi_requester_use_wedata); 
     415 
     416        if (liberator.globalVariables.multi_requester_siteinfo) { 
     417            liberator.globalVariables.multi_requester_siteinfo.forEach(function(site) { 
     418                if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
     419                $U.extend(mergedSiteinfo[site.name], site); 
     420                if (site.map) { 
     421                    CommandRegister.addUserMaps(MultiRequester.name[0], 
     422                        [[site.map, site.name, site.bang, site.args]]); 
     423                } 
     424            }); 
     425        } 
     426 
     427        SITEINFO.forEach(function(site) { 
     428            if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
     429            $U.extend(mergedSiteinfo[site.name], site); 
     430            if (site.map) { 
     431                CommandRegister.addUserMaps(MultiRequester.name[0], 
     432                    [[site.map, site.name, site.bang, site.args]]); 
     433            } 
     434        }); 
    411435 
    412436        if (useWedata) { 
     
    418442            }); 
    419443        } 
    420  
    421         if (liberator.globalVariables.multi_requester_siteinfo) { 
    422             liberator.globalVariables.multi_requester_siteinfo.forEach(function(site) { 
    423                 if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
    424                 $U.extend(mergedSiteinfo[site.name], site); 
    425             }); 
    426         } 
    427  
    428         SITEINFO.forEach(function(site) { 
    429             if (!mergedSiteinfo[site.name]) mergedSiteinfo[site.name] = {}; 
    430             $U.extend(mergedSiteinfo[site.name], site); 
    431         }); 
    432444 
    433445        return $U.A(mergedSiteinfo); 
     
    454466// main controller {{{ 
    455467var MultiRequester = { 
    456     doProcess: false, 
    457     requestCount: 0, 
    458     echoList: [], 
    459468    name: DataAccess.getCommand(), 
    460469    description: 'request, and display to the buffer', 
    461     cmdAction: function(args) { 
     470    doProcess: false, 
     471    requestNames: '', 
     472    requestCount: 0, 
     473    echoHash: {}, 
     474    cmdAction: function(args) { //{{{ 
    462475 
    463476        if (MultiRequester.doProcess) return; 
     
    471484 
    472485        MultiRequester.doProcess = true; 
     486        MultiRequester.requestNames = parsedArgs.names; 
    473487        MultiRequester.requestCount = 0; 
    474         MultiRequester.echoList = []; 
     488        MultiRequester.echoHash = {}; 
    475489        var siteinfo = parsedArgs.siteinfo; 
    476490        for (let i = 0, len = parsedArgs.count; i < len; i++) { 
     
    554568        }); 
    555569        return ret; 
    556     }, 
    557     extractLink: function(res, extractLink) { 
     570    },//}}} 
     571    extractLink: function(res, extractLink) { //{{{ 
    558572 
    559573        var el = res.getHTMLDocument(extractLink); 
     
    561575        var a = el.firstChild; 
    562576        var url = $U.pathToURL((a.href || a.action || a.value)); 
    563         var req = new Request(url, null, $U.extend(res.request.options, {extractLink: true})); 
     577        var req = new Request(url, null, $U.extend(res.req.options, {extractLink: true})); 
    564578        req.addEventListener('onException', $U.bind(this, this.onException)); 
    565579        req.addEventListener('onSuccess', $U.bind(this, this.onSuccess)); 
     
    569583        MultiRequester.doProcess = true; 
    570584 
    571     }, 
    572     onSuccess: function(res) { 
     585    },//}}} 
     586    onSuccess: function(res) { //{{{ 
    573587 
    574588        if (!MultiRequester.doProcess) { 
     
    577591        } 
    578592 
    579         $U.log('success!!!' + res.request.url); 
     593        $U.log('success!!!:' + res.req.url); 
    580594        MultiRequester.requestCount--; 
    581595        if (MultiRequester.requestCount == 0) { 
     
    589603            if (!res.isSuccess || res.responseText == '') throw 'response is fail or null'; 
    590604 
    591             url = res.request.url; 
     605            url = res.req.url; 
    592606            escapedUrl = util.escapeHTML(url); 
    593             xpath = res.request.options.siteinfo.xpath; 
    594             extractLink = res.request.options.siteinfo.extractLink; 
    595  
    596             if (extractLink && !res.request.options.extractLink) { 
     607            xpath = res.req.options.siteinfo.xpath; 
     608            extractLink = res.req.options.siteinfo.extractLink; 
     609 
     610            if (extractLink && !res.req.options.extractLink) { 
    597611                this.extractLink(res, extractLink); 
    598612                return; 
     
    603617 
    604618            html = '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
    605                    (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, 
    606                             function(all) all.toLowerCase()); 
    607  
    608             MultiRequester.echoList.push(html); 
     619                   (new XMLSerializer()).serializeToString(doc) 
     620                            .replace(/<[^>]+>/g, function(all) all.toLowerCase()) 
     621                            .replace(/<!--[^>]*-->/g, ''); 
     622 
     623            MultiRequester.echoHash[res.req.options.siteinfo.name] = html; 
    609624 
    610625        } catch (e) { 
    611626            $U.log('error!!: ' + e); 
    612             MultiRequester.echoList.push('<span style="color: red;">error!!:' + e + '</span>'); 
     627            MultiRequester.echoHash[res.req.options.siteinfo.name] = 
     628                            '<span style="color: red;">error!!: ' + e + '</span>'; 
    613629        } 
    614630 
    615631        if (MultiRequester.requestCount == 0) { 
    616             let html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
    617                         MultiRequester.echoList.join('') + 
    618                         '</div>'; 
    619             try { $U.echo(new XMLList(html)); } catch (e) { $U.echo(html); } 
     632            let echoList = []; 
     633            MultiRequester.requestNames.split(',').forEach(function(name) { 
     634                echoList.push(MultiRequester.echoHash[name]) 
     635            }); 
     636            html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
     637                   echoList.join('') + 
     638                   '</div>'; 
     639            try { $U.echo(new XMLList(html)); } catch (e) { $U.log(e); $U.echo(html); } 
    620640        } 
    621641 
     
    628648        MultiRequester.doProcess = false; 
    629649        $U.echoerr('exception!!: ' + e); 
    630     } 
     650    }//}}} 
    631651}; 
    632652//}}}