🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2025/12/27 02:37:49
🔄 下次更新时间:2025/12/27 03:37:49
手动刷新缓存

JSON Viewer

Beautify JSON display in browser

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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.

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

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

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

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

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         JSON Viewer
// @namespace    https://github.com/EastSun5566
// @version      0.0.2
// @description  Beautify JSON display in browser
// @author       Michael Wang
// @license      MIT
// @match        *://*/*
// @grant        GM_addStyle
// @run-at       document-end
// ==/UserScript==

// @ts-check
/* eslint-disable no-alert */

/** @param {string} text  */
function escapeHtml(text) {
  const div = document.createElement('div');
  div.textContent = text;
  return div.innerHTML;
}

/** @param {unknown} data */
function renderJson(data, indent = 0) {
  const indentStr = '  '.repeat(indent);
  let html = '';

  if (Array.isArray(data)) {
    if (data.length === 0) {
      return '<span class="json-bracket">[]</span>';
    }

    const id = `arr_${Math.random().toString(36).slice(2, 9)}`;
    html += `<span class="json-toggle" data-target="${id}">▼</span>`;
    html += '<span class="json-bracket">[</span>\n';
    html += `<div id="${id}" class="json-collapsible">`;

    data.forEach((item, i) => {
      html += `${indentStr}  `;
      html += renderJson(item, indent + 1);
      if (i < data.length - 1) {
        html += '<span class="json-comma">,</span>';
      }
      html += '\n';
    });

    html += '</div>';
    html += `${indentStr}<span class="json-bracket">]</span>`;
  } else if (typeof data === 'object' && data !== null) {
    const keys = Object.keys(data);
    if (keys.length === 0) {
      return '<span class="json-bracket">{}</span>';
    }

    const id = `obj_${Math.random().toString(36).slice(2, 9)}`;
    html += `<span class="json-toggle" data-target="${id}">▼</span>`;
    html += '<span class="json-bracket">{</span>\n';
    html += `<div id="${id}" class="json-collapsible">`;

    keys.forEach((key, i) => {
      html += `${indentStr}  `;
      html += `<span class="json-key">"${escapeHtml(key)}"</span>: `;
      html += renderJson(data[key], indent + 1);
      if (i < keys.length - 1) {
        html += '<span class="json-comma">,</span>';
      }
      html += '\n';
    });

    html += '</div>';
    html += `${indentStr}<span class="json-bracket">}</span>`;
  } else if (typeof data === 'string') {
    html += `<span class="json-string">"${escapeHtml(data)}"</span>`;
  } else if (typeof data === 'number') {
    html += `<span class="json-number">${data}</span>`;
  } else if (typeof data === 'boolean') {
    html += `<span class="json-boolean">${data}</span>`;
  } else if (data === null) {
    html += '<span class="json-null">null</span>';
  }

  return html;
}

(function () {
  const contentType = document.contentType || '';
  const isJSONContentType = contentType.includes('application/json');

  /** @type {HTMLPreElement | null} */
  const pre = document.querySelector('body > pre');
  const bodyText = document.body?.innerText?.trim();
  if (!pre && !isJSONContentType) return;

  const jsonText = pre?.innerText || bodyText || '';
  if (!jsonText) return;

  let jsonData;
  try {
    jsonData = JSON.parse(jsonText);
  } catch (e) {
    return;
  }

  document.body.innerHTML = '';

  // @ts-ignore
  // eslint-disable-next-line no-undef
  GM_addStyle(`
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      font-size: 14px;
      line-height: 1.5;
      color: #333;
      background: #fff;
      padding: 20px;
    }

    @media (prefers-color-scheme: dark) {
      body {
        background: #1e1e1e;
        color: #d4d4d4;
      }
      .json-key { color: #9cdcfe !important; }
      .json-string { color: #ce9178 !important; }
      .json-number { color: #b5cea8 !important; }
      .json-boolean { color: #569cd6 !important; }
      .json-null { color: #569cd6 !important; }
      .json-toggle { color: #d4d4d4 !important; }
      .toolbar { background: #252526 !important; border-color: #3e3e42 !important; }
      .toolbar button {
        background: #3e3e42 !important;
        color: #d4d4d4 !important;
        border-color: #555 !important;
      }
      .toolbar button:hover { background: #505050 !important; }
    }

    .toolbar {
      position: sticky;
      top: 0;
      background: #f5f5f5;
      padding: 10px;
      border-bottom: 1px solid #ddd;
      margin: -20px -20px 20px -20px;
      display: flex;
      gap: 10px;
      align-items: center;
      z-index: 1000;
    }

    .toolbar button {
      padding: 6px 12px;
      border: 1px solid #ccc;
      border-radius: 4px;
      background: #fff;
      cursor: pointer;
      font-size: 13px;
    }

    .toolbar button:hover {
      background: #e9e9e9;
    }

    .json-container {
      font-family: "Monaco", "Menlo", "Consolas", monospace;
      white-space: pre-wrap;
      word-wrap: break-word;
    }

    .json-line {
      padding-left: 20px;
    }

    .json-key {
      color: #881391;
      font-weight: 500;
    }

    .json-string {
      color: #c41a16;
    }

    .json-number {
      color: #1c00cf;
    }

    .json-boolean {
      color: #0d22aa;
      font-weight: bold;
    }

    .json-null {
      color: #808080;
      font-style: italic;
    }

    .json-toggle {
      cursor: pointer;
      user-select: none;
      color: #666;
      display: inline-block;
      width: 16px;
      font-weight: bold;
    }

    .json-toggle:hover {
      color: #000;
    }

    .json-collapsed {
      display: none;
    }

    .json-bracket {
      color: #666;
      font-weight: bold;
    }

    .json-comma {
      color: #666;
    }
  `);

  const toolbar = document.createElement('div');
  toolbar.className = 'toolbar';
  toolbar.innerHTML = `
    <button id="expandAll">Expand All</button>
    <button id="collapseAll">Collapse All</button>
    <button id="copyJson">Copy JSON</button>
    <button id="downloadJson">Download</button>
    <span style="margin-left: auto; font-size: 12px; color: #666;">
      ${Object.keys(jsonData).length} keys
    </span>
  `;

  const container = document.createElement('div');
  container.className = 'json-container';

  document.body.appendChild(toolbar);
  document.body.appendChild(container);

  container.innerHTML = renderJson(jsonData);

  // collapse/expand
  container.addEventListener('click', (e) => {
    const toggle = e.target.closest('.json-toggle');
    if (!toggle) return;

    const targetId = toggle.dataset.target;
    const target = document.getElementById(targetId);
    if (!target) return;

    if (target.classList.contains('json-collapsed')) {
      target.classList.remove('json-collapsed');
      toggle.textContent = '▼';
    } else {
      target.classList.add('json-collapsed');
      toggle.textContent = '▶';
    }
  });

  // toolbar buttons
  document.getElementById('expandAll')?.addEventListener('click', () => {
    document.querySelectorAll('.json-collapsible').forEach((el) => {
      el.classList.remove('json-collapsed');
    });
    document.querySelectorAll('.json-toggle').forEach((el) => {
      el.textContent = '▼';
    });
  });

  document.getElementById('collapseAll')?.addEventListener('click', () => {
    document.querySelectorAll('.json-collapsible').forEach((el) => {
      el.classList.add('json-collapsed');
    });
    document.querySelectorAll('.json-toggle').forEach((el) => {
      el.textContent = '▶';
    });
  });

  document.getElementById('copyJson')?.addEventListener('click', () => {
    navigator.clipboard.writeText(JSON.stringify(jsonData, null, 2))
      .then(() => alert('JSON copied to clipboard!'))
      .catch((error) => alert(`Failed to copy: ${error}`));
  });

  document.getElementById('downloadJson')?.addEventListener('click', () => {
    const blob = new Blob([JSON.stringify(jsonData, null, 2)], {
      type: 'application/json',
    });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;

    let filename = window.location.pathname.split('/').pop() || 'download';
    if (!/\.json$/.test(filename)) {
      filename += '.json';
    }

    a.download = filename;
    a.click();
    URL.revokeObjectURL(url);
  });
}());