generated from tpl/obsidian-sample-plugin
Add try catch for clipboard reading
This commit is contained in:
parent
f03a4f6910
commit
9d00f5d8b4
|
|
@ -15,7 +15,14 @@ export class CreateBookFromClipboardCommand extends Command {
|
|||
}
|
||||
|
||||
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));
|
||||
|
||||
if (!result.success) {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,15 @@ export class CreateBookFromGoodreadsUrlCommand extends Command {
|
|||
}
|
||||
|
||||
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(
|
||||
GOODREADS_URL_PATTERN.exec(url)?.[1] ?? "",
|
||||
10
|
||||
|
|
|
|||
|
|
@ -18,7 +18,15 @@ export class UpdateCoverFromURLCommand extends EditorCheckCommand {
|
|||
|
||||
protected async run(_editor: Editor, ctx: MarkdownView | MarkdownFileInfo): Promise<void> {
|
||||
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;
|
||||
try {
|
||||
|
|
|
|||
Loading…
Reference in New Issue