CodeSchool Keyboard Shortcuts

Adds much-needed and much-requested keyboard shortcuts to go to the next section in a course.

  1. // ==UserScript==
  2. // @name CodeSchool Keyboard Shortcuts
  3. // @namespace http://jonas.ninja
  4. // @version 1.1.0
  5. // @description Adds much-needed and much-requested keyboard shortcuts to go to the next section in a course.
  6. // @author @_jnblog
  7. // @match http*://campus.codeschool.com/courses/*/level/*/section/*
  8. // @match http*://campus.codeschool.com/courses/*/level/*/wrap-up
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. document.body.onkeyup = function(e) {
  14. if (e.altKey && e.key === 'Enter') {
  15. var button = getContinueButton();
  16.  
  17. if (button) {
  18. click(button);
  19. }
  20. }
  21. };
  22.  
  23. function getContinueButton() {
  24. /// identify the continue button. If it cannot be identified, returns undefined.
  25.  
  26. var buttons = document.getElementsByClassName('btn--continue');
  27. // on the wrap-up screen, there's a single 'btn--next' but no 'btn--continue's, so use that.
  28. if (buttons.length === 0) {
  29. buttons = document.getElementsByClassName('btn--next');
  30. }
  31. if (getTheVisibleButtons(buttons).length === 1) {
  32. return buttons[0];
  33. }
  34. return undefined;
  35. }
  36.  
  37. function getTheVisibleButtons(list) {
  38. list = [].slice.call(list); // convert the 'HtmlCollection' to a list
  39. return list.filter(function(el) {
  40. return isVisible(el);
  41. });
  42. }
  43.  
  44. function isVisible(el) {
  45. return (el.offsetHeight > 0 && el.offsetWidth > 0);
  46. }
  47.  
  48. function click(el) {
  49. var evt = document.createEvent("HTMLEvents");
  50. evt.initEvent("click", true, true);
  51. (el).dispatchEvent(evt);
  52. }
  53. })();