Changeset 9460
- Timestamp:
- 04/15/08 04:16:37 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/vimperator-plugins/trunk/feedSomeKeys.js
r9440 r9460 11 11 * 12 12 * keyイベント(正確にはkepressイベント)をWebコンテンツ側へ送る事を可能にするプラグイン 13 * Gmailとか Livedoor ReaderとかGreasemonkeyでキーを割り当てている場合に活躍するでしょう。13 * Gmailとかlivedoor ReaderとかGreasemonkeyでキーを割り当てている場合に活躍するでしょう。 14 14 * それ以外の場面ではむしろ邪魔になる諸刃の剣 15 15 * … … 24 24 js <<EOF 25 25 autocommands.add('PageLoad,TabSelect',/reader\.livedoor\.com\/reader\//, 26 'js plugins.feedKey.setup( ["j","k","s","a","p","o","v","c","<Space>","<S-Space>","z","b","<",">"]);');26 'js plugins.feedKey.setup("j k s a p o v c <Space> <S-Space> z b < >".split(/ +/));'); 27 27 EOF 28 28 * とかやると幸せになれるかも。 29 * 29 * 30 30 * == Gmail の場合 == 31 31 js <<EOF 32 32 autocommands.add('PageLoad,TabSelect',/mail\.google\.com\/mail/,[ 33 'js plugins.feedKey.setup([', 34 '["c","3c"],["/","3/"],["j","3j"],["k","3k"],["n","3n"],["p","3p"],["o","3o"],["u","3u"],["e","3e"]', 35 '["x","3x"],["s","3s"],["r","3r"],["a","3a"],["#","3#"],["[","3["],["]","3]"],["z","3z"],["?","3?"]', 36 '["gi","3gi"],["gs","3gs"],["gt","3gt"],["gd","3gd"],["ga","3ga"],["gc","3gc"]', 37 ']);' 33 'js plugins.feedKey.setup(', 34 '"c / j k n p o u e x s r a # [ ] z ? gi gs gt gd ga gc".split(/ +/).map(function(i) [i, "3" + i])', 35 ');' 38 36 ].join('')); 39 37 EOF … … 49 47 50 48 // keyTableの再定義...ひどく不毛... 51 var keyTable = [ 49 const keyTable = [ 50 [ KeyEvent.DOM_VK_BACK_SPACE, ["BS"] ], 51 [ KeyEvent.DOM_VK_TAB, ["Tab"] ], 52 [ KeyEvent.DOM_VK_RETURN, ["Return", "CR", "Enter"] ], 53 //[ KeyEvent.DOM_VK_ENTER, ["Enter"] ], 52 54 [ KeyEvent.DOM_VK_ESCAPE, ["Esc", "Escape"] ], 53 [ KeyEvent.DOM_VK_LEFT_SHIFT, ["<"] ], 54 [ KeyEvent.DOM_VK_RIGHT_SHIFT, [">"] ], 55 [ KeyEvent.DOM_VK_RETURN, ["Return", "CR", "Enter"] ], 56 [ KeyEvent.DOM_VK_TAB, ["Tab"] ], 57 [ KeyEvent.DOM_VK_DELETE, ["Del"] ], 58 [ KeyEvent.DOM_VK_BACK_SPACE, ["BS"] ], 59 [ KeyEvent.DOM_VK_HOME, ["Home"] ], 60 [ KeyEvent.DOM_VK_INSERT, ["Insert", "Ins"] ], 61 [ KeyEvent.DOM_VK_END, ["End"] ], 62 [ KeyEvent.DOM_VK_LEFT, ["Left"] ], 63 [ KeyEvent.DOM_VK_RIGHT, ["Right"] ], 64 [ KeyEvent.DOM_VK_UP, ["Up"] ], 65 [ KeyEvent.DOM_VK_DOWN, ["Down"] ], 55 [ KeyEvent.DOM_VK_SPACE, ["Spc", "Space"] ], 66 56 [ KeyEvent.DOM_VK_PAGE_UP, ["PageUp"] ], 67 57 [ KeyEvent.DOM_VK_PAGE_DOWN, ["PageDown"] ], 58 [ KeyEvent.DOM_VK_END, ["End"] ], 59 [ KeyEvent.DOM_VK_HOME, ["Home"] ], 60 [ KeyEvent.DOM_VK_LEFT, ["Left"] ], 61 [ KeyEvent.DOM_VK_UP, ["Up"] ], 62 [ KeyEvent.DOM_VK_RIGHT, ["Right"] ], 63 [ KeyEvent.DOM_VK_DOWN, ["Down"] ], 64 [ KeyEvent.DOM_VK_INSERT, ["Ins", "Insert"] ], 65 [ KeyEvent.DOM_VK_DELETE, ["Del", "Delete"] ], 68 66 [ KeyEvent.DOM_VK_F1, ["F1"] ], 69 67 [ KeyEvent.DOM_VK_F2, ["F2"] ], … … 93 91 function getKeyCode(str) { 94 92 str = str.toLowerCase(); 95 for (var i in keyTable) { 96 for (var k in keyTable[i][1]) { 97 if (keyTable[i][1][k].toLowerCase() == str) return keyTable[i][0]; 98 } 99 } 100 return 0; 93 var ret = 0; 94 keyTable.some(function(i) i[1].some(function(k) k.toLowerCase() == str && (ret = i[0]))); 95 return ret; 101 96 } 102 97 function init(keys){ 103 98 destroy(); 104 for each(var key in keys){99 keys.forEach(function(key){ 105 100 var origKey, feedKey; 106 if ( typeof(key) == 'object'){107 [origKey, feedKey] = [key[0],key[1]];101 if (key instanceof Array){ 102 [origKey, feedKey] = key; 108 103 } else if (typeof(key) == 'string'){ 109 104 [origKey, feedKey] = [key,key]; 110 105 } 111 106 replaceUserMap(origKey, feedKey); 112 } 107 }); 113 108 } 114 109 function replaceUserMap(origKey, feedKey){ … … 118 113 // origMapをそのままpushするとオブジェクト内の参照先を消されてしまう 119 114 // 仕方なく複製を試みる 120 var clone = new Map(origMap.modes.map(function(m) {return m;}),121 origMap.names.map(function(n) {return n;}),115 var clone = new Map(origMap.modes.map(function(m) m), 116 origMap.names.map(function(n) n), 122 117 origMap.description, 123 118 origMap.action, 124 { flags: origMap.flags, rhs:origMap.rhs, noremap:origMap.noremap });119 { flags:origMap.flags, rhs:origMap.rhs, noremap:origMap.noremap }); 125 120 origMaps.push(clone); 126 121 } … … 132 127 feedKeyIntoContent(feedKey); 133 128 } 134 }, { flags: liberator.Mappings.flags.COUNT, rhs:feedKey, noremap:true });129 }, { flags:liberator.Mappings.flags.COUNT, rhs:feedKey, noremap:true }); 135 130 addUserMap(map); 136 for each(var fmap in feedMaps){ 137 if (fmap.names[0] == origKey){ 138 for (var key in fmap) fmap[key] = map[key]; 139 return; 140 } 141 } 131 if (feedMaps.some(function(fmap){ 132 if (fmap.names[0] != origKey) return false; 133 for (var key in fmap) fmap[key] = map[key]; 134 return true; 135 })) return; 142 136 feedMaps.push(map); 143 137 } 144 138 function destroy(){ 145 139 try{ 146 feedMaps.forEach(function(map){147 mappings.remove(map.modes[0],map.names[0]);148 });140 feedMaps.forEach(function(map){ 141 mappings.remove(map.modes[0],map.names[0]); 142 }); 149 143 }catch(e){ log(map); } 150 144 origMaps.forEach(function(map){ … … 158 152 } 159 153 function parseKeys(keys){ 160 var matches = keys.match(/^(\d+).+/);154 var matches = /^\d+(?=\D)/.exec(keys); 161 155 if (matches){ 162 var num = parseInt(matches[ 1],10);163 if (!isNaN(num)) return [keys.substr(matches[ 1].length), num];156 var num = parseInt(matches[0],10); 157 if (!isNaN(num)) return [keys.substr(matches[0].length), num]; 164 158 } 165 159 return [keys, 0]; … … 197 191 var shift = false, ctrl = false, alt = false, meta = false; 198 192 if (charCode == 60){ // charCode:60 => "<" 199 var matches = keys.substr(i + 1).match(/ ([CSMAcsma]-)*([^>]+)/);200 if (matches && matches[2]) {193 var matches = keys.substr(i + 1).match(/^((?:[ACMSacms]-)*)([^>]+)/); 194 if (matches) { 201 195 if (matches[1]) { 202 196 ctrl = /[cC]-/.test(matches[1]); … … 209 203 charCode = matches[2].charCodeAt(0); 210 204 } else if (matches[2].toLowerCase() == "space") { 211 charCode = 32;205 charCode = KeyEvent.DOM_VK_SPACE; 212 206 } else if (keyCode = getKeyCode(matches[2])) { 213 207 charCode = 0; … … 234 228 function(args){ 235 229 if(!args){ 236 echo(feedMaps.map(function(map){ 237 return map.description.replace(/</g,'<').replace(/>/g,'>'); 238 }),true); 230 echo(feedMaps.map(function(map) map.description.replace(/</g,'<').replace(/>/g,'>')),true); 239 231 } 240 232 var [ ,lhs,rhs] = args.match(/(\S+)(?:\s+(.+))?/); … … 246 238 } 247 239 ); 248 commands.addUserCommand(['feedmapclear','fmapc'],'Clear Feed Maps', 249 function(){ 250 destroy(); 251 } 252 ); 240 commands.addUserCommand(['feedmapclear','fmapc'],'Clear Feed Maps',destroy); 253 241 var converter = { 254 setup: function(keys){ 255 init(keys); 256 }, 257 get origMap(){ 258 return origMaps; 259 }, 260 get feedMap(){ 261 return feedMaps; 262 }, 263 reset: function(){ 264 destroy(); 265 } 242 get origMap() origMaps, 243 get feedMap() feedMaps, 244 setup: init, 245 reset: destroy 266 246 }; 267 247 return converter;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)