GitHub Editor - Change Default Settings

change default settings for the github editor

As of 2020-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.20
  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://gitcdn.link/repo/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
  13. // @require https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/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. {
  31. key: 'indentWidth',
  32. label: 'Indent size',
  33. default: 4,
  34. type: 'dropdown',
  35. values: [2, 4, 8]
  36. },
  37. {
  38. key: 'wrapMode',
  39. label: 'Line wrap mode',
  40. default: 'off',
  41. type: 'dropdown',
  42. values: [
  43. { value: 'off', text: 'No wrap' },
  44. { value: 'on', text: 'Soft wrap' }
  45. ]
  46. }
  47. ]);
  48.  
  49. const updateDropdown = function (dropdown, value) {
  50. dropdown.value = value;
  51. const evt = document.createEvent('HTMLEvents');
  52. evt.initEvent('change', false, true);
  53. dropdown.dispatchEvent(evt);
  54. };
  55.  
  56. const applySettings = function (cfg) {
  57. const indentMode = document.querySelector('.js-code-indent-mode');
  58. const indentWidth = document.querySelector('.js-code-indent-width');
  59. const wrapMode = document.querySelector('.js-code-wrap-mode');
  60.  
  61. if (location.href.match(/^https?:\/\/github.com\/[^/]*\/[^/]*\/new\/.*/u)) {
  62. // new file
  63. updateDropdown(indentMode, cfg.indentMode);
  64. updateDropdown(indentWidth, cfg.indentWidth);
  65. updateDropdown(wrapMode, cfg.wrapMode);
  66. } else if (location.href.match(/^https?:\/\/github.com\/[^/]*\/[^/]*\/edit\/.*/u)) {
  67. // edit file
  68. // if the file is using space indentation we don't want to change it
  69. if (indentMode.value === 'tab') {
  70. updateDropdown(indentWidth, cfg.indentWidth);
  71. }
  72. updateDropdown(wrapMode, cfg.wrapMode);
  73. }
  74. };
  75.  
  76. GM_registerMenuCommand('GitHub Editor Settings', Config.setup);
  77. const settings = Config.load();
  78.  
  79. waitForElems({
  80. sel: '.CodeMirror-code',
  81. onmatch() {
  82. applySettings(settings);
  83. }
  84. });
  85. })();