Json formatter

try to take over the world!

  1. // ==UserScript==
  2. // @name Json formatter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author RobinTsai
  7. // @match https://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var indentation = 4; // Change this to vary the indentation
  15. var pre = document.querySelector('body pre:only-of-type'); // https://developer.mozilla.org/zh-CN/docs/Web/CSS/:only-of-type
  16.  
  17. if (!pre) return; // Don't do anything if this don't seem to be a json only document
  18. try {
  19. pre.innerHTML = JSON.stringify(JSON.parse(pre.innerHTML), null, indentation);
  20. } catch (e) {
  21. console.log(e);
  22. }
  23. })();