Created
April 8, 2023 20:49
-
-
Save rynomad/1f917b92af3ba625c3d9b9c348643ab8 to your computer and use it in GitHub Desktop.
just a test
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
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