MediaWiki:BilibiliVideo.js

来自Timberborn Wiki
跳转到导航 跳转到搜索

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * 遵循CC-BY-SA协议转载自:https://dev.fandom.com/wiki/BilibiliVideo
 * 移除了原代码中支持i18n的错误提示机制,因为这依赖了一套i18n支持,对于一个边缘功能暂时不做支持。
 * 
 * @name BilibiliVideo.js
 * @author 机智的小鱼君
 * @description Add video from bilibili.com to article
 */
!(function () {
	$('.BiliVideo, .BilibiliVideo').html(function () {
	
	  // Variables
	  var $this = $(this)
	  var videoId
	  var av = $this.attr('data-av')
	  var bv = $this.attr('data-bv')
	  var page = $this.attr('data-page') || $this.attr('data-param') || 1 // data-param is typo made by the author in the old version
	  var size = $this.attr('data-size') || $this.attr('data-width') || '80%'
	
	  // Verify data
	  // Video ID must be set
	  if (!av && !bv) {
	    return $('<span>', { class: 'error', text: "BilibiliVideo脚本错误:未定义BV号或AV号。(EN: Error in BilibiliVideo: BV number or AV number undefined. )" })
	  }
	  // Video ID
	  if (bv) {
	    bv = bv.replace(/^bv/i, '')
	    videoId = 'bvid=' + bv
	  } else if (av) {
	    av = av.replace(/^av/i, '')
	    // AV number must be positive integer
	    if (!/^[1-9]\d*$/.test(av)) {
	      return $('<span>', { class: 'error', text: "BilibiliVideo脚本错误:AV号错误无法解析。(EN: Error in BilibiliVideo: AV number is invalid. )" })
	    }
	    videoId = 'aid=' + av
	  }
	  // Page of video must be positive integer
	  if (!/^[1-9]\d*$/.test(page)) {
	    page = 1
	  }
	
	  // build iframe element
	  var $iframe = $('<iframe>', {
	    class: 'bili-show bilibili-iframe',
	    id: bv ? 'bv-' + bv : 'av-' + av,
	    src: '//player.bilibili.com/player.html?' + videoId + '&page=' + page,
	    scrolling: 'no',
	    border: 0,
	    frameborder: 'no',
	    framespacing: 0,
	    allowfullscreen: true
	  }).css('width', size)
	
	  // Resize
	  function resizeBilibili() {
	    $iframe.height(function () {
	      return $(this).width() / 4 * 3
	    })
	  }
	  // Run
	  $iframe.ready(resizeBilibili)
	  $(window).resize(resizeBilibili)
	  $('.mw-collapsible-toggle').click(resizeBilibili)
	
	  // Return element
	  return $iframe
	})
})();