echarts option Json 生成

echarts option json 生成

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);