1、安装谷歌扩展插件 油猴 Tampermonkey
2、安装脚本:
// ==UserScript==
// @name 复制swagger中的url
// @namespace https://plushine.cn
// @version 1.1
// @description 添加复制按钮,点击即可复制url到剪切板
// @author XJHui
// @match https://*/swagger/index.html*
// @grant GM_log
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_deleteValue
// @license MIT License
// @require https://code.jquery.com/jquery-2.1.4.min.js
// ==/UserScript==
(function() {
'use strict';
// Your code here...
// 感谢:https://greasyfork.org/zh-CN/scripts/373608-swagger%E6%B7%BB%E5%8A%A0%E5%A4%8D%E5%88%B6url%E6%8C%89%E9%92%AE
setTimeout(function (){
$(".opblock-summary-path").after(
$("<button>复制</button>").css("cursor","pointer").css("background","pink").css("padding","4px").css("border-radius","4px").css("border-color","pink").click(function (){
// 获取接口文本
var text=this.parentNode.children[1].dataset.path;
// 调用函数,复制到剪切板
copy(text);
// 提示复制成功
$(this).text("复制成功!");
// 定时改变显示文字
setTimeout(() => {
$(this).text("复制");
}, 1000);
}));
}, 1000);
// 感谢:https://www.jianshu.com/p/154f04482360
var copy = function (str) {
var div = document.createElement("div");
div.innerHTML = '<span>' + str + '</span>';
document.body.appendChild(div);
var range = document.createRange();
var selection = window.getSelection();
selection.removeAllRanges();
range.selectNodeContents(div);
selection.addRange(range);
document.execCommand('copy');
selection.removeAllRanges();
document.body.removeChild(div);
};
})();
需要注意的点:
- @require处 使用的jQuery记得引入
- @match处 修改对应自己的 swagger 网站匹配地址
- 需要根据自己的swagger网站修改类名和获取接口文本处的代码