GitHub Editor - Change Default Settings

change default settings for the github editor

As of 2016-08-03. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name GitHub Editor - Change Default Settings
  3. // @version 1.0
  4. // @description change default settings for the github editor
  5. // @author Adrien Pyke
  6. // @match *://github.com/*/new/*
  7. // @match *://github.com/*/edit/*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @namespace https://greatest.deepsurf.us/users/649
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. var loadConfig = function() {
  18. var defaults = {
  19. indentMode: 'tab',
  20. indentWidth: '4',
  21. wrapMode: 'off'
  22. };
  23.  
  24. var cfg = GM_getValue('cfg');
  25. if (!cfg) return defaults;
  26.  
  27. return JSON.parse(cfg);
  28. };
  29.  
  30. var saveConfig = function (cfg) {
  31. GM_setValue('cfg', JSON.stringify(cfg));
  32. };
  33.  
  34. var setup = function() {
  35. var createSelect = function(label, options, value) {
  36. var select = document.createElement('select');
  37. select.style.margin = '2px';
  38. var optgroup = document.createElement('optgroup');
  39. if (label) {
  40. optgroup.setAttribute('label', label);
  41. }
  42. select.appendChild(optgroup);
  43. options.forEach(function(opt) {
  44. var option = document.createElement('option');
  45. option.setAttribute('value', opt.value);
  46. option.textContent = opt.text;
  47. optgroup.appendChild(option);
  48. });
  49. select.value = value;
  50. return select;
  51. };
  52.  
  53. var createButton = function(text, onclick) {
  54. var button = document.createElement('button');
  55. button.style.margin = '2px';
  56. button.textContent = text;
  57. button.onclick = onclick;
  58. return button;
  59. };
  60.  
  61. var init = function(cfg) {
  62. var div = document.createElement('div');
  63. div.style.backgroundColor = 'white';
  64. div.style.border = '1px solid black';
  65. div.style.position = 'absolute';
  66. div.style.top = '0';
  67. div.style.right = '0';
  68.  
  69. var indentMode = createSelect('Indent mode', [
  70. { value: 'space', text: 'Spaces' },
  71. { value: 'tab', text: 'Tabs' }
  72. ], cfg.indentMode);
  73. div.appendChild(indentMode);
  74.  
  75. var indentWidth = createSelect('Indent size', [
  76. { value: 2, text: 2 },
  77. { value: 4, text: 4 },
  78. { value: 8, text: 8 }
  79. ], cfg.indentWidth);
  80. div.appendChild(indentWidth);
  81.  
  82. var wrapMode = createSelect('Line wrap mode', [
  83. { value: 'off', text: 'No wrap' },
  84. { value: 'on', text: 'Soft wrap' }
  85. ], cfg.wrapMode);
  86. div.appendChild(wrapMode);
  87.  
  88. div.appendChild(document.createElement('br'));
  89.  
  90. div.appendChild(createButton('Save', function(e) {
  91. var settings = {};
  92. settings.indentMode = indentMode.value;
  93. settings.indentWidth = indentWidth.value;
  94. settings.wrapMode = wrapMode.value;
  95. saveConfig(settings);
  96. div.remove();
  97. }));
  98.  
  99. div.appendChild(createButton('Cancel', function(e) {
  100. div.remove();
  101. }));
  102.  
  103. document.body.appendChild(div);
  104. };
  105. init(loadConfig());
  106. };
  107.  
  108. var updateDropdown = function(dropdown, value) {
  109. dropdown.value = value;
  110. var evt = document.createEvent('HTMLEvents');
  111. evt.initEvent('change', false, true);
  112. dropdown.dispatchEvent(evt);
  113. };
  114.  
  115. var applySettings = function(cfg) {
  116. var indentMode = document.querySelector('.js-code-indent-mode');
  117. var indentWidth = document.querySelector('.js-code-indent-width');
  118. var wrapMode = document.querySelector('.js-code-wrap-mode');
  119.  
  120. if (location.href.match(/^https?:\/\/github.com\/[^\/]*\/[^\/]*\/new\/.*/)) {
  121. // new file
  122. updateDropdown(indentMode, cfg.indentMode);
  123. updateDropdown(indentWidth, cfg.indentWidth);
  124. updateDropdown(wrapMode, cfg.wrapMode);
  125. } else if (location.href.match(/^https?:\/\/github.com\/[^\/]*\/[^\/]*\/edit\/.*/)) {
  126. // edit file
  127. // if the file is using space indentation we don't want to change it
  128. if (indentMode.value === 'tab') {
  129. updateDropdown(indentWidth, cfg.indentWidth);
  130. }
  131. updateDropdown(wrapMode, cfg.wrapMode);
  132. }
  133. };
  134.  
  135. GM_registerMenuCommand('GitHub Editor Settings', setup);
  136. var settings = loadConfig();
  137. applySettings(settings);
  138. })();