MCPcopy Create free account
hub / github.com/ChinaGodMan/UserScripts / addCopyButton

Function addCopyButton

github-clone-button/github-clone-button.user.js:406–452  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

404 }
405
406 function addCopyButton() {
407 const cmd = getCloneCmd()
408 if (!cmd) return
409 const codeBtn = Array.from(document.querySelectorAll('button[class^="prc-Button-ButtonBase-"]')).find(
410 btn => ['代码', 'Code'].includes(btn.textContent.trim())
411 )
412 if (!codeBtn) return
413 if (document.getElementById('copy-clone-submodules-btn')) return
414
415 // SVG字符串多行可读
416 const svg = `
417<svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;">
418 <path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path>
419 <path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>
420</svg>
421 `.trim()
422
423 // 克隆Code按钮
424 const copyBtn = codeBtn.cloneNode(true)
425 copyBtn.id = 'copy-clone-submodules-btn'
426
427 // 替换内容为SVG
428 const contentSpan = copyBtn.querySelector('[data-component="buttonContent"]')
429 if (contentSpan) {
430 contentSpan.innerHTML = svg
431 } else {
432 copyBtn.innerHTML = svg
433 }
434
435 // 设置title为命令内容
436 copyBtn.title = cmd
437
438 // 移除aria-haspopup等下拉相关属性
439 copyBtn.removeAttribute('aria-haspopup')
440 copyBtn.removeAttribute('aria-expanded')
441 copyBtn.removeAttribute('aria-describedby')
442
443 // 绑定复制事件
444 copyBtn.addEventListener('click', function (e) {
445 e.preventDefault()
446 e.stopPropagation()
447 copyCloneCmd(cmd)
448 })
449
450 // 插入到Code按钮后
451 codeBtn.parentNode.insertBefore(copyBtn, codeBtn.nextSibling)
452 }
453
454 // 监听页面变化(支持pjax和动态加载)
455 const observer = new MutationObserver(addCopyButton)

Callers 1

Calls 2

getCloneCmdFunction · 0.85
copyCloneCmdFunction · 0.85

Tested by

no test coverage detected