YapiCopy

Yapi接口数据一键复制

Versione datata 28/07/2023. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         YapiCopy
// @namespace    http://tampermonkey.net/
// @version      0.2.2
// @description  Yapi接口数据一键复制
// @author       Enjoy
// @icon         https://foruda.gitee.com/avatar/1671100286067517749/4867929_enjoy_li_1671100285.png!avatar60
// @match        *://yapi.*.com/project/*/interface/api/*
// @grant        GM_addElement
// @grant        GM_addStyle
// @grant        GM_setClipboard
// @license      GPL License
// ==/UserScript==

//ES6+ =>ES5  https://babel.docschina.org/repl/

run(document);
function run(document) {
  createBtn();
  return;
  function createBtn() {
    var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var _options$selector = options.selector,
      selector = _options$selector === void 0 ? 'YapiCopy' : _options$selector;
    var btnDom = document.querySelector("#".concat(selector));
    if (btnDom) return btnDom;
    btnDom = document.createElement('div');
    btnDom.innerHTML = "\n    <div title='apiTxt \u4E00\u952E\u590D\u5236'>\uF0C5'</div>\n    <div title='paramsTxt \u4E00\u952E\u590D\u5236'>\uEBCC</div>\n    <style>\n    #YapiCopy {\n      width: 15px;\n      opacity: 0.2;\n      background: #0b7d1e;\n      font-weight: 800;\n      border-radius: 5px 0 0 5px;\n      cursor: pointer;\n      color: #fff;\n      transition: all 0.2s;\n      font-size: 18px;\n      position: fixed;\n      right: 0;\n      top: 50vh;\n    }\n\n    #YapiCopy:hover {\n      opacity: 0.5;\n      width: 35px;\n      height: 70px;\n    }\n\n    #YapiCopy>div {\n      height: 35px;\n      line-height: 35px;\n      text-align: center;\n      border-radius: 5px 0 0 5px;\n    }\n\n    #YapiCopy>div:hover {\n      background-color: #777d0b;\n    }\n   </style>\n      ";
    btnDom.setAttribute('id', selector);
    btnDom.setAttribute('title', '一键复制');
    document.body.appendChild(btnDom);
    btnDom.addEventListener('click', function (e) {
      if (e.target.title === 'apiTxt 一键复制') {
        doCopy(cellecteApiTxt());
      } else {
        doCopy(cellecteParamsTxt());
      }
    });
  }
  function doCopy(newValue) {
    var selector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'textarea';
    var textarea = document.querySelector("#".concat(selector));
    if (!textarea) {
      textarea = document.createElement('textarea');
      textarea.style.cssText = 'position: absolute;left: -500px;top: -500px;max-width: 50px;opacity: 0;';
      document.body.appendChild(textarea);
    }
    textarea.value = newValue;
    textarea.select();
    setTimeout(function () {
      document.execCommand('Copy');
    }, 200);
  }
  function cellecteApiTxt() {
    var _values$, _values$2;
    var desc = document.querySelector('.colName').innerText || '';
    var pageUrl = location.href;
    var values = document.querySelectorAll('.colValue');
    var method = ((_values$ = values[3]) === null || _values$ === void 0 ? void 0 : _values$.innerText.toLocaleLowerCase()) || '';
    var apiUrl = ((_values$2 = values[4]) === null || _values$2 === void 0 ? void 0 : _values$2.innerText) || '';
    var template = "/**\n* @description ".concat(desc, "\n* @see ").concat(decodeURIComponent(pageUrl), "\n* @param { data } data\n*/\nexport function ").concat(apiUrl.replace(/^.*\//, ''), "(data) {\n  return http.").concat(method, "('").concat(apiUrl, "',data)\n}");
    console.log(template);
    return template;
  }
  function cellecteParamsTxt() {
    var dataInnerText = document.querySelectorAll('table tbody')[1].innerText || '';
    var paramsTxt = formatParamsTxt(dataInnerText);
    var template = "// \u53C2\u6570\u8BF4\u660E\nlet data = { ".concat(paramsTxt, "\n}");
    console.log(template);
    return template;
  }
  function formatParamsTxt(txt) {
    var filter1 = ['\n', '非必须', '必须', 'format: int32', '\t'];
    var filter2 = ['integer', 'string'];
    filter1.forEach(function (item) {
      txt = txt.replace(new RegExp(item, 'g'), '');
    });
    filter2.forEach(function (item) {
      txt = txt.replace(new RegExp(item, 'g'), ":'".concat(item.slice(0, 1), "',//"));
    });
    txt = txt.replace(/([a-zA-Z]+:['a-zA-Z]+,\/\/)/g, '\n $1');
    return txt;
  }
}