Add try catch for clipboard reading

This commit is contained in:
Evan Fiordeliso 2026-06-12 10:21:20 -04:00
parent f03a4f6910
commit 9d00f5d8b4
3 changed files with 26 additions and 3 deletions

View File

@ -15,7 +15,14 @@ export class CreateBookFromClipboardCommand extends Command {
} }
async callback() { async callback() {
const data = await navigator.clipboard.readText(); let data: string;
try {
data = await navigator.clipboard.readText();
} catch (err) {
console.error("Failed to read clipboard:", err)
new Notice("Failed to get book from the clipboard");
return;
}
const result = await bookSchema.safeParseAsync(JSON.parse(data)); const result = await bookSchema.safeParseAsync(JSON.parse(data));
if (!result.success) { if (!result.success) {

View File

@ -17,7 +17,15 @@ export class CreateBookFromGoodreadsUrlCommand extends Command {
} }
async callback() { async callback() {
const url = await navigator.clipboard.readText(); let url: string;
try {
url = await navigator.clipboard.readText();
} catch (err) {
console.error("Failed to read clipboard:", err)
new Notice("Failed to get URL from clipboard");
return;
}
const legacyId = parseInt( const legacyId = parseInt(
GOODREADS_URL_PATTERN.exec(url)?.[1] ?? "", GOODREADS_URL_PATTERN.exec(url)?.[1] ?? "",
10 10

View File

@ -18,7 +18,15 @@ export class UpdateCoverFromURLCommand extends EditorCheckCommand {
protected async run(_editor: Editor, ctx: MarkdownView | MarkdownFileInfo): Promise<void> { protected async run(_editor: Editor, ctx: MarkdownView | MarkdownFileInfo): Promise<void> {
const file = ctx.file!; const file = ctx.file!;
const url = await navigator.clipboard.readText();
let url: string;
try {
url = await navigator.clipboard.readText();
} catch (err) {
console.error("Failed to read clipbaord:", err);
new Notice("Failed to get cover image url from clipboard");
return;
}
let coverFile: TFile; let coverFile: TFile;
try { try {