|
|
| 第1行: |
第1行: |
| (function($) { | | (function($) { |
| function initializeBMV($container) { | | mw.hook('wikipage.content').add(function($content) { |
| if ($container.data('bmv-initialized')) return;
| | var $widget = $content.find('.js-bmv-widget'); |
| $container.data('bmv-initialized', true);
| |
| console.log('BMV from Common.js: Initializing widget for song:', $container.data('song-name'));
| |
| | |
| var songName = $container.data('song-name');
| |
| var rawDifficulties = $container.data('difficulties'); | |
| var $difficultyDiv = $container.find('.bmv-difficulty');
| |
| var $ratioDiv = $container.find('.bmv-ratio');
| |
| var $outputSpan = $container.find('.bmv-output');
| |
| | |
| var difficultyColors = {"EZ": "#57E4C4", "HD": "#FDBA61", "IN": "#FE8661", "AT": "#4C364B"};
| |
| | |
| 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() { | | if ($widget.length > 0) { |
| var difficulty = $difficultyDiv.data('states')[$difficultyDiv.data('current')].split(' ')[0]; | | console.log("Widget found. Data attributes are:", $widget.data()); |
| var ratio = $ratioDiv.data('states')[$ratioDiv.data('current')].replace(':', '-');
| | } else { |
| var encodedSongName = encodeURIComponent(songName);
| | console.log("Widget with class .js-bmv-widget NOT found."); |
| 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('wikipage.content').add(function($content) {
| |
| $content.find('.js-bmv-widget:not([data-bmv-initialized])').each(function() {
| |
| initializeBMV($(this));
| |
| });
| |
| }); | | }); |
|
| |
| }(jQuery)); | | }(jQuery)); |
(function($) {
mw.hook('wikipage.content').add(function($content) {
var $widget = $content.find('.js-bmv-widget');
if ($widget.length > 0) {
console.log("Widget found. Data attributes are:", $widget.data());
} else {
console.log("Widget with class .js-bmv-widget NOT found.");
}
});
}(jQuery));