From 96eb5572b340844fe4414dca5d9001ffa9f0354d Mon Sep 17 00:00:00 2001 From: moshangqi <2509678669@qq.com> Date: Mon, 8 Apr 2024 10:10:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E6=AC=A1=E8=A1=A5=E5=81=BF=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background/actionListener/clip.ts | 19 +++++++++++++++++++ src/core/bridge/background/clip.ts | 8 ++++++++ src/core/parseDom/plugin/image.ts | 10 ++++++++++ src/isomorphic/background/clip.ts | 1 + 4 files changed, 38 insertions(+) diff --git a/src/background/actionListener/clip.ts b/src/background/actionListener/clip.ts index 5c0d34fd..893534ab 100644 --- a/src/background/actionListener/clip.ts +++ b/src/background/actionListener/clip.ts @@ -58,6 +58,25 @@ export async function createClipActionListener( ); break; } + case OperateClipEnum.getImage: { + try { + const url = (request.data as any).url; + const response = await fetch(url); + if (response.status !== 200) { + throw new Error('Error fetching image'); + } + const blob = await response.blob(); // 将响应体转换为 Blob + const reader = new FileReader(); + reader.readAsDataURL(blob); // 读取 Blob 数据并编码为 Base64 + reader.onloadend = () => { + callback(reader.result); + }; + } catch (error) { + // + callback(''); + } + break; + } default: { break; } diff --git a/src/core/bridge/background/clip.ts b/src/core/bridge/background/clip.ts index 2d29ae04..338f103f 100644 --- a/src/core/bridge/background/clip.ts +++ b/src/core/bridge/background/clip.ts @@ -41,6 +41,14 @@ export function createClipBridge(impl: ICallBridgeImpl) { ); }); }, + + async getImage(url: string): Promise { + return new Promise(resolve => { + impl(BackgroundEvents.OperateClip, { type: OperateClipEnum.getImage, url }, (res: string) => { + resolve(res); + }); + }); + }, }, }; } diff --git a/src/core/parseDom/plugin/image.ts b/src/core/parseDom/plugin/image.ts index 299ac85f..9d498e7d 100644 --- a/src/core/parseDom/plugin/image.ts +++ b/src/core/parseDom/plugin/image.ts @@ -1,3 +1,4 @@ +import { backgroundBridge } from '@/core/bridge/background'; import { BasePlugin } from './base'; export class ImageParsePlugin extends BasePlugin { @@ -26,6 +27,15 @@ export class ImageParsePlugin extends BasePlugin { resolve(true); }; } catch (e: any) { + // 补充一次图片请求,避免被拦截 + try { + const base64 = await backgroundBridge.clip.getImage(image.src); + if (base64) { + image.src = base64; + } + } catch (error) { + // + } resolve(true); } }); diff --git a/src/isomorphic/background/clip.ts b/src/isomorphic/background/clip.ts index 126e1af6..a8712d48 100644 --- a/src/isomorphic/background/clip.ts +++ b/src/isomorphic/background/clip.ts @@ -2,6 +2,7 @@ export enum OperateClipEnum { selectArea = 'selectArea', screenOcr = 'screenOcr', clipPage = 'clipPage', + getImage = 'getImage', } export interface IOperateClipData {