echarts option Json 生成

echarts option json 生成

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         echarts option Json 生成
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  echarts option json 生成
// @author       aries.zhou
// @match        https://echarts.apache.org/examples/en/editor.html?**
// @match        https://echarts.apache.org/examples/zh/editor.html?**
// @icon         https://www.google.com/s2/favicons?domain=apache.org
// @grant    GM_setClipboard
// ==/UserScript==


function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

window.addEventListener('load', async function () {
  await sleep(1000);
  const container = document.getElementsByClassName('left-panel').item(0);
  //add text
  {
    const newText = document.createElement("textarea");
    newText.style.height = '100px';
    newText.style.width = '500px';
    newText.cols = 10;
    newText.value = '数据';
    newText.className = 'newoptionjson';
    newText.id = 'newoptionjson';
    container.appendChild(newText);
  }

//set text value
  {
    const textBox = document.getElementsByClassName('newoptionjson').item(0);
    var chart = echarts.getInstanceByDom(document.getElementsByClassName('chart-container').item(0));

    try {
      textBox.value = JSON.stringify(chart.getOption());
    } catch (exception_var) {
      console.log(exception_var);
      JSON.safeStringify = (obj, indent = 2) => {
        let cache = [];
        const retVal = JSON.stringify(
          obj,
          (key, value) =>
            typeof value === "object" && value !== null
              ? cache.includes(value)
                ? undefined // Duplicate reference found, discard key
                : cache.push(value) && value // Store value in our collection
              : value,
          indent
        );
        cache = null;
        return retVal;
      };
      textBox.value = JSON.safeStringify(chart.getOption());
    } finally {
      console.log("finally");
    }
  }
  //copy
  {
    await sleep(1000);
    document.getElementById('newoptionjson').select();
    var targetText = document.getElementsByClassName('newoptionjson').item(0);
    GM_setClipboard(targetText.value);
  }

}, false);