GitHub Editor - Change Default Settings

change default settings for the github editor

Verze ze dne 20. 10. 2019. Zobrazit nejnovější verzi.

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