Skip to content

Instantly share code, notes, and snippets.

@rynomad
Created April 8, 2023 20:49
Show Gist options
  • Save rynomad/37b012d6d33e364ec93b241b79b9ea5b to your computer and use it in GitHub Desktop.
Save rynomad/37b012d6d33e364ec93b241b79b9ea5b to your computer and use it in GitHub Desktop.
My Gist
function publishGist(button) {
if (localStorage.getItem("gistTokenName")) {
// get the gist token and publish the gist
const tokenName = localStorage.getItem("gistTokenName");
const tokenValue = localStorage.getItem("gistTokenValue");
const codeElement = button.nextElementSibling;
const gistPayload = {
description: "My Gist",
public: true,
files: {
"my-file.js": {
content: codeElement.textContent,
},
},
};
const requestOptions = {
method: "POST",
headers: {
Authorization: `token ${tokenValue}`,
"Content-Type": "application/json",
},
body: JSON.stringify(gistPayload),
};
fetch("https://api.github.com/gists", requestOptions)
.then((response) => response.json())
.then((data) => {
console.log(`Gist URL: ${data.html_url}`);
})
.catch((error) => {
console.error(error);
});
} else {
// prompt the user for a gist token
const tokenName = prompt("Please enter your GitHub Gist token name:");
const tokenValue = prompt("Please enter your GitHub Gist token value:");
localStorage.setItem("gistTokenName", tokenName);
localStorage.setItem("gistTokenValue", tokenValue);
publishGist(button);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment