diff --git a/templates/admin.html b/templates/admin.html index aa9d513..0acba2c 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -979,31 +979,50 @@ // 从剪贴板粘贴图片 async function pasteImageFromClipboard(type) { try { + // 检查剪贴板API是否可用(需要HTTPS或localhost) + if (!navigator.clipboard || !navigator.clipboard.read) { + alert('剪贴板API需要HTTPS或localhost环境。\n当前访问地址不支持,请使用文件选择上传。\n\n可改用 localhost:19010 访问来支持粘贴功能。'); + return; + } + const clipboardItems = await navigator.clipboard.read(); + let found = false; for (const item of clipboardItems) { - for (const type of item.types) { - if (type.startsWith('image/')) { - const blob = await item.getType(type); + for (const itemType of item.types) { + if (itemType.startsWith('image/')) { + found = true; + const blob = await item.getType(itemType); const reader = new FileReader(); reader.onload = async (e) => { const base64 = e.target.result; - const res = await fetch('/api/upload/image/base64', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ image: base64 }) - }); - const data = await res.json(); - if (data.success) { - currentImages.push(data.url); - updateImagePreview(); + try { + const res = await fetch('/api/upload/image/base64', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ image: base64 }) + }); + const data = await res.json(); + if (data.success) { + currentImages.push(data.url); + updateImagePreview(); + } + } catch (err) { + alert('上传失败: ' + err.message); } }; reader.readAsDataURL(blob); } } } + if (!found) { + alert('剪贴板中没有图片,请先复制一张图片'); + } } catch (e) { - alert('无法从剪贴板获取图片,请使用文件选择'); + if (e.name === 'NotAllowedError') { + alert('浏览器拒绝访问剪贴板。\n请使用文件选择上传,或改用 localhost:19010 访问。'); + } else { + alert('无法从剪贴板获取图片: ' + e.message + '\n请使用文件选择上传'); + } } } @@ -1221,31 +1240,50 @@ // 从剪贴板粘贴图片 async function pasteSmartImageFromClipboard() { try { + // 检查剪贴板API是否可用(需要HTTPS或localhost) + if (!navigator.clipboard || !navigator.clipboard.read) { + alert('剪贴板API需要HTTPS或localhost环境。\n当前访问地址不支持,请使用文件选择上传。\n\n可改用 localhost:19010 访问来支持粘贴功能。'); + return; + } + const clipboardItems = await navigator.clipboard.read(); + let found = false; for (const item of clipboardItems) { for (const type of item.types) { if (type.startsWith('image/')) { + found = true; const blob = await item.getType(type); const reader = new FileReader(); reader.onload = async (e) => { const base64 = e.target.result; - const res = await fetch('/api/upload/image/base64', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ image: base64 }) - }); - const data = await res.json(); - if (data.success) { - smartAddImages.push(data.url); - updateSmartImagePreview(); + try { + const res = await fetch('/api/upload/image/base64', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ image: base64 }) + }); + const data = await res.json(); + if (data.success) { + smartAddImages.push(data.url); + updateSmartImagePreview(); + } + } catch (err) { + alert('上传失败: ' + err.message); } }; reader.readAsDataURL(blob); } } } + if (!found) { + alert('剪贴板中没有图片,请先复制一张图片'); + } } catch (e) { - alert('无法从剪贴板获取图片,请使用文件选择'); + if (e.name === 'NotAllowedError') { + alert('浏览器拒绝访问剪贴板。\n请使用文件选择上传,或改用 localhost:19010 访问。'); + } else { + alert('无法从剪贴板获取图片: ' + e.message + '\n请使用文件选择上传'); + } } }