diff
};
- // 临时隐藏UI元素
- this.hideUIElementsForCapture();
-
- // 添加短延迟确保DOM更新已渲染
- setTimeout(() => {
- // 获取当前的滚动位置和视口尺寸
- const scrollX = window.scrollX;
- const scrollY = window.scrollY;
- const viewportWidth = window.innerWidth;
- const viewportHeight = window.innerHeight;
-
- // 检查选择区域是否在视口范围内
- const isRectVisible = (
- captureRect.left >= scrollX &&
- captureRect.top >= scrollY &&
- captureRect.left + captureRect.width <= scrollX + viewportWidth &&
- captureRect.top + captureRect.height <= scrollY + viewportHeight
- );
-
- // 根据区域是否在当前视口内,选择不同的截图方法
- if (isRectVisible) {
- chrome.runtime.sendMessage({
- action: 'captureScreen'
- }, response => {
- this.restoreUIElementsAfterCapture();
- if (response && response.success) {
- this.processQRCodeImage(response.dataUrl, captureRect, scrollX, scrollY, decodeMsg);
- } else {
- console.error("截图失败:", response?.error || "未知错误");
- decodeMsg.textContent = I18nHelper.getNotificationText('qrError', response?.error || "未知错误");
- setTimeout(() => decodeMsg.remove(), 3000);
- }
- });
- } else {
- chrome.runtime.sendMessage({
- action: 'captureFullPage',
- targetArea: captureRect
- }, response => {
- this.restoreUIElementsAfterCapture();
- if (response && response.success) {
- this.processQRCodeImage(response.dataUrl, captureRect, 0, 0, decodeMsg);
- } else {
- console.error("全页面截图失败:", response?.error || "未知错误");
- decodeMsg.textContent = I18nHelper.getNotificationText('qrError', response?.error || "未知错误");
- setTimeout(() => decodeMsg.remove(), 3000);
- }
- });
- }
- }, 30);
- }
-
- // 处理二维码图像
- processQRCodeImage(dataUrl, rect, scrollX, scrollY, notification) {
- // 首先检查 jsQR 是否可用
- if (typeof jsQR !== 'function') {
- console.error("jsQR 库未加载");
- notification.textContent = I18nHelper.getToolbarText('qrError', "二维码解析库未加载");
- setTimeout(() => notification.remove(), 3000);
- return;
- }
-
- const image = new Image();
-
- image.onload = () => {
- try {
- // 创建Canvas并裁剪图像
- const canvas = document.createElement('canvas');
- const devicePixelRatio = window.devicePixelRatio || 1;
-
- // 计算实际的裁剪区域(考虑设备像素比)
- const scaledRect = {
- left: Math.round((rect.left - scrollX) * devicePixelRatio),
- top: Math.round((rect.top - scrollY) * devicePixelRatio),
- width: Math.round(rect.width * devicePixelRatio),
- height: Math.round(rect.height * devicePixelRatio)
- };
-
- canvas.width = scaledRect.width;
- canvas.height = scaledRect.height;
-
- const ctx = canvas.getContext('2d', { willReadFrequently: true });
- if (!ctx) {
- throw new Error("无法获取Canvas上下文");
- }
-
- // 绘制裁剪区域
- ctx.drawImage(
- image,
- scaledRect.left, scaledRect.top,
- scaledRect.width, scaledRect.height,
- 0, 0, scaledRect.width, scaledRect.height
- );
-
- // 获取图像数据
- const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
-
- // 使用jsQR库解析二维码
- try {
- const code = jsQR(
- imageData.data,
- imageData.width,
- imageData.height,
- {
- inversionAttempts: "dontInvert"
- }
- );
-
- if (code && code.data) {
- console.log("二维码解析成功:", code.data);
-
- // 复制到剪贴板
- navigator.clipboard.writeText(code.data)
- .then(() => {
- notification.textContent = I18nHelper.getToolbarText('qrSuccess');
- setTimeout(() => notification.remove(), 3000);
- })
- .catch(error => {
- console.error("复制到剪贴板失败:", error);
- // 如果复制失败,尝试使用备用方法
- this.copyQRCodeFallback(code.data, notification);
- });
- } else {
- console.log("未检测到二维码");
- notification.textContent = I18nHelper.getToolbarText('qrNoQRFound');
- setTimeout(() => notification.remove(), 3000);
- }
- } catch (qrError) {
- console.error("二维码解析出错:", qrError);
- notification.textContent = I18nHelper.getToolbarText('qrError', "二维码解析失败");
- setTimeout(() => notification.remove(), 3000);
- }
- } catch (error) {
- console.error("处理二维码图像时出错:", error);
- notification.textContent = I18nHelper.getToolbarText('qrError', error.message);
- setTimeout(() => notification.remove(), 3000);
- }
- };
-
- image.onerror = () => {
- console.error("图像加载失败");
- notification.textContent = I18nHelper.getToolbarText('qrError', "图像加载失败");
- setTimeout(() => notification.remove(), 3000);
- };
-
- image.src = dataUrl;
- }
-
- // 添加备用复制方法
- copyQRCodeFallback(text, notification) {
- try {
- // 创建临时文本区域
- const textarea = document.createElement('textarea');
- textarea.value = text;
- textarea.style.position = 'fixed';
- textarea.style.opacity = '0';
- document.body.appendChild(textarea);
-
- // 选择并复制文本
- textarea.select();
- document.execCommand('copy');
-
- // 移除临时元素
- document.body.removeChild(textarea);
-
- // 显示成功消息
- notification.textContent = I18nHelper.getToolbarText('qrSuccess');
- setTimeout(() => notification.remove(), 3000);
- } catch (error) {
- console.error("备用复制方法失败:", error);
- notification.textContent = I18nHelper.getToolbarText('qrError', "复制失败");
- setTimeout(() => notification.remove(), 3000);
- }
- }
-
- // 抠图功能 - 移除图像背景并复制到剪贴板
- removeBackground() {
- if (!this.selection) {
- console.error("未找到选择框,无法抠图");
- return;
- }
-
- console.log("开始抠图处理");
-
- // 显示处理中提示
- const removeMsg = this.showNotification(I18nHelper.isZh() ? "正在抠图处理中..." : "Removing background...");
-
- // 使用我们跟踪的绝对坐标进行截图
- const captureRect = {
- left: Math.min(this.startX, this.endX),
- top: Math.min(this.startY, this.endY),
- width: Math.abs(this.endX - this.startX),
- height: Math.abs(this.endY - this.startY)
- };
-
- console.log("抠图区域坐标(绝对):", captureRect);
+ console.log("二维码区域坐标(绝对):", captureRect);
// 获取当前的滚动位置和视口尺寸