Changeset 9842

Show
Ignore:
Timestamp:
04/19/08 08:21:28 (5 years ago)
Author:
drry
Message:

lang/javascript/userscripts/autofocus.user.js:

  • fixed a regexp.
  • et cetera.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userscripts/autofocus.user.js

    r7505 r9842  
    1111// 
    1212 
    13 var CACHE_EXPIRE = 24 * 60 * 60 * 1000 
    14 var SITEINFO_URLS = ['http://wiki.livedoor.jp/samurai20000/d/']; 
    15 var SITEINFO = [ 
     13const CACHE_EXPIRE = 24 * 60 * 60 * 1000; 
     14const SITEINFO_URLS = ['http://wiki.livedoor.jp/samurai20000/d/']; 
     15const SITEINFO = [ 
    1616    { 
    17         url:   'http://reader.livedoor.com/', 
     17        url:   'http://reader\\.livedoor\\.com/', 
    1818        focus: '//div[@id="login"]/h3/a' 
    1919    } 
    20     /* templete  
     20    /* templete 
    2121    { 
    22         url:   '', 
    23         focus: '' 
     22        url:   '', 
     23        focus: '' 
    2424    } 
    2525    */ 
     
    3030    focus_element.focus(); 
    3131    log('focus!'); 
    32 } 
     32}; 
    3333 
    3434var launchAutoFocus = function(list) { 
     
    4545        } 
    4646    } 
    47 } 
     47}; 
    4848 
    4949 
     
    5353        if(!cacheInfo[i] || cacheInfo[i].expire < new Date()) { 
    5454            var opt = { 
    55                 method  : 'get', 
    56                 url     : i, 
    57                 onload  : function(res) {getCacheCallback(res, i)}, 
    58                 onerror : function(res) {getCacheErrorCallback(i)} 
     55                method  : 'get', 
     56                url     : i, 
     57                onload  : function(res) {getCacheCallback(res, i)}, 
     58                onerror : function(res) {getCacheErrorCallback(i)} 
    5959            }; 
    6060            GM_xmlhttpRequest(opt); 
     
    6363            launchAutoFocus(cacheInfo[i].info); 
    6464        } 
    65     }) 
    66 } 
     65    }); 
     66}; 
    6767 
    6868// utility SITE_INFO 
     
    7272    var re = /^([^:]+?):(.*)$/; 
    7373    var info = {}; 
    74     for (var i = 0; i < lines.length; i++) { 
    75         if (re.test(lines[i])) { 
     74    lines.forEach(function(line) { 
     75        if (re.test(line)) { 
    7676            info[RegExp.$1] = RegExp.$2.replace(/^\s+/, ''); 
    7777        } 
    78     } 
     78    }); 
    7979 
    8080    var isValid = function(info) { 
    81         var property = ['url', 'focus']; 
    82         for (var i = 0; i < property.length; i++) { 
    83             if (!info[property[i]]) { 
    84                 return false; 
    85             } 
    86         } 
    87         return true; 
    88     } 
     81        return !['url', 'focus'].some(function(property) { 
     82            return !info.hasOwnProperty(property); 
     83        }); 
     84    }; 
    8985 
    9086    return isValid(info) ? info : null; 
     
    9692 
    9793function getCache() { 
    98     return eval(GM_getValue('cacheInfo')) || {}; 
     94    return eval(GM_getValue('cacheInfo', '({})')); 
    9995} 
    10096 
     
    104100    } 
    105101 
    106     var info      = []; 
    107     var matched   = false; 
     102    var info      = []; 
     103    var matched   = false; 
    108104    var hdoc      = createHTMLDocumentByString(res.responseText); 
    109105    var pre_areas = getElementsByXPath('//pre', hdoc) || []; 
     
    111107    pre_areas.forEach(function(pre_area) { 
    112108        var d = parseInfo(pre_area.innerHTML); 
    113         if (d) { 
    114             info.push(d); 
    115             if (!matched && location.href.match(d.url)) { 
    116                 matched = d; 
    117             } 
     109        if (!d) return; 
     110        info.push(d); 
     111        if (!matched && location.href.match(d.url)) { 
     112            matched = d; 
    118113        } 
    119114    }); 
     
    121116    if (info.length > 0) { 
    122117        cacheInfo[url] = { 
    123             urls        : url, 
    124             expire      : new Date(new Date().getTime() + CACHE_EXPIRE), 
    125             info        : info 
     118            urls   : url, 
     119            expire : new Date(new Date().getTime() + CACHE_EXPIRE), 
     120            info   : info 
    126121        } 
    127122        GM_setValue('cacheInfo', cacheInfo.toSource()); 
     
    143138 
    144139function createHTMLDocumentByString(str) { 
    145     var html = str.replace(/<!DOCTYPE\s[^>]*?>|<html[^>]*?>|<\/html\s*>.*/g, ''); 
     140    var html     = str.replace(/<!DOCTYPE\s[^>]*>|<html(?:\s[^>]*)?>|<\/html\s*>.*/g, ''); 
    146141    var htmlDoc  = document.implementation.createDocument(null, 'html', null); 
    147142    var fragment = createDocumentFragmentByString(html); 
     
    158153function getFocusElementByXpath(xpath) { 
    159154    var result = document.evaluate(xpath, document, null, 
    160                                    XPathResult.FIRST_ORDERED_NODE_TYPE, null); 
     155                                   XPathResult.FIRST_ORDERED_NODE_TYPE, null); 
    161156    return result.singleNodeValue ? result.singleNodeValue: null; 
    162157} 
     
    168163    var data = []; 
    169164    for (var i = 0; i < nodesSnapshot.snapshotLength; i++) { 
    170         data.push(nodesSnapshot.snapshotItem(i)); 
     165        data.push(nodesSnapshot.snapshotItem(i)); 
    171166    } 
    172167    return (data.length >= 1) ? data : null;