Skip to content

Instantly share code, notes, and snippets.

@Saber2pr
Created September 20, 2021 07:25
Show Gist options
  • Save Saber2pr/5e8dab44fc89cf6082e9e8292d8e2341 to your computer and use it in GitHub Desktop.
Save Saber2pr/5e8dab44fc89cf6082e9e8292d8e2341 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Gh Jsdelivr
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://github.com/*/*
// @icon https://www.google.com/s2/favicons?domain=github.com
// @grant none
// ==/UserScript==
;(function () {
const main = () => {
if (location.search !== '?cdn') return
const user = document.querySelector('.author a').textContent
const repo = document.querySelector('[itemprop=name] a').textContent
const branch = document.querySelector(
'.css-truncate-target[data-menu-button]'
).textContent
const files = Array.from(
document.querySelectorAll('.js-navigation-open[title]')
)
files.forEach(file => {
const a = document.createElement('a')
a.href = `https://cdn.jsdelivr.net/gh/${user}/${repo}@${branch}/${encodeURIComponent(
file.getAttribute('title')
)}`
a.target = '_blank'
a.textContent = 'cdn'
a.style.marginRight = '8px'
file.before(a)
})
}
const waitEl = (callback, ...selectors) => {
let index = 0
const handle = setInterval(() => {
if (selectors.every(selector => !!document.querySelector(selector))) {
clearInterval(handle)
callback()
}
index++
if (index >= 10) {
clearInterval(handle)
}
}, 1000)
}
waitEl(
main,
'.author a',
'[itemprop=name] a',
'.css-truncate-target[data-menu-button]',
'.js-navigation-open[title]'
)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment