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

模块:SongPlayer

来自Rizline中文维基
卡介菌留言 | 贡献2025年11月29日 (六) 18:08的版本 (测试版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块:SongPlayer/doc创建

local p = {}

function p.player(frame)
    local args = frame:getParent().args
    local title = args.title
    local artist = args.artist
    local imageName = args.image

    if not title or title == '' or not imageName or imageName == '' then
        return ''
    end

    local sanitizedTitle = title
    sanitizedTitle = sanitizedTitle:gsub(' ', '_')
    sanitizedTitle = sanitizedTitle:gsub('[\\/:*?"<>|]', '-')

    local encodedTitle = mw.uri.encode(sanitizedTitle, 'PATH')
    local imagePath = frame:preprocess('{{filepath:' .. imageName .. '}}')

    local jsTitle = mw.text.jsonEncode(title)
    local jsArtist = mw.text.jsonEncode(artist or 'Unknown Artist')
    local jsUrl = mw.text.jsonEncode('http://pan.rizwiki.cn/d/song/' .. encodedTitle .. '.mp3')
    local jsCover = mw.text.jsonEncode(imagePath)

    local html = {}
    
    table.insert(html, [[
<style>
    #song-player-fixed .aplayer {
        background: var(--color-surface-3, #e6e6e6);
        color: var(--color-base, #000);
        border: 1px solid var(--border-color-base, #a2a9b1);
        box-shadow: 0 0 10px rgba(0,0,0,0.2);
    }
    #song-player-fixed .aplayer .aplayer-body,
    #song-player-fixed .aplayer .aplayer-list ol {
        background: var(--color-surface-1, #fff);
    }
    #song-player-fixed .aplayer .aplayer-info .aplayer-music *,
    #song-player-fixed .aplayer .aplayer-list ol li {
        color: var(--color-base, #000);
    }
    #song-player-fixed .aplayer .aplayer-list ol li.aplayer-list-light {
        background: var(--color-surface-2, #efefef);
    }
    #song-player-fixed .aplayer .aplayer-info .aplayer-controller .aplayer-bar-wrap .aplayer-bar .aplayer-played {
        background: var(--color-progressive, #4285f4) !important;
    }
    #song-player-fixed .aplayer .aplayer-info .aplayer-controller .aplayer-volume-bar-wrap .aplayer-volume-bar .aplayer-volume {
        background: var(--color-progressive, #4285f4) !important;
    }
</style>
    ]])
    
    table.insert(html, '<link rel="stylesheet" href="/aplayer/dist/APlayer.min.css">')
    table.insert(html, '<div id="song-player-fixed"></div>')
    table.insert(html, '<script src="/aplayer/dist/APlayer.min.js"></script>')
    table.insert(html, '<script>')
    table.insert(html, 'mw.loader.using("mediawiki.util").then(function() {')
    table.insert(html, '  const ap = new APlayer({')
    table.insert(html, '    container: document.getElementById("song-player-fixed"),')
    table.insert(html, '    mini: true,')
    table.insert(html, '    fixed: true,')
    table.insert(html, '    audio: [{')
    table.insert(html, '      name: ' .. jsTitle .. ',')
    table.insert(html, '      artist: ' .. jsArtist .. ',')
    table.insert(html, '      url: ' .. jsUrl .. ',')
    table.insert(html, '      cover: ' .. jsCover)
    table.insert(html, '    }]')
    table.insert(html, '  });')
    table.insert(html, '});')
    table.insert(html, '</script>')

    return table.concat(html, '\n')
end

return p