Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 上传图片加一次补偿请求 #239

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/background/actionListener/clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export async function createClipActionListener(
request: RequestMessage<IOperateClipData>,
callback: (params: any) => void,

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

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
sender: chrome.runtime.MessageSender,
) {
const { type, isRunningHostPage } = request.data;
Expand Down Expand Up @@ -58,6 +58,25 @@
);
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
Loading