YapiCopy

Yapi接口数据一键复制

Tính đến 28-07-2023. Xem phiên bản mới nhất.

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 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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

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

// ==UserScript==
// @name         YapiCopy
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @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==
"use strict";

createBtn();

function cellecteData() {
  var _values$, _values$2;
  let paramsInnerText = document.querySelectorAll('table tbody')[1].innerText || '';
  let paramsStr = formatParamsStr(paramsInnerText);
  let desc = document.querySelector('.colName').innerText || '';
  let pageUrl = location.href;
  let values = document.querySelectorAll('.colValue');
  let method = ((_values$ = values[3]) === null || _values$ === void 0 ? void 0 : _values$.innerText.toLocaleLowerCase()) || '';
  let apiUrl = ((_values$2 = values[4]) === null || _values$2 === void 0 ? void 0 : _values$2.innerText) || '';
  let template = `
/* // 参数说明
 let  params = { ${paramsStr}
  }
*/

/**
* @description ${desc}
* @see ${pageUrl}
* @param { params } params
*/
export function ${apiUrl.replace(/^.*\//, '')} (params) {
  return http.${method} ('${apiUrl}',params)
}`;
  return template;
}
function formatParamsStr(paramsStr) {
  ;
  ['\n', '非必须', '必须', 'format: int32', '\t'].forEach(item => {
    paramsStr = paramsStr.replace(new RegExp(item, 'g'), '');
  });
  ['integer', 'string'].forEach(item => {
    paramsStr = paramsStr.replace(new RegExp(item, 'g'), `:'${item}',// `);
  });
  paramsStr = paramsStr.replace(/([a-zA-Z]+:['a-zA-Z]+,\/\/)/g, '\n $1');
  return paramsStr;
}
function createBtn(options = {}) {
  let {
    selector = 'yapiShuctCopy'
  } = options;
  let btnDom = document.querySelector(`#${selector}`);
  if (btnDom) return btnDom;
  btnDom = document.createElement('div');
  btnDom.innerHTML = `
  \uf0c5
  <style>
    #yapiShuctCopy {
      width: 20px;
      height: 35px;
      opacity: 0.2;
      background: #0b7d1e;
      font-weight: 800;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 35px 0 0 35px;
      cursor: pointer;
      color: #fff;
      transition: all 0.2s;
      font-size: 18px;
      position: fixed;
      right: 0;
      top: 50vh;
    }

    #yapiShuctCopy:hover {
      opacity: 0.5;
      width: 35px;
    }
  `;
  btnDom.setAttribute('id', selector);
  btnDom.setAttribute('title', '一键复制');
  // btnDom.style.cssText = ''
  document.body.appendChild(btnDom);
  btnDom.addEventListener('click', () => {
    doCopy(cellecteData());
  });
}

function doCopy(newValue, selector = 'textarea') {
  let textarea = document.querySelector(`#${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(() => {
    document.execCommand('Copy');
  }, 200);
}