Auto-Copy-Github-Repository-Name

add an btn that can copy github repository name when deleteing a repository

当前为 2022-05-13 提交的版本,查看 最新版本

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