GitHub Editor - Change Default Settings

change default settings for the github editor

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

  1. // ==UserScript==
  2. // @name GitHub Editor - Change Default Settings
  3. // @namespace https://greatest.deepsurf.us/users/649
  4. // @version 1.0.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. // ==/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. indentMode: indentMode.value,
  93. indentWidth: indentWidth.value,
  94. wrapMode: wrapMode.value
  95. };
  96. saveConfig(settings);
  97. div.remove();
  98. }));
  99.  
  100. div.appendChild(createButton('Cancel', function(e) {
  101. div.remove();
  102. }));
  103.  
  104. document.body.appendChild(div);
  105. };
  106. init(loadConfig());
  107. };
  108.  
  109. var updateDropdown = function(dropdown, value) {
  110. dropdown.value = value;
  111. var evt = document.createEvent('HTMLEvents');
  112. evt.initEvent('change', false, true);
  113. dropdown.dispatchEvent(evt);
  114. };
  115.  
  116. var applySettings = function(cfg) {
  117. var indentMode = document.querySelector('.js-code-indent-mode');
  118. var indentWidth = document.querySelector('.js-code-indent-width');
  119. var wrapMode = document.querySelector('.js-code-wrap-mode');
  120.  
  121. if (location.href.match(/^https?:\/\/github.com\/[^\/]*\/[^\/]*\/new\/.*/)) {
  122. // new file
  123. updateDropdown(indentMode, cfg.indentMode);
  124. updateDropdown(indentWidth, cfg.indentWidth);
  125. updateDropdown(wrapMode, cfg.wrapMode);
  126. } else if (location.href.match(/^https?:\/\/github.com\/[^\/]*\/[^\/]*\/edit\/.*/)) {
  127. // edit file
  128. // if the file is using space indentation we don't want to change it
  129. if (indentMode.value === 'tab') {
  130. updateDropdown(indentWidth, cfg.indentWidth);
  131. }
  132. updateDropdown(wrapMode, cfg.wrapMode);
  133. }
  134. };
  135.  
  136. GM_registerMenuCommand('GitHub Editor Settings', setup);
  137. var settings = loadConfig();
  138. applySettings(settings);
  139. })();