Prettify JSON Selection

Adds a shortcut(Shift + Alt(Option) + F) and context menu option to prettify code selection.

2024-10-11 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Prettify JSON Selection
// @namespace    vengut.github.io
// @version      2024-10-10
// @description  Adds a shortcut(Shift + Alt(Option) + F) and context menu option to prettify code selection.
// @license      MIT
// @author       Venkat G
// @match        *://*/*
// @icon         data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📄</text></svg>
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
  "use strict";

  GM_registerMenuCommand("Prettify", prettifyJson);

  document.querySelector("body").addEventListener("keydown", (event) => {
    // Shift + Alt(Option) + F
    if (event.shiftKey && event.altKey && event.key === "Ï") {
      prettifyJson();
    }
  });

  function prettifyJson() {
    try {
      let text = "";

      // https://stackoverflow.com/a/5379408
      if (window.getSelection) {
        text = window.getSelection().toString();
      } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
      }

      const json = JSON.stringify(JSON.parse(text), null, 2);
      const blob = new Blob([json], { type: "application/json" });
      const blobUrl = URL.createObjectURL(blob);
      window.open(blobUrl);
    } catch (err) {}
  }
})();