画像のupload先がscrapboxのときのみ、自前でGyazoへuploadするUserScript
文脈のりしろ基素.icon
ScrapboxのプロジェクトをGyazoアップロードに設定していたらGyazoがないと云々でアップできないと言われる
Scrapboxにアップするように設定した場合に自分の画像はGyazoにあがらなくて不便になるかも?と思う
つまり画像のupload先がscrapboxのときのみ、自前でGyazoへuploadする機能を作ればいいわけかtakker.icon あ、D&D対応忘れてた
多分document.querySelector(".drag-and-drop-enter")!.addEventListener("drop", (e) => { const = items = e.dataTransfer.items;/* ... */})を追加すれば行けるはず
code:paste-gyazo.ts
/// <reference no-default-lib="true"/>
/// <reference lib="esnext"/>
/// <reference lib="dom"/>
/// <reference lib="dom.iterable"/>
import {
getGyazoToken,
getProject,
import {
insertText,
textInput,
useStatusBar,
declare const scrapbox: Scrapbox;
const projectResult = await getProject(scrapbox.Project.name);
if (!projectResult.ok) {
alert(Failed to get the information of "${scrapbox.Project.name}");
throw Error(projectResult.value.name);
}
const project = projectResult.value;
if (project.isMember && project.uploadImaegTo === "gcs") {
const result = await getGyazoToken();
if (!result.ok) {
alert(
"You haven't logged in Gyazo yet, so you can only upload images to scrapbox.io.",
);
} else {
const accessToken = result.value;
if (!accessToken) {
alert(
"You haven't connect Gyazo to scrapbox.io yet, so you can only upload images to scrapbox.io.",
);
} else {
textInput()!.addEventListener("paste", async (e: ClipboardEvent) => {
const files = [...e.clipboardData?.items ?? []].flatMap((item) => {
if (!item.type.includes("image")) return [];
const file = item.getAsFile();
if (!file) return [];
});
if (files.length === 0) return;
e.preventDefault();
e.stopPropagation();
const { render, dispose } = useStatusBar();
let counter = 1;
let failed = 0;
try {
let inserted = Promise.resolve();
for (const file of files) {
render({ type: "spinner" }, {
type: "text",
text:
uploading ${counter}/${files.length} files, ${failed} failed,
});
const uploadResult = await upload(file, { accessToken });
if (!uploadResult.ok) {
console.error(uploadResult.value);
failed++;
continue;
}
counter++;
const url = uploadResult.value.permalink_url;
inserted.then(() => {
inserted = insertText([${url}]\n);
});
}
} catch (e: unknown) {
if (!(e instanceof Error)) throw e;
console.error(e);
render({ type: "exclamation-triangle" }, {
type: "text",
text: UnexpextedError: ${e.name}, ${e.message},
});
} finally {
render({ type: "check-circle" }, {
"type": "text",
text: Uploaded ${counter} images, ${failed} failed.,
});
setTimeout(() => dispose(), 3000);
}
});
}
}
}