模块:SongPlayer
来自Rizline中文维基
更多操作
此模块的文档可以在模块: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)
-- 将 CSS 和 JS 内容存储在变量中
local css_content = [[
#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;
}
]]
local js_content = string.format([[
mw.loader.using("mediawiki.util").then(function() {
const ap = new APlayer({
container: document.getElementById("song-player-fixed"),
mini: true,
fixed: true,
audio: [{
name: %s,
artist: %s,
url: %s,
cover: %s
}]
});
});
]], jsTitle, jsArtist, jsUrl, jsCover)
-- 使用 #tag 解析器函数安全地生成标签
local style_tag = frame:callParserFunction('tag', 'style', css_content)
local script_tag = frame:callParserFunction('tag', 'script', js_content)
-- 组合最终的HTML输出
local html = {
style_tag,
'<link rel="stylesheet" href="/aplayer/dist/APlayer.min.css">',
'<div id="song-player-fixed"></div>',
'<script src="/aplayer/dist/APlayer.min.js"></script>',
script_tag
}
return table.concat(html, '\n')
end
return p