shortcut.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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|w_rev|w_from|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. if(shortcut_key_list['w'] === 1) {
  26. window.location.pathname = '/w/' + doc_href;
  27. } else if(shortcut_key_list['e'] === 1) {
  28. window.location.pathname = '/edit/' + doc_href;
  29. } else if(shortcut_key_list['h'] === 1) {
  30. window.location.pathname = '/history/' + doc_href;
  31. }
  32. }
  33. }
  34. }