Skip to content

Instantly share code, notes, and snippets.

@dzharii
Created February 2, 2021 05:03
Show Gist options
  • Save dzharii/451af80f1274e4d4c0b529d050f27594 to your computer and use it in GitHub Desktop.
Save dzharii/451af80f1274e4d4c0b529d050f27594 to your computer and use it in GitHub Desktop.
if (typeof process !== 'undefined' && process.exit)
(async () => {
const fs = require('fs').promises;
let cont = (await fs.readFile(__filename)).toString();
cont = cont.replace(/^.+?(?=\(\(d\) => \{)/s, '');
cont = cont.replace(/(?<!(?:function|typeof|const|let))\s+(?=(?:[^\'"]*[\'"][^\'"]*[\'"])*[^\'"]*$)/gs, '');
console.log('javascript:' + cont);
process.exit(0);
})();
else
((d) => {
function update() {
fetch("https://api.giphy.com/v1/gifs/random?api_key=y8CpAcHAapCxnArZIrZHCkSktr8y3IcQ&tag=cat")
.catch(err => console.error('Unable to fetch cats', err))
.then(rsp => rsp.text())
.catch(err => console.error('Unable to get cats response test', err))
.then(txt => JSON.parse(txt))
.catch(err => console.error('Unable to parse cats response json', err))
.then(json => ['data', 'images', 'original', 'url'].reduce((a, n) => a ? a[n] : undefined, json))
.then(url => {
d.body.innerHTML = tmpl(url);
const btnCatz = d.getElementById('catz');
btnCatz.addEventListener('click', update);
});
}
function tmpl(url) {
return [
'<h1>Random Cats</h1>',
'<button id="catz">More Please!</button>',
'<div>',
url ? '<img src="' + url+ '">' : '<p>Failed to load cats</p>',
'</div>'
].join('');
}
d.body = d.createElement('body');
update();
})(typeof document !== 'undefined' ? document : {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment