Created
October 7, 2022 07:00
-
-
Save brandonhill/84c3cb5007036da016cf4f184a6824e6 to your computer and use it in GitHub Desktop.
Loads javascript gists into an HTML page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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