How to embed a YouTube video or any iframe with vue-dompurify-html(如何使用vue-domPurify-html嵌入YouTube视频或任何iframe)
问题描述
我已在Nuxt中创建了一个博客项目,并且正在为我的数据库中的description
字段使用quill text editor。
从数据库呈现博客的description
时,我使用了v-html
,但我收到
34:23警告‘v-html’指令可能导致XSS攻击vue/no-v-html
<span v-html="blog.description"></span>
若要删除此警告,我使用了vue-dompurify-html。
<span v-dompurify-html="blog.description"></span>
现在,当我通过我的quill编辑器添加一个嵌入的视频链接时,domPurify会在呈现时删除视频。对如何将其列入白名单有什么建议吗?
推荐答案
这将允许您使用vue-dompurify-html
实现嵌入式Youtube视频<template>
<div>
<div v-dompurify-html="test"></div>
</div>
</template>
<script>
import Vue from 'vue'
import VueDOMPurifyHTML from 'vue-dompurify-html'
Vue.use(VueDOMPurifyHTML, {
default: {
ADD_TAGS: ['iframe'], // this one whitelists Youtube
},
})
/* eslint-disable no-useless-escape */
/* eslint-disable prettier/prettier */
export default {
data() {
return {
test: '<iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/9_MzJ9QkiHU"></iframe><p><br></p><p><br></p><p>Description</p>'
}
}
}
</script>
如果您想从
'<iframe class="ql-video" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/9_MzJ9QkiHU"></iframe><p><br></p><p><br></p><p>Description</p>'
变成更干净的(对于Vue)这样的
"<iframe class='ql-video' frameborder='0' allowfullscreen='true' src='https://www.youtube.com/embed/9_MzJ9QkiHU'></iframe><p><br></p><p><br></p><p>Description</p>"
您可以使用此方法
string.replaceAll('"', "'")
从这个提交中找到了答案:https://github.com/eternagame/eternagame.org/commit/dfcfb6bf8fc77495bb17ea9231091ca5d4f2cbad#diff-841254fe75488c1bd4cd7f68f00b4be0e48dcfbc4a16b45847b68295e0e3b27bL13-R25
这篇关于如何使用vue-domPurify-html嵌入YouTube视频或任何iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用vue-domPurify-html嵌入YouTube视频或任何iframe


基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01