JSON Formatter

auto format JSON files

当前为 2020-07-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name JSON Formatter
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.0
  5. // @description auto format JSON files
  6. // @author Adrien Pyke
  7. // @include /^.*\.json(\?.*)?$/
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @require https://cdn.jsdelivr.net/gh/kufii/My-UserScripts@c7f613292672252995cb02a0cab3b6acb18ccac5/libs/gm_config.js
  12. // ==/UserScript==
  13.  
  14. (() => {
  15. 'use strict';
  16.  
  17. const Config = GM_config([
  18. { key: 'tabSize', label: 'Tab Size', type: 'number', min: 0, default: 2 }
  19. ]);
  20. GM_registerMenuCommand('JSON Formatter: Tab Size', Config.setup);
  21.  
  22. const format = tabSize => {
  23. const pre = document.querySelector('pre');
  24. pre.textContent = JSON.stringify(JSON.parse(pre.textContent), null, tabSize);
  25. };
  26. format(Config.load().tabSize);
  27. Config.onsave = ({ tabSize }) => format(tabSize);
  28. })();