Skip to content

Instantly share code, notes, and snippets.

@SeanGallen
Last active December 25, 2022 00:19
Show Gist options
  • Save SeanGallen/83f0228a277f8bc3801ea977ae0515d0 to your computer and use it in GitHub Desktop.
Save SeanGallen/83f0228a277f8bc3801ea977ae0515d0 to your computer and use it in GitHub Desktop.
ZX script to run certain JS libraries in the Chrome DevTools for quick demonstrations and testing
#!/usr/bin/env zx
/*
This is a fun little tool for some scratch paper purposes.
It calls cdnjs for the JS libraries you are seeking and allows
you to embed it in the Chrome DevTools' Console section.
This way you can work with the library's tools fast and locally.
Once the page is refreshed you have to load it again.
*/
/*
To run
install zx:
zx ./scratchPaperDemo.mjs
or
chmod +x ./scratchPaperDemo.mjs
./scratchPaperDemo.mjs
*/
/*
Notes on how zx handles arguments:
0 argument: node's location in your system,
1st argument: zx's location in your system,
2nd argument: fileName,
3rd argument: library you are looking for,
.
.
nth argument,
*/
const LIBRARY_SEEKING = process.argv[3];
const cndResponse = await fetch(
`https://api.cdnjs.com/libraries/${LIBRARY_SEEKING}?fields=name,filename,version`
);
if (cndResponse.ok) {
const { name, filename, version } = await cndResponse.json();
await $`echo "const script = document.createElement('script');
script.src='https://cdnjs.cloudflare.com/ajax/libs/${name}/${version}/${filename}';
document.head.appendChild(script);" | pbcopy`;
await $`open -a 'Google Chrome' about:blank`;
// Open DevTools in the blank Chrome page and paste in the script to add the library.
// Now, you should be able to run the library on the Chrome page.
console.log("Success ");
} else {
console.log("There was an issue: ", cndResponse.status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment