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-21 提交的版本。查看 最新版本

  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.0
  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. // @match https://github.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  11. // @run-at document-ready
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15.  
  16. const waitForFilelist = setInterval(() => {
  17. const fileListContainer = document.querySelector("div.Box > div.js-details-container.Details div")
  18.  
  19. if ( !fileListContainer ) return
  20.  
  21. const fileList = fileListContainer.children
  22.  
  23. if ( fileList < 1 ) return
  24.  
  25. appendButtons(fileList)
  26.  
  27. }, 1000)
  28.  
  29. function appendButtons(fileList) {
  30. for ( let i = 1; i < fileList.length; i++ ) {
  31. const file = fileList[i]
  32. const fileUrl = file.querySelector('div:nth-child(2) .js-navigation-open').href
  33. const rawFileUrl = fileUrl.replace('/blob/', '/raw/')
  34.  
  35. file.append(creatyCopyButton(rawFileUrl))
  36. }
  37. clearInterval(waitForFilelist)
  38. };
  39.  
  40. function creatyCopyButton(copyText) {
  41. const copy2clipboard = `
  42. <clipboard-copy aria-label="Copy" value="test value" data-view-component="true" class="" tabindex="0" role="button" title="Copy raw file url">
  43. <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
  44. <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>
  45. </svg>
  46. </clipboard-copy>`
  47.  
  48. const copyButton = document.createElement('div')
  49. copyButton.setAttribute('role', 'gridcell')
  50. copyButton.style = "margin-left: 10px;"
  51. copyButton.innerHTML = copy2clipboard
  52. copyButton.children[0].value = copyText
  53. copyButton.children[0].style = "cursor: pointer;"
  54.  
  55. return copyButton
  56. }