atcoder-keyboard-shortcuts

atcoder keyboard shortcuts

  1. // ==UserScript==
  2. // @name atcoder-keyboard-shortcuts
  3. // @namespace https://halmk.github.io/
  4. // @version 0.1
  5. // @description atcoder keyboard shortcuts
  6. // @author springroll
  7. // @match https://atcoder.jp/contests/*/tasks/*
  8. // @grant none
  9. // @require https://unpkg.com/hotkeys-js/dist/hotkeys.min.js
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. var copySample = function(index) {
  15. window.getSelection().removeAllRanges();
  16. try {
  17. var range = document.createRange();
  18. range.selectNode($('#pre-sample'+(2*(index-1))).get(0));
  19. window.getSelection().addRange(range);
  20. document.execCommand('copy');
  21. } catch (err) {
  22. console.log(err);
  23. }
  24. alert("Copied Sample" + index + ".");
  25. window.getSelection().removeAllRanges();
  26. };
  27.  
  28. var pasteSourceCode = function() {
  29. try {
  30. var pasteArea = document.getElementsByTagName('textarea')[0];
  31. pasteArea.focus();
  32. document.execCommand('paste');
  33. } catch (err) {
  34. console.log(err);
  35. }
  36. };
  37.  
  38. var submitCode = function() {
  39. try {
  40. var btn = document.getElementById('submit');
  41. btn.click();
  42. } catch (err) {
  43. console.log(err);
  44. }
  45. };
  46.  
  47. hotkeys('shift+1,shift+2,shift+3,shift+4,shift+5', function (event, handler){
  48. switch (handler.key) {
  49. case 'shift+1':
  50. copySample(1);
  51. break;
  52. case 'shift+2':
  53. copySample(2);
  54. break;
  55. case 'shift+3':
  56. copySample(3);
  57. break;
  58. case 'shift+4':
  59. copySample(4);
  60. break;
  61. case 'shift+5':
  62. copySample(5);
  63. break;
  64. default: alert(event);
  65. }
  66. });
  67.  
  68. hotkeys('shift+a, cmd+shift+s', function (event, handler) {
  69. switch (handler.key) {
  70. case 'shift+a':
  71. pasteSourceCode();
  72. break;
  73. case 'cmd+shift+s':
  74. submitCode();
  75. break;
  76. default: alert(event);
  77. }
  78. });
  79. })();