自动复制Github仓库名

删除github仓库时,自动生成一个按钮复制仓库名

  1. // ==UserScript==
  2. // @name Auto-Copy-Github-Repository-Name
  3. // @name:zh-CN 自动复制Github仓库名
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.0.3
  6. // @description add an btn that can copy github repository name when deleteing a repository
  7. // @description:zh-CN 删除github仓库时,自动生成一个按钮复制仓库名
  8. // @author kkopite
  9. // @match https://github.com/*
  10. // @require https://code.jquery.com/jquery-3.6.0.slim.min.js
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict'
  17.  
  18. waitForKeyElements('h2#danger-zone', addBtn)
  19. function addBtn() {
  20. const form = document.querySelector('[aria-label="Delete repository"] form')
  21. const parent = form.parentElement
  22. if (parent.querySelector('#copy-repository-name')) return
  23. const btn = document.createElement('button')
  24. btn.id = 'copy-repository-name'
  25. parent.insertBefore(btn, form)
  26. btn.innerText = 'Copy'
  27. btn.classList.add('btn', 'btn-danger', 'm-1')
  28. btn.addEventListener('click', copy)
  29. }
  30. function copy() {
  31. const pathname = window.location.pathname
  32. const index = pathname.lastIndexOf('/settings')
  33. const name = pathname.slice(1, index)
  34. navigator.clipboard.writeText(name)
  35. }
  36.  
  37. // https://gist.github.com/BrockA/2625891
  38. function waitForKeyElements(
  39. selectorTxt, /* Required: The jQuery selector string that
  40. specifies the desired element(s).
  41. */
  42. actionFunction, /* Required: The code to run when elements are
  43. found. It is passed a jNode to the matched
  44. element.
  45. */
  46. bWaitOnce, /* Optional: If false, will continue to scan for
  47. new elements even after the first match is
  48. found.
  49. */
  50. iframeSelector /* Optional: If set, identifies the iframe to
  51. search.
  52. */
  53. ) {
  54. var targetNodes, btargetsFound
  55.  
  56. if (typeof iframeSelector == "undefined")
  57. targetNodes = $(selectorTxt)
  58. else
  59. targetNodes = $(iframeSelector).contents()
  60. .find(selectorTxt)
  61.  
  62. if (targetNodes && targetNodes.length > 0) {
  63. btargetsFound = true
  64. /*--- Found target node(s). Go through each and act if they
  65. are new.
  66. */
  67. targetNodes.each(function () {
  68. var jThis = $(this)
  69. var alreadyFound = jThis.data('alreadyFound') || false
  70.  
  71. if (!alreadyFound) {
  72. //--- Call the payload function.
  73. var cancelFound = actionFunction(jThis)
  74. if (cancelFound)
  75. btargetsFound = false
  76. else
  77. jThis.data('alreadyFound', true)
  78. }
  79. })
  80. }
  81. else {
  82. btargetsFound = false
  83. }
  84.  
  85. //--- Get the timer-control variable for this selector.
  86. var controlObj = waitForKeyElements.controlObj || {}
  87. var controlKey = selectorTxt.replace(/[^\w]/g, "_")
  88. var timeControl = controlObj[controlKey]
  89.  
  90. //--- Now set or clear the timer as appropriate.
  91. if (btargetsFound && bWaitOnce && timeControl) {
  92. //--- The only condition where we need to clear the timer.
  93. clearInterval(timeControl)
  94. delete controlObj[controlKey]
  95. }
  96. else {
  97. //--- Set a timer, if needed.
  98. if (!timeControl) {
  99. timeControl = setInterval(function () {
  100. waitForKeyElements(selectorTxt,
  101. actionFunction,
  102. bWaitOnce,
  103. iframeSelector
  104. )
  105. },
  106. 300
  107. )
  108. controlObj[controlKey] = timeControl
  109. }
  110. }
  111. waitForKeyElements.controlObj = controlObj
  112. }
  113. })()