From 5433605fec0075c7db5d2515fd8c186f203a9f77 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Mon, 27 Apr 2026 18:40:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=BC=BA=E5=89=AA=E8=B4=B4?= =?UTF-8?q?=E6=9D=BF=E7=B2=98=E8=B4=B4=E7=9A=84=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=EF=BC=8C=E8=AF=B4=E6=98=8EHTTPS/localhost=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/admin.html | 84 ++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 23 deletions(-) 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请使用文件选择上传'); + } } }