User:Zbx1425/BilibiliVideo.js

来自星海市轨道交通建设 · 2022年5月

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

  • 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
/*
** BilibiliVideo
** Author:马小萌
** Bilibili:https://space.bilibili.com/123855714
**/
"use strict";
$(function() {
    $(".bvideo").each(function(i){
        var id = $(this).data("id").toString().replace(/^(av|bv)*(\w{10}|\d+)$/i,"$2");
        var cid = $(this).data("cid");
        var page = $(this).data("page");
        var title = $(this).data("title");
        var width = $(this).data("width");
        var height = $(this).data("height");
        var idname = isNaN(id)?"bvid=BV":"aid=";
        width=Math.min(width,$(this).parents()[2].clientWidth-40);
        $.getJSON(`https://api.bilibili.com/x/web-interface/view?${idname}${id}`,function(result) {
            if(result.code==0){
                if(title == "") title = result.data.title;
                var v;
                result.data.pages.forEach(function(item) {
                    if(cid==0){
                        if(item.page == page){
                            v = item;
                            return;
                        }
                    }else{
                        if(item.cid == cid){
                            v = item;
                            return;
                        }
                    }
                });
                height=height>359?height+46:height;
                $("<iframe>").css({
                    "width":width,
                    "height":height,
                    "margin":"0 20px;"
                }).attr({
                    "max-width":"100%",
                    "scrolling":"no",
                    "border":"0",
                    "frameborder":"no",
                    "framespacing":"0",
                    "allowfullscreen":"true",
                    "src":`//www.bilibili.com/blackboard/newplayer.html?aid=${result.data.aid}&bvid=${result.data.bvid}&page=${v.page}&cid=${v.cid}&high_quality=1&crossDomain=1&from=wiki&autoplay=`
                }).appendTo(`[data-id$=${id}][data-${cid==0?"page":"cid"}=${cid==0?page:cid}]`);
                $(`<ul>
<li>如果无法观看,请检查是否启用了H5播放器,或点击下方链接</li>
<li><a href="https://www.bilibili.com/video/${result.data.bvid}/?p=${v.page}" title="${result.data.title}">${title}</a></li>
</ul>`).appendTo(`[data-id$=${id}][data-${cid==0?"page":"cid"}=${cid==0?page:cid}]`);
                console.log(title, "载入完成。(aid=",result.data.aid,",bvid=",result.data.bvid,",p=",v.page,",cid=",v.cid,")");
            }else{
                $("<span>啊咧?!视频不见了!</span>").appendTo(`[data-id$=${id}][data-${cid==0?"page":"cid"}=${cid==0?page:cid}]`);
                console.log(isNaN(id)?"BV"+id:"av"+id, "载入失败。(",result.message,")");
            }
        });
    });
    console.log("%c BilibiliVideo %c https://wiki.biligame.com/wiki/?curid=2342", "color: #fff; padding: 5px 0; background: #9dbafc;", "padding: 5px 5px 5px 0; background: #e5e5ff;");
});