打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Module:Bilibili:修订间差异

来自OTTOWiki
琪露若
琪露若留言 | 贡献 (debug)
第1行: 第1行:
local p = {}
local p = {}
local function validateBvid(bvid)
    return bvid and string.match(bvid, "^[Bb][Vv][0-9A-Za-z]+$") ~= nil
end


function p.getTitle(frame)
function p.getTitle(frame)
     local bvid = frame.args[1]
     local bvid = frame.args[1]
     if not bvid or bvid == '' then
     if not validateBvid(bvid) then
         return "无效BV号"
         return "无效 BV 号"
     end
     end
      
      
    -- 调用B站API获取视频信息
     local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid
     local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid
     local response = mw.ext.externalData.getWebData{
     local response = mw.ext.externalData.getWebData{
第14行: 第17行:
     }
     }
      
      
     if not response then
     if not response or response.code ~= 0 or not response.data then
         return "获取信息失败"
         return "获取信息失败"
     end
     end
      
      
     if response.code == 0 and response.title then
     return response.data.title or "无标题"
        return response.title  -- 返回视频标题
end
     else
 
         return "视频不存在或获取失败"
-- 新增:生成 Bilibili 播放器 HTML(安全嵌入)
function p.getPlayer(frame)
    local bvid = frame.args[1]
     if not validateBvid(bvid) then
         return '<div class="error">错误:无效的 BV 号(示例:BV1GJ411x7h7)</div>'
     end
     end
    -- 获取标题(复用你的 getTitle 方法)
    local title = p.getTitle(frame)
   
    -- 返回播放器 HTML
    return string.format([[
        <div class="bilibili-player" style="max-width:600px; margin:1em auto; border-radius:4px; overflow:hidden; box-shadow:0 2px 5px rgba(0,0,0,0.1);">
            <div style="background:#FB7299; color:white; padding:8px 12px; font-weight:bold;">
                <a href="https://www.bilibili.com/video/%s" target="_blank" style="color:white; text-decoration:none;">
                    %s
                </a>
            </div>
            <div style="position:relative; padding-bottom:56.25%%; height:0; background:#000;">
                <iframe
                    src="https://player.bilibili.com/player.html?bvid=%s&high_quality=1&danmaku=0"
                    style="position:absolute; top:0; left:0; width:100%%; height:100%%; border:none;"
                    allowfullscreen
                    scrolling="no"
                ></iframe>
            </div>
        </div>
    ]], bvid, title, bvid)
end
end


return p
return p

2025年6月26日 (四) 09:55的版本

local p = {}

local function validateBvid(bvid)
    return bvid and string.match(bvid, "^[Bb][Vv][0-9A-Za-z]+$") ~= nil
end

function p.getTitle(frame)
    local bvid = frame.args[1]
    if not validateBvid(bvid) then
        return "无效 BV 号"
    end
    
    local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid
    local response = mw.ext.externalData.getWebData{
        url = url,
        format = "json"
    }
    
    if not response or response.code ~= 0 or not response.data then
        return "获取信息失败"
    end
    
    return response.data.title or "无标题"
end

-- 新增:生成 Bilibili 播放器 HTML(安全嵌入)
function p.getPlayer(frame)
    local bvid = frame.args[1]
    if not validateBvid(bvid) then
        return '<div class="error">错误:无效的 BV 号(示例:BV1GJ411x7h7)</div>'
    end

    -- 获取标题(复用你的 getTitle 方法)
    local title = p.getTitle(frame)
    
    -- 返回播放器 HTML
    return string.format([[
        <div class="bilibili-player" style="max-width:600px; margin:1em auto; border-radius:4px; overflow:hidden; box-shadow:0 2px 5px rgba(0,0,0,0.1);">
            <div style="background:#FB7299; color:white; padding:8px 12px; font-weight:bold;">
                <a href="https://www.bilibili.com/video/%s" target="_blank" style="color:white; text-decoration:none;">
                    %s
                </a>
            </div>
            <div style="position:relative; padding-bottom:56.25%%; height:0; background:#000;">
                <iframe
                    src="https://player.bilibili.com/player.html?bvid=%s&high_quality=1&danmaku=0"
                    style="position:absolute; top:0; left:0; width:100%%; height:100%%; border:none;"
                    allowfullscreen
                    scrolling="no"
                ></iframe>
            </div>
        </div>
    ]], bvid, title, bvid)
end

return p