shortcut.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. let shortcut_key_list = {};
  3. window.addEventListener("keyup", e => {
  4. delete shortcut_key_list[e.code];
  5. });
  6. window.addEventListener("blur", () => {
  7. shortcut_key_list = {};
  8. });
  9. document.addEventListener("visibilitychange", () => {
  10. if(document.hidden) {
  11. shortcut_key_list = {};
  12. }
  13. });
  14. window.addEventListener("keydown", e => {
  15. let shortcut_check = e.target.tagName.toLowerCase();
  16. if(shortcut_check === 'input' || shortcut_check === 'textarea') {
  17. return;
  18. } else if(e.repeat) {
  19. return;
  20. }
  21. shortcut_key_list[e.code] = 1;
  22. if(Object.keys(shortcut_key_list).length === 1) {
  23. let doc_shortcut = /^\/(w|w_from|history|edit|acl|topic|xref)\//i;
  24. if(shortcut_key_list['KeyF'] === 1) {
  25. window.location.href = '/';
  26. } else if(shortcut_key_list['KeyC'] === 1) {
  27. window.location.href = '/recent_change';
  28. } else if(shortcut_key_list['KeyD'] === 1) {
  29. window.location.href = '/recent_discuss';
  30. } else if(shortcut_key_list['KeyA'] === 1) {
  31. window.location.href = '/random';
  32. }
  33. if(window.location.pathname.match(doc_shortcut)) {
  34. let doc_href = window.location.pathname.replace(doc_shortcut, '');
  35. if(shortcut_key_list['KeyW'] === 1) {
  36. window.location.pathname = '/w/' + doc_href;
  37. } else if(shortcut_key_list['KeyE'] === 1) {
  38. window.location.pathname = '/edit/' + doc_href;
  39. } else if(shortcut_key_list['KeyH'] === 1) {
  40. window.location.pathname = '/history/' + doc_href;
  41. }
  42. }
  43. }
  44. });