Skip to content

Instantly share code, notes, and snippets.

@brandonhill
Created October 7, 2022 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonhill/84c3cb5007036da016cf4f184a6824e6 to your computer and use it in GitHub Desktop.
Save brandonhill/84c3cb5007036da016cf4f184a6824e6 to your computer and use it in GitHub Desktop.
Loads javascript gists into an HTML page.
const gist = async (id, d = document, s = [
'//api.github.com/gists/',
'?callback=',
'__gg',
'script']) => {
const load = (raw, src) => {
const el = d.createElement(s[3])
raw && (el.innerHTML = raw) || (el.src = src)
d.head.appendChild(el)
return el
}
return new Promise((resolve, reject) => {
try {
let el = load(undefined, s[0] + id + s[1] + s[2])
window[s[2]] = ({ data, meta: { status } }) => {
el.remove()
delete window[s[2]]
status !== 200 ? reject(data)
: Object.values(data.files).forEach(async _ => {
el = load(await (await fetch(_.raw_url)).text())
el.remove() // optional, not tested on all browsers
resolve()
})
}
} catch (e) {
reject(e)
}
})
}
/* example:
(async () => {
try {
await gist('b0aefe09a618164fd4137a67feae5393')
// do the things
} catch (e) {
console.error('unable to load gist', e)
}
})()
//*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment