Skip to content

Instantly share code, notes, and snippets.

@5minpause
Created April 8, 2025 09:25
Show Gist options
  • Save 5minpause/fac6f1b58cd6bfe9a1416a8314bb379a to your computer and use it in GitHub Desktop.
Save 5minpause/fac6f1b58cd6bfe9a1416a8314bb379a to your computer and use it in GitHub Desktop.
Draft to Micro.blog
let apiToken = "api-token";
let blogId = "blog-id"
let alert = new Alert()
const contentText = alert.addTextField()
alert.title = "Website als Entwurf in MB speichern"
alert.message = args.urls[0]
alert.addCancelAction("OK") // response -1
alert.addDestructiveAction("Abbrechen") // response 0
const alertResponse = await alert.presentAlert()
if (alertResponse === -1) {
let urls = args.urls[0];
let content = contentText.text;
const rawData = {
"h": "entry",
"content": `${urls}\n\n${content}`,
"mp-destination": blogId,
"post-status": "draft"
}
const s = Object.entries(rawData)
.map(([key, val]) => encodeURIComponent(key) + "=" + encodeURIComponent(val))
.join("&");
// Debugging
// console.log(s)
// alert = new Alert()
// alert.title = "Message"
// alert.message = s
// alert.addCancelAction("OK")
// await alert.presentAlert()
// HTTP POST Request
const postDraft = async (s) => {
let xhr = new Request("https://micro.blog/micropub");
xhr.headers = {"Authorization": `Bearer ${apiToken}`, "Content-Type": "application/x-www-form-urlencoded"};
xhr.method = "POST"
xhr.body = s;
return await xhr.loadString()
}
const response = await postDraft(s);
// Debugging
// console.log(response)
// alert = new Alert()
// alert.title = "Response"
// alert.message = response
// alert.addCancelAction("OK")
// await alert.presentAlert()
}
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment