Skip to content

Commit

Permalink
fix: 上传图片加一次补偿请求 (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
moshangqi authored Apr 8, 2024
1 parent 475bc63 commit ceb2c0b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/background/actionListener/clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ export async function createClipActionListener(
);
break;
}
case OperateClipEnum.getImage: {
try {
const url = (request.data as any).url;

Check warning on line 63 in src/background/actionListener/clip.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/bridge/background/clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export function createClipBridge(impl: ICallBridgeImpl) {
);
});
},

async getImage(url: string): Promise<string> {
return new Promise(resolve => {
impl(BackgroundEvents.OperateClip, { type: OperateClipEnum.getImage, url }, (res: string) => {
resolve(res);
});
});
},
},
};
}
10 changes: 10 additions & 0 deletions src/core/parseDom/plugin/image.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { backgroundBridge } from '@/core/bridge/background';
import { BasePlugin } from './base';

export class ImageParsePlugin extends BasePlugin {
Expand Down Expand Up @@ -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);
}
});
Expand Down
1 change: 1 addition & 0 deletions src/isomorphic/background/clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum OperateClipEnum {
selectArea = 'selectArea',
screenOcr = 'screenOcr',
clipPage = 'clipPage',
getImage = 'getImage',
}

export interface IOperateClipData {
Expand Down

0 comments on commit ceb2c0b

Please sign in to comment.