AI-News/frontend/app/composables/useImageExtractor.ts

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-12-04 10:04:21 +08:00
/**
* HTML URL
*/
export function useImageExtractor() {
/**
* HTML src
* @param html HTML
* @returns URL null
*/
function extractFirstImage(html: string): string | null {
if (!html || typeof html !== 'string') return null
// 创建一个临时 DOM 元素来解析 HTML
const tempDiv = document.createElement('div')
tempDiv.innerHTML = html
// 查找第一个 img 标签
const firstImg = tempDiv.querySelector('img')
if (firstImg && firstImg.src) {
return firstImg.src
}
// 如果没有找到 img 标签,尝试使用正则表达式匹配
const imgRegex = /<img[^>]+src=["']([^"']+)["']/i
const match = html.match(imgRegex)
if (match && match[1]) {
return match[1]
}
return null
}
/**
* HTML src
* @param html HTML
* @returns URL
*/
function extractAllImages(html: string): string[] {
if (!html || typeof html !== 'string') return []
const tempDiv = document.createElement('div')
tempDiv.innerHTML = html
const images = tempDiv.querySelectorAll('img')
const urls: string[] = []
images.forEach((img) => {
if (img.src) {
urls.push(img.src)
}
})
return urls
}
return {
extractFirstImage,
extractAllImages,
}
}