用户:RedDragon/Test2:修订间差异
来自Rizline中文维基
更多操作
小 |
小 |
||
| 第16行: | 第16行: | ||
<script> | <script> | ||
$(document).ready(function() { | $(document).ready(function () { | ||
$.ajax({ | $.ajax({ | ||
url: mw.util.wikiScript('api'), | url: mw.util.wikiScript('api'), | ||
data: { | data: { | ||
action: 'parse', | action: 'parse', | ||
page: | page: '曲目列表', | ||
prop: 'text', | prop: 'text', | ||
format: 'json' | format: 'json' | ||
}, | }, | ||
success: function(data) { | success: function (data) { | ||
var content = data.parse.text['*'] | |||
var content = data.parse.text['*'] | var $content = $('<div>').html(content) | ||
var $content = $('<div>').html(content) | var songs = [] | ||
$content.find('.wikitable tr').each(function (index) { | |||
if (index === 0) return | |||
var $cells = $(this).find('td, th') | |||
if ($cells.length >= 7) { | |||
var song = { | |||
image: extractImageUrl($cells.eq(0)), | |||
title: $cells.eq(1).text().trim(), | |||
ez: $cells.eq(4).text().trim(), | |||
hd: $cells.eq(5).text().trim(), | |||
in: $cells.eq(6).text().trim() | |||
} | |||
if (song.title) songs.push(song) | |||
} | |||
} | }) | ||
if (songs.length > 0) { | |||
var randomSong = songs[Math.floor(Math.random() * songs.length)] | |||
loadSongDetails(randomSong) | |||
} | |||
} | } | ||
}) | }) | ||
function extractImageUrl($cell) { | |||
var $img = $cell.find('img') | |||
if ($img.length > 0) return $img.attr('src') || $img.attr('data-src') | |||
return $cell.text().trim() | |||
return | |||
} | } | ||
var | |||
function loadSongDetails(song) { | |||
updateSongInfo(song) | |||
if (bgColor | |||
$.ajax({ | |||
url: mw.util.wikiScript('api'), | |||
data: { | |||
action: 'parse', | |||
page: song.title, | |||
prop: 'text', | |||
format: 'json' | |||
}, | |||
success: function (data) { | |||
var content = data.parse.text['*'] | |||
var $content = $('<div>').html(content) | |||
updateStyles({ | |||
titleStyle: getBackgroundColor($content.find('.infobox-title')) || '#94E1FF', | |||
headerStyle: getBackgroundColor($content.find('.infobox-header')) || '#A3E5FF', | |||
labelStyle: getBackgroundColor($content.find('.infobox-label')) || '#B2E9FE' | |||
}) | |||
}, | |||
error: function () { | |||
updateStyles({ | |||
titleStyle: '#94E1FF', | |||
headerStyle: '#A3E5FF', | |||
labelStyle: '#B2E9FE' | |||
}) | |||
} | |||
}) | |||
} | |||
function getBackgroundColor($element) { | |||
if ($element.length === 0) return null | |||
var bgColor = $element.css('background-color') | |||
if (bgColor) { | |||
if (bgColor.startsWith('rgb')) { | |||
var rgb = bgColor.match(/\d+/g) | |||
if (rgb) { | |||
if (rgb.length === 3) { | |||
return '#' + | |||
('0' + parseInt(rgb[0]).toString(16)).slice(-2) + | |||
('0' + parseInt(rgb[1]).toString(16)).slice(-2) + | |||
('0' + parseInt(rgb[2]).toString(16)).slice(-2) | |||
} | |||
} | |||
} | |||
} | } | ||
return bgColor | |||
} | |||
function updateSongInfo(song) { | |||
$('.infobox-title th').text(song.title) | |||
if (song.image) $('#song-image').attr('src', song.image) | |||
window.difficultyArray = [ | |||
createDiffSpan("EZ", song.ez), | |||
createDiffSpan("HD", song.hd), | |||
createDiffSpan("IN", song.in), | |||
'<span style="width: max-content;text-align:center;display:inline-block;border-radius:1em;color:white;background-color:#53D6FF;padding:0 0.80em;margin:0.1em 0.50em;">无限制</span>' | |||
] | |||
generateRandomRestrictions() | |||
} | |||
function createDiffSpan(diff, level) { | |||
var colors = { EZ: '#57E4C4', HD: '#FDBA61', IN: '#FE8661', AT: '#4C364B' } | |||
return '<span style="width: max-content;text-align:center;display:inline-block;border-radius:1em;color:white;background-color:' + colors[diff] + ';padding:0 0.80em;margin:0.1em 0.50em;">' + diff + ' ' + level + '</span>' | |||
} | |||
function updateStyles(styles) { | |||
$('.infobox-title th').css('background', styles.titleStyle) | |||
$('.infobox-header th').css('background', styles.headerStyle) | |||
$('.infobox-label').css('background', styles.labelStyle) | |||
} | } | ||
function generateRandomRestrictions() { | |||
var randomDifficulty = window.difficultyArray[Math.floor(Math.random() * window.difficultyArray.length)] | |||
$('#difficulty-data').html(randomDifficulty) | |||
var speedOptions = ["无限制", "无限制", "无限制"].concat(Array.from({ length: 91 }, (_, i) => (i + 10) / 10.0)) | |||
$('#speed-data').text(speedOptions[Math.floor(Math.random() * speedOptions.length)]) | |||
$('# | var modOptions = ["无MOD"] | ||
var otherOptions = ["无限制"] | |||
$('#mod-data').text(modOptions[Math.floor(Math.random() * modOptions.length)]) | |||
$('#other-data').text(otherOptions[Math.floor(Math.random() * otherOptions.length)]) | |||
} | } | ||
}) | |||
}) | |||
</script> | </script> | ||
</html> | </html> | ||