shortcut.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. let shortcut_key_list = [];
  3. document.onkeyup = function(e) {
  4. delete shortcut_key_list[e.key];
  5. }
  6. document.onkeypress = function(e) {
  7. let shortcut_check = event.target.tagName.toLowerCase();
  8. if(
  9. shortcut_check !== 'input' &&
  10. shortcut_check !== 'textarea'
  11. ) {
  12. let doc_shortcut = /^\/(w|history|edit|acl|topic|xref)\//i;
  13. shortcut_key_list[e.key] = 1;
  14. if(shortcut_key_list['f'] === 1) {
  15. window.location.href = '/';
  16. } else if(shortcut_key_list['c'] === 1) {
  17. window.location.href = '/recent_change';
  18. } else if(shortcut_key_list['d'] === 1) {
  19. window.location.href = '/recent_discuss';
  20. } else if(shortcut_key_list['a'] === 1) {
  21. window.location.href = '/random';
  22. }
  23. if(window.location.pathname.match(doc_shortcut)) {
  24. let doc_href = window.location.pathname.replace(doc_shortcut, '');
  25. doc_href = doc_href.replace(/(?:%2F|\/)(?:doc_from|doc_rev)(?:%2F|\/)(?:((?!%2F|\/).)+)$/, '');
  26. if(shortcut_key_list['w'] === 1) {
  27. window.location.pathname = '/w/' + doc_href;
  28. } else if(shortcut_key_list['e'] === 1) {
  29. window.location.pathname = '/edit/' + doc_href;
  30. } else if(shortcut_key_list['h'] === 1) {
  31. window.location.pathname = '/history/' + doc_href;
  32. }
  33. }
  34. }
  35. }