|
|
| (未显示同一用户的10个中间版本) |
| 第1行: |
第1行: |
| /* === BMV (Bilibili Music Video) Widget Start === */ | | /* PWA安装测试 */ |
| (function($) { | | (function() { |
| | | var btn = document.getElementById('pwa-test-btn'); |
| // ------------------- 初始化单个BMV实例的函数 ------------------- | | var savedPrompt = null; |
| function initializeBMV($container) {
| | window.addEventListener('beforeinstallprompt', function(e) { |
| if ($container.data('bmv-initialized')) return;
| | e.preventDefault(); |
| $container.data('bmv-initialized', true);
| | savedPrompt = e; |
| console.log('BMV from Common.js: Initializing widget for song:', $container.data('song-name'));
| | if (btn) btn.style.display = 'block'; |
| | | }); |
| var songName = $container.data('song-name'); | | if (btn) { |
| var rawDifficulties = $container.data('difficulties'); | | btn.addEventListener('click', function() { |
| var $difficultyDiv = $container.find('.bmv-difficulty'); | | if (savedPrompt) { |
| var $ratioDiv = $container.find('.bmv-ratio');
| | savedPrompt.prompt(); |
| var $outputSpan = $container.find('.bmv-output');
| | savedPrompt = null; |
| | | } else { |
| var difficultyColors = {"EZ": "#57E4C4", "HD": "#FDBA61", "IN": "#FE8661", "AT": "#4C364B"};
| | alert('暂时无法安装(可能已安装或不支持)'); |
| | |
| function parseDifficulties(input) {
| |
| if (!input) return null;
| |
| try {
| |
| return String(input).split(',').map(function(item) {
| |
| var match = item.trim().match(/^([A-Za-z]+)(\d+)$/);
| |
| if (!match) throw new Error('Invalid format');
| |
| return (match[1].toUpperCase() + ' ' + match[2]);
| |
| }); | |
| } catch (e) { | |
| console.error("BMV Error: parsing difficulties:", input, e); | |
| return null;
| |
| } | | } |
| }
| |
|
| |
| var difficultyStates = parseDifficulties(rawDifficulties);
| |
|
| |
| if (!songName || !difficultyStates || !difficultyStates.length) {
| |
| $container.find('table').replaceWith('<div style="color:red; padding:10px; text-align:center;">BMV Error: Missing or invalid songName/difficulties.</div>');
| |
| return;
| |
| }
| |
|
| |
| $difficultyDiv.data({states: difficultyStates, current: 0});
| |
| $ratioDiv.data({states: ["16:9", "4:3"], current: 0});
| |
|
| |
| function updateOutput() {
| |
| var difficulty = $difficultyDiv.data('states')[$difficultyDiv.data('current')].split(' ')[0];
| |
| var ratio = $ratioDiv.data('states')[$ratioDiv.data('current')].replace(':', '-');
| |
| var encodedSongName = encodeURIComponent(songName);
| |
| var videoUrl = 'https://pan.rizwiki.cn/d/' + ratio + '_' + encodedSongName + '_' + difficulty + '.mp4';
| |
| var videoHTML = '<video class="html5media-video" src="' + videoUrl + '" controls preload="metadata" loading="lazy" style="height:100%; object-fit:contain;"></video>';
| |
| $outputSpan.html(videoHTML);
| |
| }
| |
|
| |
| $difficultyDiv.on('click', function() {
| |
| var $this = $(this), states = $this.data('states'), current = $this.data('current');
| |
| var next = (current + 1) % states.length;
| |
| $this.text(states[next]).data('current', next);
| |
| var difficultyType = states[next].split(' ')[0];
| |
| $this.parent().css('background-color', difficultyColors[difficultyType] || '#FFFFFF');
| |
| updateOutput();
| |
| });
| |
|
| |
| $ratioDiv.parent().on('click', function() {
| |
| var states = $ratioDiv.data('states'), current = $ratioDiv.data('current');
| |
| var next = (current + 1) % states.length;
| |
| $ratioDiv.text(states[next]).data('current', next);
| |
| updateOutput();
| |
| }); | | }); |
|
| |
| var firstDiff = difficultyStates[0], diffType = firstDiff.split(' ')[0];
| |
| $difficultyDiv.text(firstDiff);
| |
| $difficultyDiv.parent().css('background-color', difficultyColors[diffType]);
| |
| updateOutput();
| |
| } | | } |
| | | })(); |
| // ------------------- 主执行逻辑:使用 mw.hook -------------------
| |
| mw.hook('wikipage.content').add(function($content) {
| |
| $content.find('.js-bmv-widget:not([data-bmv-initialized])').each(function() {
| |
| initializeBMV($(this));
| |
| });
| |
| });
| |
| | |
| }(jQuery));
| |
| /* === BMV (Bilibili Music Video) Widget End === */
| |
/* PWA安装测试 */
(function() {
var btn = document.getElementById('pwa-test-btn');
var savedPrompt = null;
window.addEventListener('beforeinstallprompt', function(e) {
e.preventDefault();
savedPrompt = e;
if (btn) btn.style.display = 'block';
});
if (btn) {
btn.addEventListener('click', function() {
if (savedPrompt) {
savedPrompt.prompt();
savedPrompt = null;
} else {
alert('暂时无法安装(可能已安装或不支持)');
}
});
}
})();