tweet-card-upload
https://gyazo.com/f7d882280f56ec1ee13285e43b03a885
code:usage.js
code:script.js
scrapbox.PopupMenu.addButton({
title: "tweet-card (upload)",
onClick: (text) => {
if (!window.get_tweet_card) {
alert("UserScript is not installed?")
return
}
if (!window.tweet_card_upload) {
window.tweet_card_upload = new Map();
}
const gyazoClientId =
"e2bd725244baa768eb100126fa2cd85910445778a25ba7ef1328608750b070d5";
const tweetcard =
const m = text.match(/twitter.com\/(.+)\/status\/(\d+)/);
if (!m) return text;
if (window.tweet_card_upload.has(m2)) { return window.tweet_card_upload.get(m2); } else {
(async () => {
let imageData;
try {
const req = await window.get_tweet_card(${tweetcard}/${m[2]}.jpg?lang=ja&tz=9);
if (req.status !== 200) {
alert("Image fetch failed(!=200)");
return;
}
imageData = await new Promise((res, rej) => {
const reader = new FileReader();
reader.onerror = rej;
reader.onload = () => {
res(reader.result);
};
reader.readAsDataURL(req.response);
});
} catch (e) {
console.error(e);
alert("Image fetch failed!")
return;
}
const tweetUrl = https://twitter.com/${m[1]}/status/${m[2]}
// upload to gyazo
try {
const formData = new FormData();
formData.append("client_id", gyazoClientId);
formData.append("referer_url", tweetUrl);
formData.append("image_url", imageData);
const easyAuth = await fetch(
https://upload.gyazo.com/api/upload/easy_auth,
{
method: "POST",
mode: "cors",
credentials: "include",
body: formData,
}
);
const uploadResult = await easyAuth.json();
const getImage = await fetch(uploadResult.get_image_url, {
mode: "cors",
credentials: "include",
});
window.tweet_card_upload.set(
[${getImage.url} ${tweetUrl}]
);
alert(Upload done: ${getImage.url});
} catch (e) {
console.error(e);
return;
}
})();
return text;
}
},
});