Prettify JSON Selection

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

اعتبارا من 11-10-2024. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل 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) => {
    console.log(event.key);
    // Shift + Alt(Option) + F
    if (event.shiftKey && event.altKey && (event.key === "Ï" || event.key === "F")) {
      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) {}
  }
})();