-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
300 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
import { BasePlugin } from './base'; | ||
|
||
export class ImageParsePlugin extends BasePlugin { | ||
public parse(cloneDom: HTMLElement): Promise<void> | void { | ||
public async parse(cloneDom: HTMLElement): Promise<void> { | ||
const images = cloneDom.querySelectorAll('img'); | ||
images.forEach(image => { | ||
/** | ||
* data-src 占位图 | ||
* currentSrc 真实渲染的图片 | ||
* src | ||
*/ | ||
image.setAttribute('src', image.getAttribute('data-src') || image.currentSrc || image.src); | ||
const requestArray = Array.from(images).map(image => { | ||
return new Promise(async resolve => { | ||
image.setAttribute('src', image.getAttribute('data-src') || image.currentSrc || image.src); | ||
const isOriginImage = /^(http|https):\/\//.test(image.src); | ||
if (!isOriginImage) { | ||
resolve(true); | ||
return; | ||
} | ||
try { | ||
const response = await fetch(image.src); | ||
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 = () => { | ||
// 获取 Base64 编码的数据 | ||
const base64data = reader.result; | ||
image.src = base64data as string; | ||
resolve(true); | ||
}; | ||
} catch (e: any) { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
await Promise.all(requestArray); | ||
} | ||
} |
Oops, something went wrong.