YapiCopy

Yapi接口数据一键复制

2023-07-28 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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


function cellecteData() {
  let desc = document.querySelector('.colName').innerText || ''
  let values = document.querySelectorAll('.colValue')
  console.log('values=',values)
  let method = values[3]?.innerText.toLocaleLowerCase() || ''
  let apiUrl = values[4].innerText || ''
  let paramsStr = document.querySelectorAll('table')[1].innerText || ''

    ;['\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.replace(/(,\/\/[^a-zA-Z]+)/g,'$1 \n')

  let template = `
        /**
         * @description ${desc}
         * @see ${apiUrl}
         * @param { {
         ${paramsStr}
         } } data
         */
        export function ${apiUrl.replace(/^.*\//,'')}(data) {
          return http.${method}('${apiUrl}',data)
        }
        `
  return template
}

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: 70px;
    }

    #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())
  })
}

createBtn()


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)
}