index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview txv-video 插件
  3. * Include txv-video (https://github.com/tvfe/txv-miniprogram-plugin)
  4. */
  5. const TxvVideo = function (vm) {
  6. this.vm = vm
  7. }
  8. // #ifdef MP-WEIXIN || MP-QQ
  9. try {
  10. const TxvContext = requirePlugin('tencentvideo')
  11. TxvVideo.prototype.onLoad = function () {
  12. setTimeout(() => {
  13. for (let i = 0; i < this.videos.length; i++) {
  14. const ctx = TxvContext.getTxvContext(this.videos[i])
  15. ctx.id = this.videos[i]
  16. this.vm._videos.push(ctx)
  17. }
  18. }, 50)
  19. }
  20. } catch (e) {
  21. console.error('使用txv-video扩展需注册腾讯视频插件')
  22. }
  23. TxvVideo.prototype.onUpdate = function (_, config) {
  24. config.trustTags['txv-video'] = true
  25. this.videos = []
  26. }
  27. TxvVideo.prototype.onParse = function (node, parser) {
  28. if (node.name === 'iframe' && (node.attrs.src || '').includes('vid')) {
  29. const vid = node.attrs.src.match(/vid=([^&\s]+)/)
  30. if (vid) {
  31. node.name = 'txv-video'
  32. node.attrs.vid = vid[1]
  33. this.videos.push(vid[1])
  34. node.attrs.src = undefined
  35. parser.expose()
  36. }
  37. }
  38. }
  39. // #endif
  40. module.exports = TxvVideo