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

Function downHandler

github-gist-copier/github-gist-copier.user.js:210–251  ·  view source on GitHub ↗
(e, element)

Source from the content-addressed store, hash-verified

208 })
209 }
210 const downHandler = (e, element) => {
211 if (timeoutId) {
212 return
213 }
214 e.preventDefault()
215 const gistName = element.parentElement.querySelector('.gist-blob-name').innerText
216 const rawUrl = rawButton.href
217 if (gistName.slice(-8) === '.user.js') {
218 const a = document.createElement('a')
219 a.href = rawUrl
220 a.click()
221 return
222 }
223 GM_xmlhttpRequest({
224 method: 'GET',
225 url: rawUrl,
226 onload: function (response) {
227 if (response.status === 200) {
228 const blob = new Blob([response.responseText], {
229 type: 'text/plain'
230 })
231 const url = URL.createObjectURL(blob)
232 const a = document.createElement('a')
233 a.href = url
234 a.download = gistName
235 a.click()
236 downIcon.style.display = 'none'
237 downcheckIcon.style.display = 'inline-block'
238 timeoutId = setTimeout(() => {
239 downIcon.style.display = 'inline-block'
240 downcheckIcon.style.display = 'none'
241 timeoutId = null
242 }, 500)
243 } else {
244 console.error('Download Failed')
245 }
246 },
247 onerror: function (error) {
248 timeoutId = null
249 }
250 })
251 }
252
253 buttonToDown.addEventListener('click', (e) => downHandler(e, fileActionElement))
254 button.addEventListener('click', copyHandler)

Callers 1

createCopyButtonFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected