如何解决JSDOM:使用 base64 字符串更新“src”属性
此代码尝试用 Base 64 数据字符串替换图像 URL。
我无法将 src 属性更新为 base64String
。目前尚不清楚问题是否源于 axios 承诺或 setAttribute。
const dom = new JSDOM(body,{ includeNodeLocations: true,QuerySelector: true,FetchExternalResources: true })
const document = dom.window.document
const imageElements = document.querySelectorAll('img')
imageElements.forEach(async imageElement => {
const src = imageElement.getAttribute('src')
axios
.get(src,{ responseType: 'arraybuffer' })
.then(image => {
const raw = Buffer.from(image.data).toString('base64')
const base64String = 'data:' + image.headers['content-type'] + ';base64,' + raw
imageElement.setAttribute('src',base64String)
})
.catch(error => {
rejects(error)
})
})
const article = new Readability(document).parse()
const content = sanitizeHtml(article.content,{
allowedTags: ['h1','p' 'a','img'],allowedAttributes: { img: ['src'] },})
console.log(content)
// Expect images to be replaced with data string. Original image urls remain as src attributed.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。