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

Function run

github-jsdelivr-link/github-jsdelivr-link.user.js:104–151  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

102 const iconUrl = 'https://camo.githubusercontent.com/6a5d2046028682a99b5fa88ef0f3399c9bced1d514179686a3973a323bccbf44/68747470733a2f2f7777772e6a7364656c6976722e636f6d2f69636f6e5f323536783235362e706e67'
103
104 function run() {
105 const copyPathButton = document.querySelector('[aria-label="Copy path"]')
106 if (!copyPathButton) {
107 return
108 }
109
110 const url = new URL(window.location.href)
111 const { pathname } = url
112 const isFile = pathname.includes('/blob/')
113 const isFolder = pathname.includes('/tree/')
114
115 let link
116 if (isFolder) {
117 const index = pathname.indexOf('/tree/')
118 const author = pathname.slice(1, index)
119 let rest = pathname.slice(index + 6)
120 const version = rest.slice(0, rest.indexOf('/'))
121 const filepath = rest.slice(rest.indexOf('/'))
122 link = `https://cdn.jsdelivr.net/gh/${author}@${version}${filepath}/`
123 } else if (isFile) {
124 const index = pathname.indexOf('/blob/')
125 const author = pathname.slice(1, index)
126 let rest = pathname.slice(index + 6)
127 const version = rest.slice(0, rest.indexOf('/'))
128 const filepath = rest.slice(rest.indexOf('/'))
129 link = `https://cdn.jsdelivr.net/gh/${author}@${version}${filepath}`
130 } else {
131 return
132 }
133
134 let actionButton = document.querySelector('button[aria-label="Open JsDelivr Link"]')
135 if (!actionButton) {
136 const copyPathButtonWrapper = copyPathButton.parentElement
137 const container = copyPathButtonWrapper.parentElement
138 const actionNode = copyPathButtonWrapper.cloneNode(true)
139 actionButton = actionNode.querySelector('button')
140 actionButton.setAttribute('title', 'Open JsDelivr Link')
141 actionButton.setAttribute('aria-label', 'Open JsDelivr Link')
142 actionButton.innerHTML = `<img src=${iconUrl} style="height: 16px;" />`
143 container.appendChild(actionNode)
144 Array.from(actionNode.children).forEach(child => child.getAttribute('title') !== 'Open JsDelivr Link' && actionNode.removeChild(child))
145 }
146
147
148 actionButton.onclick = () => {
149 window.open(link)
150 }
151 }
152
153 setInterval((event) => {
154 run()

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected