GitHub Editor - Change Default Settings

change default settings for the github editor

As of 2018-05-08. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub Editor - Change Default Settings
  3. // @namespace https://greatest.deepsurf.us/users/649
  4. // @version 1.1.5
  5. // @description change default settings for the github editor
  6. // @author Adrien Pyke
  7. // @match *://github.com/*/new/*
  8. // @match *://github.com/*/edit/*
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @require https://cdn.rawgit.com/kufii/My-UserScripts/1fa63b68a5d8734d772f0230e8aeb621bd7084a9/libs/gm_config.js
  13. // @require https://cdn.rawgit.com/fuzetsu/userscripts/477063e939b9658b64d2f91878da20a7f831d98b/wait-for-elements/wait-for-elements.js
  14. // ==/UserScript==
  15.  
  16. (() => {
  17. 'use strict';
  18.  
  19. const Config = GM_config([
  20. {
  21. key: 'indentMode',
  22. label: 'Indent mode',
  23. default: 'tab',
  24. type: 'dropdown',
  25. values: [
  26. { value: 'space', text: 'Spaces' },
  27. { value: 'tab', text: 'Tabs' }
  28. ]
  29. }, {
  30. key: 'indentWidth',
  31. label: 'Indent size',
  32. default: 4,
  33. type: 'dropdown',
  34. values: [2, 4, 8]
  35. }, {
  36. key: 'wrapMode',
  37. label: 'Line wrap mode',
  38. default: 'off',
  39. type: 'dropdown',
  40. values: [
  41. { value: 'off', text: 'No wrap' },
  42. { value: 'on', text: 'Soft wrap' }
  43. ]
  44. }
  45. ]);
  46.  
  47. const updateDropdown = function(dropdown, value) {
  48. dropdown.value = value;
  49. let evt = document.createEvent('HTMLEvents');
  50. evt.initEvent('change', false, true);
  51. dropdown.dispatchEvent(evt);
  52. };
  53.  
  54. const applySettings = function(cfg) {
  55. let indentMode = document.querySelector('.js-code-indent-mode');
  56. let indentWidth = document.querySelector('.js-code-indent-width');
  57. let wrapMode = document.querySelector('.js-code-wrap-mode');
  58.  
  59. if (location.href.match(/^https?:\/\/github.com\/[^/]*\/[^/]*\/new\/.*/)) {
  60. // new file
  61. updateDropdown(indentMode, cfg.indentMode);
  62. updateDropdown(indentWidth, cfg.indentWidth);
  63. updateDropdown(wrapMode, cfg.wrapMode);
  64. } else if (location.href.match(/^https?:\/\/github.com\/[^/]*\/[^/]*\/edit\/.*/)) {
  65. // edit file
  66. // if the file is using space indentation we don't want to change it
  67. if (indentMode.value === 'tab') {
  68. updateDropdown(indentWidth, cfg.indentWidth);
  69. }
  70. updateDropdown(wrapMode, cfg.wrapMode);
  71. }
  72. };
  73.  
  74. GM_registerMenuCommand('GitHub Editor Settings', Config.setup);
  75. let settings = Config.load();
  76.  
  77. waitForElems({
  78. sel: '.CodeMirror-code',
  79. onmatch() {
  80. applySettings(settings);
  81. }
  82. });
  83. })();