Show
Ignore:
Timestamp:
12/01/08 22:39:23 (6 weeks ago)
Author:
suVene
Message:

1度目のレスポンスから次のリクエストを取り出す仕組み

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

Legend:

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

    r25491 r25643  
    55 * @description-ja   リクエストの結果をバッファに出力する。 
    66 * @author           suVene suvene@zeromemory.info 
    7  * @version          0.3.3 
     7 * @version          0.4.0 
    88 * @minVersion       1.2 
    99 * @maxVersion       1.2 
     
    1111 * ==/VimperatorPlugin== 
    1212 * 
     13 * HEAD COMMENT {{{ 
    1314 * Usage: 
    1415 *   command[!] subcommand [ANY_TEXT] 
     
    3536 *   liberator.globalVariables.multi_requester_siteinfo = [ 
    3637 *       { 
    37  *           name:        'ex',                             // required 
    38  *           description: 'example',                        // required 
    39  *           url:         'http://example.com/?%s',         // required, %s <-- replace string 
    40  *           xpath:       '//*',                            // optional(default all) 
    41  *           srcEncode:   'SHIFT_JIS',                      // optional(default UTF-8) 
    42  *           urlEncode:   'SHIFT_JIS',                      // optional(default srcEncode) 
    43  *           ignoreTags:  'img'                             // optional(default script), syntax 'tag1,tag2,……' 
     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' 
    4446 *       }, 
    4547 *   ]; 
     
    6264 * TODO: 
    6365 *    - wedata local cache. 
     66 *  }}} 
    6467 */ 
    6568(function() { 
    6669 
     70// global variables {{{ 
    6771var DEFAULT_COMMAND = ['mr']; 
    6872var SITEINFO = [ 
     
    8286    }, 
    8387]; 
    84  
    8588var mergedSiteinfo = {}; 
    86  
    87 // utilities 
     89//}}} 
     90 
     91// utility class {{{ 
    8892var $U = { 
    8993    log: function(msg, level) { 
    9094        liberator.log(msg, (level || 8)); 
    91     }, 
    92     debug: function(msg) { 
    93         this.log(msg, 8); 
    94         liberator.echo(msg); 
    9595    }, 
    9696    echo: function(msg, flg) { 
     
    121121        ignoreTags = '(?:' + ignoreTags.join('|') + ')'; 
    122122        return str.replace(new RegExp('<' + ignoreTags + '(?:[ \\t\\n\\r][^>]*|/)?>([\\S\\s]*?)<\/' + ignoreTags + '[ \\t\\r\\n]*>', 'ig'), ''); 
    123     }, 
    124     stripScripts: function(str) { 
    125         return this.stripScripts(str, 'script'); 
    126123    }, 
    127124    eval: function(text) { 
     
    147144    }, 
    148145    getSelectedString: function() { 
    149          var sel = (new XPCNativeWrapper(window.content.window)).getSelection(); 
    150          return sel.toString(); 
     146         return (new XPCNativeWrapper(window.content.window)).getSelection().toString(); 
     147    }, 
     148    pathToURL: function(path) { 
     149        if (path.match(/^http:\/\//)) return path; 
     150        var link = document.createElement('a'); 
     151        link.href= path; 
     152        return link.href; 
    151153    } 
    152154}; 
    153  
    154 // vimperator plugin command register 
     155//}}} 
     156 
     157// vimperator plugin command register {{{ 
    155158var CommandRegister = { 
    156159    register: function(cmdClass, siteinfo) { 
     
    207210    } 
    208211}; 
    209  
    210  
    211 // like the Prototype JavaScript framework 
     212//}}} 
     213 
     214// Request and Response class. like the Prototype JavaScript framework {{{ 
    212215var Request = function() { 
    213216    this.initialize.apply(this, arguments); 
     
    384387    } 
    385388}; 
    386  
    387 // initial data access. 
     389//}}} 
     390 
     391// initial data access class {{{ 
    388392var DataAccess = { 
    389393    getCommand: function() { 
     
    444448    } 
    445449}; 
    446  
    447 // main controller. 
     450//}}} 
     451 
     452// main controller {{{ 
    448453var MultiRequester = { 
    449454    doProcess: false, 
     
    500505        } 
    501506 
    502         $U.echo('Loading ' + parsedArgs.names + ' ...', commandline.FORCE_SINGLELINE); 
     507        if (MultiRequester.requestCount) { 
     508            $U.echo('Loading ' + parsedArgs.names + ' ...', commandline.FORCE_SINGLELINE); 
     509        } else { 
     510            MultiRequester.doProcess = false; 
     511        } 
    503512    }, 
    504513    // return {names: '', str: '', count: 0, siteinfo: [{}]} 
     
    540549        return ret; 
    541550    }, 
     551    extractLink: function(res, extractLink) { 
     552 
     553        var el = res.getHTMLDocument(extractLink); 
     554        if (!el) throw 'extract link failed.: extractLink -> ' + extractLink; 
     555        var a = el.firstChild; 
     556        var url = $U.pathToURL((a.href || a.action || a.value)); 
     557        var req = new Request(url, null, $U.extend(res.request.options, {extractLink: true})); 
     558        req.addEventListener('onException', $U.bind(this, this.onException)); 
     559        req.addEventListener('onSuccess', $U.bind(this, this.onSuccess)); 
     560        req.addEventListener('onFailure', $U.bind(this, this.onFailure)); 
     561        req.get(); 
     562        MultiRequester.requestCount++; 
     563        MultiRequester.doProcess = true; 
     564 
     565    }, 
    542566    onSuccess: function(res) { 
    543567 
     
    548572 
    549573        var url, escapedUrl, xpath, doc, html; 
     574 
    550575        $U.log('success!!!' + res.request.url); 
    551576        MultiRequester.requestCount--; 
     577        if (MultiRequester.requestCount == 0) { 
     578            MultiRequester.doProcess = false; 
     579        } 
    552580 
    553581        try { 
     
    558586            escapedUrl = liberator.util.escapeHTML(url); 
    559587            xpath = res.request.options.siteinfo.xpath; 
     588            extractLink = res.request.options.siteinfo.extractLink; 
     589 
     590            if (extractLink && !res.request.options.extractLink) { 
     591                this.extractLink(res, extractLink); 
     592                return; 
     593            } 
     594 
    560595            doc = res.getHTMLDocument(xpath); 
    561596            if (!doc) throw 'XPath result is undefined or null.: XPath -> ' + xpath; 
    562597 
    563             html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
    564                    '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
    565                    (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, function(all) all.toLowerCase()) + 
    566                    '</div>'; 
     598            html = '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
     599                   (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, 
     600                            function(all) all.toLowerCase()); 
    567601 
    568602            MultiRequester.echoList.push(html); 
    569  
    570             if (MultiRequester.requestCount == 0) { 
    571                 MultiRequester.doProcess = false; 
    572                 let html = MultiRequester.echoList.join(''); 
    573                 try { $U.echo(new XMLList(html)); } catch (e) { $U.echo(html); } 
    574             } 
    575603 
    576604        } catch (e) { 
    577605            $U.log('error!!: ' + e); 
    578             MultiRequester.echoList.push('error!!:' + e); 
    579         } 
     606            MultiRequester.echoList.push('<span style="color: red;">error!!:' + e + '</span>'); 
     607        } 
     608 
     609        if (MultiRequester.requestCount == 0) { 
     610            let html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
     611                        MultiRequester.echoList.join('') + 
     612                        '</div>'; 
     613            try { $U.echo(new XMLList(html)); } catch (e) { $U.echo(html); } 
     614        } 
     615 
    580616    }, 
    581617    onFailure: function(res) { 
     
    588624    } 
    589625}; 
    590  
     626//}}} 
     627 
     628// boot strap {{{ 
    591629CommandRegister.register(MultiRequester, DataAccess.getSiteInfo()); 
    592630if (liberator.globalVariables.multi_requester_mappings) { 
    593631    CommandRegister.addUserMaps(MultiRequester.name[0], liberator.globalVariables.multi_requester_mappings); 
    594632} 
     633//}}} 
    595634 
    596635return MultiRequester; 
  • lang/javascript/vimperator-plugins/trunk/multi_requester.js

    r25491 r25643  
    55 * @description-ja   リクエストの結果をバッファに出力する。 
    66 * @author           suVene suvene@zeromemory.info 
    7  * @version          0.3.3 
     7 * @version          0.4.0 
    88 * @minVersion       2.0pre 
    99 * @maxVersion       2.0pre 
     
    1111 * ==/VimperatorPlugin== 
    1212 * 
     13 * HEAD COMMENT {{{ 
    1314 * Usage: 
    1415 *   command[!] subcommand [ANY_TEXT] 
     
    3536 *   liberator.globalVariables.multi_requester_siteinfo = [ 
    3637 *       { 
    37  *           name:        'ex',                             // required 
    38  *           description: 'example',                        // required 
    39  *           url:         'http://example.com/?%s',         // required, %s <-- replace string 
    40  *           xpath:       '//*',                            // optional(default all) 
    41  *           srcEncode:   'SHIFT_JIS',                      // optional(default UTF-8) 
    42  *           urlEncode:   'SHIFT_JIS',                      // optional(default srcEncode) 
    43  *           ignoreTags:  'img'                             // optional(default script), syntax 'tag1,tag2,……' 
     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' 
    4446 *       }, 
    4547 *   ]; 
     
    6264 * TODO: 
    6365 *    - wedata local cache. 
     66 *  }}} 
    6467 */ 
    6568(function() { 
    6669 
     70// global variables {{{ 
    6771var DEFAULT_COMMAND = ['mr']; 
    6872var SITEINFO = [ 
     
    8286    }, 
    8387]; 
    84  
    8588var mergedSiteinfo = {}; 
    86  
    87 // utilities 
     89//}}} 
     90 
     91// utility class {{{ 
    8892var $U = { 
    8993    log: function(msg, level) { 
    90         liberator.log(msg, (level || 8)); 
    91     }, 
    92     debug: function(msg) { 
    93         this.log(msg, 8); 
    94         liberator.echo(msg); 
     94        liberator.log(msg, (level || 0)); 
    9595    }, 
    9696    echo: function(msg, flg) { 
     
    121121        ignoreTags = '(?:' + ignoreTags.join('|') + ')'; 
    122122        return str.replace(new RegExp('<' + ignoreTags + '(?:[ \\t\\n\\r][^>]*|/)?>([\\S\\s]*?)<\/' + ignoreTags + '[ \\t\\r\\n]*>', 'ig'), ''); 
    123     }, 
    124     stripScripts: function(str) { 
    125         return this.stripScripts(str, 'script'); 
    126123    }, 
    127124    eval: function(text) { 
     
    147144    }, 
    148145    getSelectedString: function() { 
    149          var sel = (new XPCNativeWrapper(window.content.window)).getSelection(); 
    150          return sel.toString(); 
     146         return (new XPCNativeWrapper(window.content.window)).getSelection().toString(); 
     147    }, 
     148    pathToURL: function(path) { 
     149        if (path.match(/^http:\/\//)) return path; 
     150        var link = document.createElement('a'); 
     151        link.href= path; 
     152        return link.href; 
    151153    } 
    152154}; 
    153  
    154 // vimperator plugin command register 
     155//}}} 
     156 
     157// vimperator plugin command register {{{ 
    155158var CommandRegister = { 
    156159    register: function(cmdClass, siteinfo) { 
     
    209212    } 
    210213}; 
    211  
    212  
    213 // like the Prototype JavaScript framework 
     214//}}} 
     215 
     216// Request and Response class. like the Prototype JavaScript framework {{{ 
    214217var Request = function() { 
    215218    this.initialize.apply(this, arguments); 
     
    386389    } 
    387390}; 
    388  
    389 // initial data access. 
     391//}}} 
     392 
     393// initial data access class {{{ 
    390394var DataAccess = { 
    391395    getCommand: function() { 
     
    446450    } 
    447451}; 
    448  
    449 // main controller. 
     452//}}} 
     453 
     454// main controller {{{ 
    450455var MultiRequester = { 
    451456    doProcess: false, 
     
    506511        } 
    507512 
    508         $U.echo('Loading ' + parsedArgs.names + ' ...', commandline.FORCE_SINGLELINE); 
     513        if (MultiRequester.requestCount) { 
     514            $U.echo('Loading ' + parsedArgs.names + ' ...', commandline.FORCE_SINGLELINE); 
     515        } else { 
     516            MultiRequester.doProcess = false; 
     517        } 
    509518    }, 
    510519    // return {names: '', str: '', count: 0, siteinfo: [{}]} 
     
    546555        return ret; 
    547556    }, 
     557    extractLink: function(res, extractLink) { 
     558 
     559        var el = res.getHTMLDocument(extractLink); 
     560        if (!el) throw 'extract link failed.: extractLink -> ' + extractLink; 
     561        var a = el.firstChild; 
     562        var url = $U.pathToURL((a.href || a.action || a.value)); 
     563        var req = new Request(url, null, $U.extend(res.request.options, {extractLink: true})); 
     564        req.addEventListener('onException', $U.bind(this, this.onException)); 
     565        req.addEventListener('onSuccess', $U.bind(this, this.onSuccess)); 
     566        req.addEventListener('onFailure', $U.bind(this, this.onFailure)); 
     567        req.get(); 
     568        MultiRequester.requestCount++; 
     569        MultiRequester.doProcess = true; 
     570 
     571    }, 
    548572    onSuccess: function(res) { 
    549573 
     
    554578 
    555579        var url, escapedUrl, xpath, doc, html; 
     580 
    556581        $U.log('success!!!' + res.request.url); 
    557582        MultiRequester.requestCount--; 
     583        if (MultiRequester.requestCount == 0) { 
     584            MultiRequester.doProcess = false; 
     585        } 
    558586 
    559587        try { 
     
    564592            escapedUrl = util.escapeHTML(url); 
    565593            xpath = res.request.options.siteinfo.xpath; 
     594            extractLink = res.request.options.siteinfo.extractLink; 
     595 
     596            if (extractLink && !res.request.options.extractLink) { 
     597                this.extractLink(res, extractLink); 
     598                return; 
     599            } 
     600 
    566601            doc = res.getHTMLDocument(xpath); 
    567602            if (!doc) throw 'XPath result is undefined or null.: XPath -> ' + xpath; 
    568603 
    569             html = '<div style="white-space:normal;"><base href="' + escapedUrl + '"/>' + 
    570                    '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
    571                    (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, function(all) all.toLowerCase()) + 
    572                    '</div>'; 
     604            html = '<a href="' + escapedUrl + '" class="hl-Title" target="_self">' + escapedUrl + '</a>' + 
     605                   (new XMLSerializer()).serializeToString(doc).replace(/<[^>]+>/g, 
     606                            function(all) all.toLowerCase()); 
    573607 
    574608            MultiRequester.echoList.push(html); 
    575  
    576             if (MultiRequester.requestCount == 0) { 
    577                 MultiRequester.doProcess = false; 
    578                 let html = MultiRequester.echoList.join(''); 
    579                 try { $U.echo(new XMLList(html)); } catch (e) { $U.echo(html); } 
    580             } 
    581609 
    582610        } catch (e) { 
    583611            $U.log('error!!: ' + e); 
    584             MultiRequester.echoList.push('error!!:' + e); 
    585         } 
     612            MultiRequester.echoList.push('<span style="color: red;">error!!:' + e + '</span>'); 
     613        } 
     614 
     615        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); } 
     620        } 
     621 
    586622    }, 
    587623    onFailure: function(res) { 
     
    594630    } 
    595631}; 
    596  
     632//}}} 
     633 
     634// boot strap {{{ 
    597635CommandRegister.register(MultiRequester, DataAccess.getSiteInfo()); 
    598636if (liberator.globalVariables.multi_requester_mappings) { 
    599637    CommandRegister.addUserMaps(MultiRequester.name[0], liberator.globalVariables.multi_requester_mappings); 
    600638} 
     639//}}} 
    601640 
    602641return MultiRequester;