Changeset 9842
- Timestamp:
- 04/19/08 08:21:28 (5 years ago)
- Files:
-
- 1 modified
-
lang/javascript/userscripts/autofocus.user.js (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/userscripts/autofocus.user.js
r7505 r9842 11 11 // 12 12 13 var CACHE_EXPIRE = 24 * 60 * 60 * 1000 14 varSITEINFO_URLS = ['http://wiki.livedoor.jp/samurai20000/d/'];15 varSITEINFO = [13 const CACHE_EXPIRE = 24 * 60 * 60 * 1000; 14 const SITEINFO_URLS = ['http://wiki.livedoor.jp/samurai20000/d/']; 15 const SITEINFO = [ 16 16 { 17 url: 'http://reader .livedoor.com/',17 url: 'http://reader\\.livedoor\\.com/', 18 18 focus: '//div[@id="login"]/h3/a' 19 19 } 20 /* templete 20 /* templete 21 21 { 22 url: '',23 focus: ''22 url: '', 23 focus: '' 24 24 } 25 25 */ … … 30 30 focus_element.focus(); 31 31 log('focus!'); 32 } 32 }; 33 33 34 34 var launchAutoFocus = function(list) { … … 45 45 } 46 46 } 47 } 47 }; 48 48 49 49 … … 53 53 if(!cacheInfo[i] || cacheInfo[i].expire < new Date()) { 54 54 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)} 59 59 }; 60 60 GM_xmlhttpRequest(opt); … … 63 63 launchAutoFocus(cacheInfo[i].info); 64 64 } 65 }) 66 } 65 }); 66 }; 67 67 68 68 // utility SITE_INFO … … 72 72 var re = /^([^:]+?):(.*)$/; 73 73 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)) { 76 76 info[RegExp.$1] = RegExp.$2.replace(/^\s+/, ''); 77 77 } 78 } 78 }); 79 79 80 80 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 }; 89 85 90 86 return isValid(info) ? info : null; … … 96 92 97 93 function getCache() { 98 return eval(GM_getValue('cacheInfo' )) || {};94 return eval(GM_getValue('cacheInfo', '({})')); 99 95 } 100 96 … … 104 100 } 105 101 106 var info = [];107 var matched = false;102 var info = []; 103 var matched = false; 108 104 var hdoc = createHTMLDocumentByString(res.responseText); 109 105 var pre_areas = getElementsByXPath('//pre', hdoc) || []; … … 111 107 pre_areas.forEach(function(pre_area) { 112 108 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; 118 113 } 119 114 }); … … 121 116 if (info.length > 0) { 122 117 cacheInfo[url] = { 123 urls : url,124 expire : new Date(new Date().getTime() + CACHE_EXPIRE),125 info : info118 urls : url, 119 expire : new Date(new Date().getTime() + CACHE_EXPIRE), 120 info : info 126 121 } 127 122 GM_setValue('cacheInfo', cacheInfo.toSource()); … … 143 138 144 139 function 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, ''); 146 141 var htmlDoc = document.implementation.createDocument(null, 'html', null); 147 142 var fragment = createDocumentFragmentByString(html); … … 158 153 function getFocusElementByXpath(xpath) { 159 154 var result = document.evaluate(xpath, document, null, 160 XPathResult.FIRST_ORDERED_NODE_TYPE, null);155 XPathResult.FIRST_ORDERED_NODE_TYPE, null); 161 156 return result.singleNodeValue ? result.singleNodeValue: null; 162 157 } … … 168 163 var data = []; 169 164 for (var i = 0; i < nodesSnapshot.snapshotLength; i++) { 170 data.push(nodesSnapshot.snapshotItem(i));165 data.push(nodesSnapshot.snapshotItem(i)); 171 166 } 172 167 return (data.length >= 1) ? data : null;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)