Button to copy the raw file URL | Github

Adds a button at the end of each file row to copy the raw file URL

目前为 2022-10-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Button to copy the raw file URL | Github
  3. // @name:de Button zum Kopieren der Raw-Datei URL | Github
  4. // @namespace https://greasyfork.org/users/928242
  5. // @version 1.1
  6. // @description Adds a button at the end of each file row to copy the raw file URL
  7. // @description:de Fügt am Ende jeder Dateizeile eine Schaltfläche zum Kopieren der Raw File URL hinzu
  8. // @author Kamikaze (https://github.com/Kamiikaze)
  9. // @supportURL https://github.com/Kamiikaze/Tampermonkey/issues
  10. // @match https://github.com/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  12. // @run-at document-ready
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16.  
  17. const waitForFilelist = setInterval(() => {
  18. const fileListContainer = document.querySelector("div.Box > div.js-details-container.Details div")
  19.  
  20. if ( !fileListContainer ) return
  21.  
  22. const fileList = fileListContainer.children
  23.  
  24. if ( fileList < 1 ) return
  25.  
  26. appendButtons(fileList)
  27.  
  28. }, 1000)
  29.  
  30. function appendButtons(fileList) {
  31. for ( let i = 0; i < fileList.length; i++ ) {
  32. const file = fileList[i]
  33.  
  34. if (
  35. file.classList.contains("sr-only") ||
  36. file.childElementCount !== 4
  37. ) continue;
  38.  
  39. const fileUrl = file.querySelector('div:nth-child(2) .js-navigation-open').href
  40. const rawFileUrl = fileUrl.replace('/blob/', '/raw/')
  41.  
  42. file.append(creatyCopyButton(rawFileUrl))
  43. }
  44. // clearInterval(waitForFilelist)
  45. };
  46.  
  47. function creatyCopyButton(copyText) {
  48. const copy2clipboard = `
  49. <clipboard-copy aria-label="Copy" value="test value" data-view-component="true" class="" tabindex="0" role="button" title="Copy raw file url">
  50. <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
  51. <path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path>
  52. </svg>
  53. </clipboard-copy>`
  54.  
  55. const copyButton = document.createElement('div')
  56. copyButton.setAttribute('role', 'gridcell')
  57. copyButton.style = "margin-left: 10px;"
  58. copyButton.innerHTML = copy2clipboard
  59. copyButton.children[0].value = copyText
  60. copyButton.children[0].style = "cursor: pointer;"
  61.  
  62. return copyButton
  63. }