Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.0 KiB

  1. /* based on chromium plugin code, adapted by Nibble<.gs@gmail.com> */
  2. var hint_num_str = '';
  3. var hint_elems = [];
  4. var hint_open_in_new_tab = false;
  5. var hint_enabled = false;
  6. function hintMode(newtab){
  7. hint_enabled = true;
  8. if (newtab) {
  9. hint_open_in_new_tab = true;
  10. } else {
  11. hint_open_in_new_tab = false;
  12. }
  13. setHints();
  14. document.removeEventListener('keydown', initKeyBind, false);
  15. document.addEventListener('keydown', hintHandler, false);
  16. hint_num_str = '';
  17. }
  18. function hintHandler(e){
  19. e.preventDefault(); //Stop Default Event
  20. var pressedKey = get_key(e);
  21. if (pressedKey == 'Enter') {
  22. if (hint_num_str == '')
  23. hint_num_str = '1';
  24. judgeHintNum(Number(hint_num_str));
  25. } else if (/[0-9]/.test(pressedKey) == false) {
  26. removeHints();
  27. } else {
  28. hint_num_str += pressedKey;
  29. var hint_num = Number(hint_num_str);
  30. if (hint_num * 10 > hint_elems.length + 1) {
  31. judgeHintNum(hint_num);
  32. } else {
  33. var hint_elem = hint_elems[hint_num - 1];
  34. if (hint_elem != undefined && hint_elem.tagName.toLowerCase() == 'a') {
  35. setHighlight(hint_elem, true);
  36. }
  37. }
  38. }
  39. }
  40. function setHighlight(elem, is_active) {
  41. if (is_active) {
  42. var active_elem = document.body.querySelector('a[highlight=hint_active]');
  43. if (active_elem != undefined)
  44. active_elem.setAttribute('highlight', 'hint_elem');
  45. elem.setAttribute('highlight', 'hint_active');
  46. } else {
  47. elem.setAttribute('highlight', 'hint_elem');
  48. }
  49. }
  50. function setHintRules() {
  51. if (document.styleSheets.length < 1) {
  52. var style = document.createElement("style");
  53. style.appendChild(document.createTextNode(""));
  54. document.head.appendChild(style);
  55. }
  56. var ss = document.styleSheets[0];
  57. ss.insertRule('a[highlight=hint_elem] {background-color: yellow}', 0);
  58. ss.insertRule('a[highlight=hint_active] {background-color: lime}', 0);
  59. }
  60. function deleteHintRules() {
  61. var ss = document.styleSheets[0];
  62. ss.deleteRule(0);
  63. ss.deleteRule(0);
  64. }
  65. function judgeHintNum(hint_num) {
  66. var hint_elem = hint_elems[hint_num - 1];
  67. if (hint_elem != undefined) {
  68. execSelect(hint_elem);
  69. } else {
  70. removeHints();
  71. }
  72. }
  73. function execSelect(elem) {
  74. var tag_name = elem.tagName.toLowerCase();
  75. var type = elem.type ? elem.type.toLowerCase() : "";
  76. if (tag_name == 'a' && elem.href != '') {
  77. setHighlight(elem, true);
  78. // TODO: ajax, <select>
  79. if (hint_open_in_new_tab)
  80. window.open(elem.href);
  81. else location.href=elem.href;
  82. } else if (tag_name == 'input' && (type == "submit" || type == "button" || type == "reset")) {
  83. elem.click();
  84. } else if (tag_name == 'input' && (type == "radio" || type == "checkbox")) {
  85. // TODO: toggle checkbox
  86. elem.checked = !elem.checked;
  87. } else if (tag_name == 'input' || tag_name == 'textarea') {
  88. elem.focus();
  89. elem.setSelectionRange(elem.value.length, elem.value.length);
  90. }
  91. removeHints();
  92. }
  93. function setHints() {
  94. setHintRules();
  95. var win_top = window.scrollY;
  96. var win_bottom = win_top + window.innerHeight;
  97. var win_left = window.scrollX;
  98. var win_right = win_left + window.innerWidth;
  99. // TODO: <area>
  100. var elems = document.body.querySelectorAll('a, input:not([type=hidden]), textarea, select, button');
  101. var div = document.createElement('div');
  102. div.setAttribute('highlight', 'hints');
  103. document.body.appendChild(div);
  104. for (var i = 0; i < elems.length; i++) {
  105. var elem = elems[i];
  106. if (!isHintDisplay(elem))
  107. continue;
  108. var pos = elem.getBoundingClientRect();
  109. var elem_top = win_top + pos.top;
  110. var elem_bottom = win_top + pos.bottom;
  111. var elem_left = win_left + pos.left;
  112. var elem_right = win_left + pos.left;
  113. if ( elem_bottom >= win_top && elem_top <= win_bottom) {
  114. hint_elems.push(elem);
  115. setHighlight(elem, false);
  116. var span = document.createElement('span');
  117. span.style.cssText = [
  118. 'left: ', elem_left, 'px;',
  119. 'top: ', elem_top, 'px;',
  120. 'position: absolute;',
  121. 'font-size: 13px;',
  122. 'background-color: ' + (hint_open_in_new_tab ? '#ff6600' : 'red') + ';',
  123. 'color: white;',
  124. 'font-weight: bold;',
  125. 'padding: 0px 1px;',
  126. 'z-index: 100000;'
  127. ].join('');
  128. span.innerHTML = hint_elems.length;
  129. div.appendChild(span);
  130. if (elem.tagName.toLowerCase() == 'a') {
  131. if (hint_elems.length == 1) {
  132. setHighlight(elem, true);
  133. } else {
  134. setHighlight(elem, false);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. function isHintDisplay(elem) {
  141. var pos = elem.getBoundingClientRect();
  142. return (pos.height != 0 && pos.width != 0);
  143. }
  144. function removeHints() {
  145. if (!hint_enabled)
  146. return;
  147. hint_enabled = false;
  148. deleteHintRules();
  149. for (var i = 0; i < hint_elems.length; i++) {
  150. hint_elems[i].removeAttribute('highlight');
  151. }
  152. hint_elems = [];
  153. hint_num_str = '';
  154. var div = document.body.querySelector('div[highlight=hints]');
  155. if (div != undefined) {
  156. document.body.removeChild(div);
  157. }
  158. document.removeEventListener('keydown', hintHandler, false);
  159. document.addEventListener('keydown', initKeyBind, false);
  160. }
  161. function addKeyBind( key, func, eve ){
  162. var pressedKey = get_key(eve);
  163. if( pressedKey == key ){
  164. eve.preventDefault(); //Stop Default Event
  165. eval(func);
  166. }
  167. }
  168. document.addEventListener( 'keydown', initKeyBind, false );
  169. function initKeyBind(e){
  170. var t = e.target;
  171. if( t.nodeType == 1){
  172. addKeyBind( 'C-w', 'hintMode()', e );
  173. addKeyBind( 'C-F', 'hintMode(true)', e );
  174. addKeyBind( 'C-c', 'removeHints()', e );
  175. }
  176. }
  177. var keyId = {
  178. "U+0008" : "BackSpace",
  179. "U+0009" : "Tab",
  180. "U+0018" : "Cancel",
  181. "U+001B" : "Esc",
  182. "U+0020" : "Space",
  183. "U+0021" : "!",
  184. "U+0022" : "\"",
  185. "U+0023" : "#",
  186. "U+0024" : "$",
  187. "U+0026" : "&",
  188. "U+0027" : "'",
  189. "U+0028" : "(",
  190. "U+0029" : ")",
  191. "U+002A" : "*",
  192. "U+002B" : "+",
  193. "U+002C" : ",",
  194. "U+002D" : "-",
  195. "U+002E" : ".",
  196. "U+002F" : "/",
  197. "U+0030" : "0",
  198. "U+0031" : "1",
  199. "U+0032" : "2",
  200. "U+0033" : "3",
  201. "U+0034" : "4",
  202. "U+0035" : "5",
  203. "U+0036" : "6",
  204. "U+0037" : "7",
  205. "U+0038" : "8",
  206. "U+0039" : "9",
  207. "U+003A" : ":",
  208. "U+003B" : ";",
  209. "U+003C" : "<",
  210. "U+003D" : "=",
  211. "U+003E" : ">",
  212. "U+003F" : "?",
  213. "U+0040" : "@",
  214. "U+0041" : "a",
  215. "U+0042" : "b",
  216. "U+0043" : "c",
  217. "U+0044" : "d",
  218. "U+0045" : "e",
  219. "U+0046" : "f",
  220. "U+0047" : "g",
  221. "U+0048" : "h",
  222. "U+0049" : "i",
  223. "U+004A" : "j",
  224. "U+004B" : "k",
  225. "U+004C" : "l",
  226. "U+004D" : "m",
  227. "U+004E" : "n",
  228. "U+004F" : "o",
  229. "U+0050" : "p",
  230. "U+0051" : "q",
  231. "U+0052" : "r",
  232. "U+0053" : "s",
  233. "U+0054" : "t",
  234. "U+0055" : "u",
  235. "U+0056" : "v",
  236. "U+0057" : "w",
  237. "U+0058" : "x",
  238. "U+0059" : "y",
  239. "U+005A" : "z",
  240. //"U+005B" : "[",
  241. //"U+005C" : "\\",
  242. //"U+005D" : "]",
  243. "U+00DB" : "[",
  244. "U+00DC" : "\\",
  245. "U+00DD" : "]",
  246. "U+005E" : "^",
  247. "U+005F" : "_",
  248. "U+0060" : "`",
  249. "U+007B" : "{",
  250. "U+007C" : "|",
  251. "U+007D" : "}",
  252. "U+007F" : "Delete",
  253. "U+00A1" : "¡",
  254. "U+0300" : "CombGrave",
  255. "U+0300" : "CombAcute",
  256. "U+0302" : "CombCircum",
  257. "U+0303" : "CombTilde",
  258. "U+0304" : "CombMacron",
  259. "U+0306" : "CombBreve",
  260. "U+0307" : "CombDot",
  261. "U+0308" : "CombDiaer",
  262. "U+030A" : "CombRing",
  263. "U+030B" : "CombDblAcute",
  264. "U+030C" : "CombCaron",
  265. "U+0327" : "CombCedilla",
  266. "U+0328" : "CombOgonek",
  267. "U+0345" : "CombYpogeg",
  268. "U+20AC" : "€",
  269. "U+3099" : "CombVoice",
  270. "U+309A" : "CombSVoice",
  271. }
  272. function get_key(evt){
  273. var key = keyId[evt.keyIdentifier] || evt.keyIdentifier,
  274. ctrl = evt.ctrlKey ? 'C-' : '',
  275. meta = (evt.metaKey || evt.altKey) ? 'M-' : '',
  276. shift = evt.shiftKey ? 'S-' : '';
  277. if (evt.shiftKey){
  278. if (/^[a-z]$/.test(key))
  279. return ctrl+meta+key.toUpperCase();
  280. if (/^[0-9]$/.test(key)) {
  281. switch(key) {
  282. // TODO
  283. case "4":
  284. key = "$";
  285. break;
  286. };
  287. return key;
  288. }
  289. if (/^(Enter|Space|BackSpace|Tab|Esc|Home|End|Left|Right|Up|Down|PageUp|PageDown|F(\d\d?))$/.test(key))
  290. return ctrl+meta+shift+key;
  291. }
  292. return ctrl+meta+key;
  293. }
  294. function guarantee_stylesheet(){
  295. var thehead = document.getElementsByTagName('head')[0];
  296. if (document.getElementsByTagName('style').length < 1){
  297. var thesty = document.createElement('style');
  298. thesty.innerHTML = thesty.innerHTML + ' div#diag_kludge{color: #0099ff;} ';
  299. thesty.innerHTML = thesty.innerHTML + ' div#diag_kludge{background-color: #660000;} ';
  300. thehead.appendChild(thesty);
  301. divv = document.getElementById('diag_kludge');
  302. titl = headd.getElementsByTagName('title')[0];
  303. }
  304. }
  305. window.addEventListener('load', guarantee_stylesheet, false);