2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

User:AnnAngela/widgetBilibili/es2015.js

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索

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

  • Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
  • Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
  • Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
  • Opera:在“工具→首选项”中清除缓存
如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
添加代码后的本页地址如下:-{R|https://moegirl.icu/User:AnnAngela/widgetBilibili/es2015.js?_=1}-
'use strict';
class BilibiliWidget {
    appendiframe(iframe, container) {
        const self = this;
        let overlay = iframe.closest('.bilibili-iframe-container').find('.bilibili-iframe-overlay');
        iframe.appendTo(container).data('ready', 'complete').on('load', function () {
            self.load($(this));
        });
        window.setTimeout(function () {
            overlay.append('<br/>').append(self.refreshLink('iframe'));
        }, 10000);
    }
    constructor() {
        let self = this;
        self.globalAutoplay = false;
        $('.bilibili-video-container').show().each(function () {
            self.exec($(this));
        });
    }
    error({
        $video,
        mode,
        info
    }) {
        const self = this;
        let message = {
            execError: `(rawPage: ${info.page}, execPage: ${info._page}, pageListLength: ${info.list.length}, list: ${JSON.stringify(info.list)})`,
            network: `readyState: ${info.readyState}, responseText: "${info.responseText}", status: ${info.status}, statusText: "${info.statusText}"`
        };
        $video.find('.bilibili-iframe-overlay').text('非常抱歉,我们无法解析这个视频~').append('<br/>').append('请点击下方链接重试,如果多次不成功请到提问求助区反馈并附上最下方信息!').append(self.refreshLink('load', {
            $video
        })).append('<br/>').append(message[mode]);
    }
    exec($video) {
        const self = this;
        let {
            _aid,
            _page,
            pagename,
            title,
            _height,
            _width,
            _autoplay
        } = $video[0].dataset;
        let aid = _aid.replace('av', ''),
            page = self.validNumber(_page) ? +_page : 1,
            height = self.validNumber(_height) ? +_height : 421,
            width = self.validNumber(_width) ? +_width : 600,
            autoplay = ['false', ''].find('_autoplay') ? false : true;
        let $title = $video.find('.bilibili-title'),
            iframeContainer = $video.find('.bilibili-iframe-container'),
            iframe = $('<iframe/>').attr({
                frameborder: 0,
                scrolling: 'no',
                src: '',
                allowfullscreen: true
            }).css({
                width: width,
                height: height
            }),
            overlay = iframeContainer.find('.bilibili-iframe-overlay');
        $title.text((title || `av${aid}`) + ([0, 1].find(page) ? ` (${page})` : ''));
        iframeContainer.add(overlay.text('正在加载,请稍候……')).css({
            width: width,
            height: height
        });
        self.getCid(aid).then(function ([list, Title]) {
            let _page = 1,
                name = title || (Title ? Title : `av${aid}`),
                index,
                length;
            if (pagename) {
                for (index = 0, length = list.length; index < length; index++) {
                    if (list[index].Title !== pagename && list[index].title !== pagename) continue;
                    _page = list[index].page;
                    break;
                }
            } else _page = page;
            let idx = _page - 1,
                href = $title.attr('href');
            if (list[index] !== undefined && list[index].VideoCid !== undefined) {
                iframe.attr('src', `https://www.bilibili.com/html/html5player.html?cid=${list[index].VideoCid}&aid=${aid}&page=${_page}&enable_ssl=1&as_wide=1${autoplay ? '&autoplay' : ''}`);
                if (iframeContainer.is(':visible')) self.appendiframe(iframe, iframeContainer);
                else iframe.data('ready', 'load');
                $title.attr('href', href.replace(new RegExp(`/index_${page}`, 'g'), `/index_${_page}`));
                if (_page !== 1) name += ` [${_page}/${list.length}]`;
                $title.text(name);
            } else {
                $title.text(title || `av${aid} [${_page}/${list.length}]`);
                self.error({
                    $video,
                    mode: 'execError',
                    info: {
                        page,
                        _page,
                        list,
                        aid
                    }
                });
            }
        }, function (err) {
            $title.text((title || `av${aid}`) + ([0, 1].find(page) ? ` (${page})` : ''));
            self.error({
                $video,
                mode: 'network',
                info: err
            });
        });
        $video.find('.bilibili-toggle').on('click', function () {
            $(this).closest('tbody').children('.bilibili-video').toggle();
            if ($(this).val() === '显示视频') {
                $(this).val('隐藏视频');
                if (iframe.data('ready') === true) self.appendiframe(iframe, iframeContainer);
            } else $(this).val('显示视频');
        })[autoplay ? 'click' : 'data']();
    }
    getCid(aid) {
        const self = this;
        return new Promise(function (res, rej) {
            $.ajax({
                url: `https://mgwbcprd.azureedge.net/BilibiliCid/Index/av${aid}`,
                type: 'GET',
                success: function (data) {
                    res([data.VideoEntities.map(function (e, i) {
                        e.page = i + 1;
                        e.title = e.Title.replace(/^\d+、/, '');
                        delete e.Id;
                        delete e.ParentCollectionId;
                        return e;
                    }), data.Title]);
                },
                error: function (err) {
                    rej(err);
                }
            });
        });
    }
    load(iframe) {
        let overlay = iframe.closest('.bilibili-iframe-container').find('.bilibili-iframe-overlay');
        iframe.data('load', 'complete');
        overlay.text('加载完毕,如果无法观看视频请刷新本页面,如果多次无法观看请到提问求助区反馈。').delay(2000).queue(function () {
            $(this).fadeOut(370, function () {
                $(this).remove();
            });
            $(this).dequeue();
        });
    }
    refresh(iframe) {
        const self = this;
        let container = $(this).closest('.bilibili-iframe-container'),
            clone = iframe.clone();
        iframe.remove();
        container.append(clone.on('load', function () {
            self.load($(this));
        }));
    }
    refreshLink(mode, info) {
        const self = this;
        return $('<a/>').text('重新加载').on('click', function () {
            switch (mode) {
                case 'iframe': {
                    self.refresh($(this).closest('.bilibili-iframe-container').find('iframe'));
                    break;
                }
                case 'load': {
                    info.$video.remove('iframe').find('.bilibili-iframe-overlay').text('正在加载,请稍候……');
                    self.exec(info.$video);
                }
            }
        });
    }
    validNumber(_n) {
        var n = +_n;
        if (isNaN(n)
            || n < 0
            || /\./.test(_n))
            return false;
        return true;
    }
}