(url)
| 332 | * @returns {Object|null} { embedUrl, platform, isEmbed: true } 或 null |
| 333 | */ |
| 334 | export const getVideoEmbedInfo = (url) => { |
| 335 | if (!url || typeof url !== 'string') return null; |
| 336 | |
| 337 | // 1. YouTube |
| 338 | // https://www.youtube.com/watch?v=dQw4w9WgXcQ |
| 339 | // https://youtu.be/dQw4w9WgXcQ |
| 340 | const ytRegex = /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([^&\s?]+)/; |
| 341 | const ytMatch = url.match(ytRegex); |
| 342 | if (ytMatch) { |
| 343 | return { |
| 344 | embedUrl: `https://www.youtube.com/embed/${ytMatch[1]}`, |
| 345 | platform: 'youtube', |
| 346 | isEmbed: true |
| 347 | }; |
| 348 | } |
| 349 | |
| 350 | // 2. Bilibili |
| 351 | // https://www.bilibili.com/video/BV1GJ411x7h7 |
| 352 | // http://player.bilibili.com/player.html?bvid=BV1GJ411x7h7 |
| 353 | const biliRegex = /(?:bilibili\.com\/video\/|player\.bilibili\.com\/player\.html\?bvid=)(BV[a-zA-Z0-9]+)/; |
| 354 | const biliMatch = url.match(biliRegex); |
| 355 | if (biliMatch) { |
| 356 | return { |
| 357 | // 增加 high_quality=1&danmaku=0 等参数优化预览体验 |
| 358 | embedUrl: `//player.bilibili.com/player.html?bvid=${biliMatch[1]}&page=1&high_quality=1&danmaku=0`, |
| 359 | platform: 'bilibili', |
| 360 | isEmbed: true |
| 361 | }; |
| 362 | } |
| 363 | |
| 364 | // 3. X (Twitter) |
| 365 | // X 的嵌入比较特殊,通常是嵌入整个 Tweet |
| 366 | // https://x.com/username/status/123456789 |
| 367 | // https://twitter.com/username/status/123456789 |
| 368 | const xRegex = /(?:x\.com|twitter\.com)\/[a-zA-Z0-9_]+\/status\/(\d+)/; |
| 369 | const xMatch = url.match(xRegex); |
| 370 | if (xMatch) { |
| 371 | return { |
| 372 | // 使用 Twitter 官方的嵌入部件 URL (这种方式在 iframe 中可能受限,取决于平台策略) |
| 373 | // 备选方案是返回原始链接但在预览中提示使用 X 的嵌入 |
| 374 | embedUrl: `https://platform.twitter.com/embed/Tweet.html?id=${xMatch[1]}`, |
| 375 | platform: 'x', |
| 376 | isEmbed: true |
| 377 | }; |
| 378 | } |
| 379 | |
| 380 | // 4. Direct Video Link (mp4, webm, ogg, etc.) |
| 381 | const videoExtRegex = /\.(mp4|webm|ogg|mov|m4v)(?:\?.*)?$/i; |
| 382 | if (videoExtRegex.test(url)) { |
| 383 | return { |
| 384 | embedUrl: url, |
| 385 | platform: 'video', |
| 386 | isEmbed: false // Direct video tag, not iframe |
| 387 | }; |
| 388 | } |
| 389 | |
| 390 | // 如果不是已知平台,返回 null |
| 391 | return null; |
no outgoing calls
no test coverage detected