Changeset 25324

Show
Ignore:
Timestamp:
11/29/08 12:21:09 (6 weeks ago)
Author:
janus_wel
Message:

refactoring
change to not use window.eval

Files:
1 modified

Legend:

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

    r22947 r25324  
    44 * @description   append anchors to texts look like url. 
    55 * @author        SAKAI, Kazuaki 
    6  * @version       0.02 
     6 * @version       0.03 
    77 * @minVersion    2.0pre 
    88 * @maxVersion    2.0pre 
     
    1212(function(){ 
    1313 
    14   if (window.eval(liberator.globalVariables.auto_append_anchor || 'false')) { 
     14  // settings --- 
     15  // "ACEILMPRSTXY" is result of below code. 
     16  //   Array.prototype.uniq = function() this.reduceRight( function (a, b) (a[0] === b || a.unshift(b), a), []); 
     17  //   [ 'TITLE', 'STYLE', 'SCRIPT', 'TEXTAREA', 'XMP', 'A', ].join('').split('').sort().uniq().join(''); 
     18  const xpathQueryPlainText = '/descendant::*[not(contains(" TITLE STYLE SCRIPT TEXTAREA XMP A ", concat(" ", translate(local-name(), "aceilmprstxy", "ACEILMPRSTXY"), " ")))]/child::text()'; 
     19  const regexpLikeURL = new RegExp("h?(ttps?):/+([a-zA-Z0-9][-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+[-_~*(a-zA-Z0-9;/?@&=+$%#])"); 
     20 
     21  // process global variable 
     22  if (stringToBoolean(liberator.globalVariables.auto_append_anchor, 'false')) { 
    1523    let originalHintsShow = liberator.modules.hints.show; 
    1624    hints.show = function () { 
     
    2028  } 
    2129 
     30  // register command 
    2231  liberator.modules.commands.addUserCommand(['anc'], 'append anchors to texts look like url', 
    2332    function(arg, special) { 
    24       var doc = window.content.document; 
    25       var nodes = liberator.modules.buffer.evaluateXPath( 
    26         '/descendant::*[not(contains(" TITLE STYLE SCRIPT TEXTAREA XMP A ", concat(" ", translate(local-name(), "aceilmprstxy", "ACEILMPRSTXY"), " ")))]/child::text()' 
    27       ); 
    28       var regex = new RegExp("h?(ttps?):/+([a-zA-Z0-9][-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+[-_~*(a-zA-Z0-9;/?@&=+$%#])"); 
     33      const doc = window.content.document; 
     34      const range = doc.createRange(); 
    2935 
    30       var range = doc.createRange(); 
    31       var last; 
    32       var href; 
    33       for (let i = 0, l = nodes.snapshotLength; i < l; i++) { 
    34         let node = nodes.snapshotItem(i); 
    35         range.selectNode(node); 
    36         while (node && (last = range.toString().search(regex)) > -1) { 
    37           range.setStart(node, last); 
    38           range.setEnd(node, last + RegExp.lastMatch.length); 
    39           href = 'h' + RegExp.$1 + '://' + RegExp.$2; 
     36      let nodes = liberator.modules.buffer.evaluateXPath(xpathQueryPlainText); 
     37      for (let node in nodes) { 
     38        while (node) { 
     39          range.selectNode(node) 
     40 
     41          // search string like URL 
     42          let start = range.toString().search(regexpLikeURL); 
     43          // go to next node when there is nothing look like URL in current node 
     44          if (!(start > -1)) break; 
     45 
     46          // build URL 
     47          let href = 'h' + RegExp.$1 + '://' + RegExp.$2; 
     48 
     49          // reset range 
     50          range.setStart(node, start); 
     51          range.setEnd(node, start + RegExp.lastMatch.length); 
     52 
     53          // build anchor element 
    4054          let anchor = doc.createElement('a'); 
    41           range.insertNode(anchor); 
    4255          anchor.setAttribute('href', href); 
    4356          range.surroundContents(anchor); 
     57 
     58          // insert 
     59          range.insertNode(anchor); 
     60 
     61          // iterate 
    4462          node = node.nextSibling.nextSibling; 
    45           range.selectNode(node); 
    4663        } 
    4764      } 
     
    4966    },{} 
    5067  ); 
     68 
     69  // stuff function 
     70  function stringToBoolean(str, defaultValue) { 
     71    return !str                          ? (defaultValue ? true : false) 
     72         : str.toLowerCase() === 'false' ? false 
     73         : /^\d+$/.test(str)             ? (parseInt(str) ? true : false) 
     74         :                                 true; 
     75  } 
     76 
    5177})();