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

feat: 聊天窗预览YouTube和B站视频 #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 37 additions & 1 deletion src/views/chat/components/Message/Text.vue
Original file line number Diff line number Diff line change
@@ -50,10 +50,46 @@ const wrapClass = computed(() => {
]
})

function getVideoPreviewer({ type, vid }: { type: 'bilibili' | 'youtube'; vid: string }) {
let previewer = ''
if (type === 'bilibili') {
previewer = `<iframe src="//player.bilibili.com/player.html?bvid=${vid}&autoplay=false" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>`
}
else if (type === 'youtube') {
previewer = `<iframe src="https://www.youtube.com/embed/${vid}" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`
}
else {
// 更新其他视频网站的在线预览
}
return `<div style="margin-bottom:16px;maring-right:16px;">${previewer}</div>`
}

function generateVideoPreviewerText(text: string) {
if (!text)
return ''
// 这里截取视频网站和vid
const reg = /(www\.youtube\.com\/watch\?v\=[^\s\)]+)|(www\.bilibili\.com\/video\/[^\s\)]+)/g
const links = text.match(reg)
return links
? links?.map((link) => {
if (link.includes('youtube')) {
const pattern = /watch\?v\=([^\s\&\)]+)/
const matchResult = link.match(pattern)
return matchResult ? getVideoPreviewer({ type: 'youtube', vid: matchResult[1] }) : ''
}
else {
const pattern = /video\/(([^\s\/\?\)]+))/
const matchResult = link.match(pattern)
return matchResult ? getVideoPreviewer({ type: 'bilibili', vid: matchResult[1] }) : ''
}
}).join('\n')
: ''
}

const text = computed(() => {
const value = props.text ?? ''
if (!props.asRawText)
return mdi.render(value)
return mdi.render(value) + generateVideoPreviewerText(value)
return value
})