Changeset 25643 for lang/javascript/vimperator-plugins
- Timestamp:
- 12/01/08 22:39:23 (6 weeks ago)
- Location:
- lang/javascript/vimperator-plugins
- Files:
-
- 2 modified
-
branches/1.2/multi_requester.js (modified) (15 diffs)
-
trunk/multi_requester.js (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/vimperator-plugins/branches/1.2/multi_requester.js
r25491 r25643 5 5 * @description-ja リクエストの結果をバッファに出力する。 6 6 * @author suVene suvene@zeromemory.info 7 * @version 0. 3.37 * @version 0.4.0 8 8 * @minVersion 1.2 9 9 * @maxVersion 1.2 … … 11 11 * ==/VimperatorPlugin== 12 12 * 13 * HEAD COMMENT {{{ 13 14 * Usage: 14 15 * command[!] subcommand [ANY_TEXT] … … 35 36 * liberator.globalVariables.multi_requester_siteinfo = [ 36 37 * { 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' 44 46 * }, 45 47 * ]; … … 62 64 * TODO: 63 65 * - wedata local cache. 66 * }}} 64 67 */ 65 68 (function() { 66 69 70 // global variables {{{ 67 71 var DEFAULT_COMMAND = ['mr']; 68 72 var SITEINFO = [ … … 82 86 }, 83 87 ]; 84 85 88 var mergedSiteinfo = {}; 86 87 // utilities 89 //}}} 90 91 // utility class {{{ 88 92 var $U = { 89 93 log: function(msg, level) { 90 94 liberator.log(msg, (level || 8)); 91 },92 debug: function(msg) {93 this.log(msg, 8);94 liberator.echo(msg);95 95 }, 96 96 echo: function(msg, flg) { … … 121 121 ignoreTags = '(?:' + ignoreTags.join('|') + ')'; 122 122 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');126 123 }, 127 124 eval: function(text) { … … 147 144 }, 148 145 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; 151 153 } 152 154 }; 153 154 // vimperator plugin command register 155 //}}} 156 157 // vimperator plugin command register {{{ 155 158 var CommandRegister = { 156 159 register: function(cmdClass, siteinfo) { … … 207 210 } 208 211 }; 209 210 211 // like the Prototype JavaScript framework212 //}}} 213 214 // Request and Response class. like the Prototype JavaScript framework {{{ 212 215 var Request = function() { 213 216 this.initialize.apply(this, arguments); … … 384 387 } 385 388 }; 386 387 // initial data access. 389 //}}} 390 391 // initial data access class {{{ 388 392 var DataAccess = { 389 393 getCommand: function() { … … 444 448 } 445 449 }; 446 447 // main controller. 450 //}}} 451 452 // main controller {{{ 448 453 var MultiRequester = { 449 454 doProcess: false, … … 500 505 } 501 506 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 } 503 512 }, 504 513 // return {names: '', str: '', count: 0, siteinfo: [{}]} … … 540 549 return ret; 541 550 }, 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 }, 542 566 onSuccess: function(res) { 543 567 … … 548 572 549 573 var url, escapedUrl, xpath, doc, html; 574 550 575 $U.log('success!!!' + res.request.url); 551 576 MultiRequester.requestCount--; 577 if (MultiRequester.requestCount == 0) { 578 MultiRequester.doProcess = false; 579 } 552 580 553 581 try { … … 558 586 escapedUrl = liberator.util.escapeHTML(url); 559 587 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 560 595 doc = res.getHTMLDocument(xpath); 561 596 if (!doc) throw 'XPath result is undefined or null.: XPath -> ' + xpath; 562 597 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()); 567 601 568 602 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 }575 603 576 604 } catch (e) { 577 605 $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 580 616 }, 581 617 onFailure: function(res) { … … 588 624 } 589 625 }; 590 626 //}}} 627 628 // boot strap {{{ 591 629 CommandRegister.register(MultiRequester, DataAccess.getSiteInfo()); 592 630 if (liberator.globalVariables.multi_requester_mappings) { 593 631 CommandRegister.addUserMaps(MultiRequester.name[0], liberator.globalVariables.multi_requester_mappings); 594 632 } 633 //}}} 595 634 596 635 return MultiRequester; -
lang/javascript/vimperator-plugins/trunk/multi_requester.js
r25491 r25643 5 5 * @description-ja リクエストの結果をバッファに出力する。 6 6 * @author suVene suvene@zeromemory.info 7 * @version 0. 3.37 * @version 0.4.0 8 8 * @minVersion 2.0pre 9 9 * @maxVersion 2.0pre … … 11 11 * ==/VimperatorPlugin== 12 12 * 13 * HEAD COMMENT {{{ 13 14 * Usage: 14 15 * command[!] subcommand [ANY_TEXT] … … 35 36 * liberator.globalVariables.multi_requester_siteinfo = [ 36 37 * { 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' 44 46 * }, 45 47 * ]; … … 62 64 * TODO: 63 65 * - wedata local cache. 66 * }}} 64 67 */ 65 68 (function() { 66 69 70 // global variables {{{ 67 71 var DEFAULT_COMMAND = ['mr']; 68 72 var SITEINFO = [ … … 82 86 }, 83 87 ]; 84 85 88 var mergedSiteinfo = {}; 86 87 // utilities 89 //}}} 90 91 // utility class {{{ 88 92 var $U = { 89 93 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)); 95 95 }, 96 96 echo: function(msg, flg) { … … 121 121 ignoreTags = '(?:' + ignoreTags.join('|') + ')'; 122 122 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');126 123 }, 127 124 eval: function(text) { … … 147 144 }, 148 145 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; 151 153 } 152 154 }; 153 154 // vimperator plugin command register 155 //}}} 156 157 // vimperator plugin command register {{{ 155 158 var CommandRegister = { 156 159 register: function(cmdClass, siteinfo) { … … 209 212 } 210 213 }; 211 212 213 // like the Prototype JavaScript framework214 //}}} 215 216 // Request and Response class. like the Prototype JavaScript framework {{{ 214 217 var Request = function() { 215 218 this.initialize.apply(this, arguments); … … 386 389 } 387 390 }; 388 389 // initial data access. 391 //}}} 392 393 // initial data access class {{{ 390 394 var DataAccess = { 391 395 getCommand: function() { … … 446 450 } 447 451 }; 448 449 // main controller. 452 //}}} 453 454 // main controller {{{ 450 455 var MultiRequester = { 451 456 doProcess: false, … … 506 511 } 507 512 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 } 509 518 }, 510 519 // return {names: '', str: '', count: 0, siteinfo: [{}]} … … 546 555 return ret; 547 556 }, 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 }, 548 572 onSuccess: function(res) { 549 573 … … 554 578 555 579 var url, escapedUrl, xpath, doc, html; 580 556 581 $U.log('success!!!' + res.request.url); 557 582 MultiRequester.requestCount--; 583 if (MultiRequester.requestCount == 0) { 584 MultiRequester.doProcess = false; 585 } 558 586 559 587 try { … … 564 592 escapedUrl = util.escapeHTML(url); 565 593 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 566 601 doc = res.getHTMLDocument(xpath); 567 602 if (!doc) throw 'XPath result is undefined or null.: XPath -> ' + xpath; 568 603 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()); 573 607 574 608 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 }581 609 582 610 } catch (e) { 583 611 $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 586 622 }, 587 623 onFailure: function(res) { … … 594 630 } 595 631 }; 596 632 //}}} 633 634 // boot strap {{{ 597 635 CommandRegister.register(MultiRequester, DataAccess.getSiteInfo()); 598 636 if (liberator.globalVariables.multi_requester_mappings) { 599 637 CommandRegister.addUserMaps(MultiRequester.name[0], liberator.globalVariables.multi_requester_mappings); 600 638 } 639 //}}} 601 640 602 641 return MultiRequester;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)