用户:RedDragon/Test2:修订间差异
来自Rizline中文维基
更多操作
小 |
小 |
||
| 第35行: | 第35行: | ||
<script> | <script> | ||
document.addEventListener('DOMContentLoaded', function () { | |||
const baseUrl = window.location.origin + window.location.pathname.split('/wiki/')[0] | |||
const apiUrl = baseUrl + '/api.php' | |||
fetch(apiUrl + '?action=parse&page=%E6%9B%B2%E7%9B%AE%E5%88%97%E8%A1%A8&prop=text&format=json') | |||
.then(response => response.json()) | |||
.then(data => { | |||
const content = data.parse.text['*'] | |||
const parser = new DOMParser() | |||
const doc = parser.parseFromString(content, 'text/html') | |||
const songs = [] | |||
const rows = doc.querySelectorAll('.wikitable tr') | |||
rows.forEach((row, index) => { | |||
if (index === 0) return | |||
const cells = row.querySelectorAll('td, th') | |||
if (cells.length >= 7) { | |||
const song = { | |||
image: extractImageUrl(cells[0]), | |||
title: cells[1].textContent.trim(), | |||
ez: cells[4].textContent.trim(), | |||
hd: cells[5].textContent.trim(), | |||
in: cells[6].textContent.trim() | |||
} | } | ||
}) | if (song.title) songs.push(song) | ||
} | |||
}) | |||
if (songs.length > 0) { | |||
const randomSong = songs[Math.floor(Math.random() * songs.length)] | |||
loadSongDetails(randomSong) | |||
} else { | |||
} | |||
useMockData() | useMockData() | ||
} | } | ||
}) | }) | ||
.catch(error => { | |||
console.error('获取曲目列表失败:', error) | |||
useMockData() | |||
}) | |||
}) | |||
function extractImageUrl(cell) { | |||
const img = cell.querySelector('img') | |||
if (img) return img.src || img.getAttribute('data-src') | |||
return cell.textContent.trim() | |||
} | |||
function loadSongDetails(song) { | |||
updateSongInfo(song) | |||
const baseUrl = window.location.origin + window.location.pathname.split('/wiki/')[0] | |||
const apiUrl = baseUrl + '/api.php' | |||
fetch(apiUrl + '?action=parse&page=' + encodeURIComponent(song.title) + '&prop=text&format=json') | |||
.then(response => response.json()) | |||
.then(data => { | |||
const content = data.parse.text['*'] | |||
const parser = new DOMParser() | |||
const doc = parser.parseFromString(content, 'text/html') | |||
const titleStyle = getBackgroundColor(doc.querySelector('.infobox-title')) | |||
const headerStyle = getBackgroundColor(doc.querySelector('.infobox-header')) | |||
const labelStyle = getBackgroundColor(doc.querySelector('.infobox-label')) | |||
updateStyles({ | |||
titleStyle: titleStyle || '#94E1FF', | |||
headerStyle: headerStyle || '#A3E5FF', | |||
labelStyle: labelStyle || '#B2E9FE' | |||
}) | |||
}) | |||
.catch(() => { | |||
updateStyles({ | |||
titleStyle: '#94E1FF', | |||
headerStyle: '#A3E5FF', | |||
labelStyle: '#B2E9FE' | |||
}) | }) | ||
} | }) | ||
} | |||
function getBackgroundColor(element) { | |||
if (!element) return null | |||
const bgColor = element.style.backgroundColor | |||
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) { | |||
const titleElement = document.querySelector('.infobox-title th') | |||
titleElement.textContent = song.title | |||
const imageElement = document.getElementById('song-image') | |||
if (song.image) imageElement.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) { | |||
const colors = { EZ: '#57E4C4', HD: '#FDBA61', IN: '#FE8661' } | |||
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) { | |||
document.querySelector('.infobox-title th').style.background = styles.titleStyle | |||
document.querySelector('.infobox-header th').style.background = styles.headerStyle | |||
document.querySelectorAll('.infobox-label').forEach(label => { | |||
label.style.background = styles.labelStyle | |||
}) | |||
} | |||
function generateRandomRestrictions() { | |||
const difficultyElement = document.getElementById('difficulty-data') | |||
const randomDifficulty = window.difficultyArray[Math.floor(Math.random() * window.difficultyArray.length)] | |||
difficultyElement.innerHTML = randomDifficulty | |||
const speedOptions = ["无限制", "无限制", "无限制"] | |||
for (let i = 10; i <= 100; i++) speedOptions.push((i / 10).toFixed(1)) | |||
document.getElementById('speed-data').textContent = speedOptions[Math.floor(Math.random() * speedOptions.length)] | |||
const modOptions = ["无MOD"] | |||
const otherOptions = ["无限制"] | |||
document.getElementById('mod-data').textContent = modOptions[Math.floor(Math.random() * modOptions.length)] | |||
document.getElementById('other-data').textContent = otherOptions[Math.floor(Math.random() * otherOptions.length)] | |||
} | |||
function useMockData() { | |||
const mockSong = { | |||
title: "Pastel Lines", | |||
image: "/images/thumb/4/41/%E6%8F%92%E7%94%BB%E5%9B%BE%E6%A0%87.png/200px-%E6%8F%92%E7%94%BB%E5%9B%BE%E6%A0%87.png", | |||
ez: "1", | |||
hd: "6", | |||
in: "11" | |||
} | |||
updateSongInfo(mockSong) | |||
updateStyles({ | |||
titleStyle: '#94E1FF', | |||
headerStyle: '#A3E5FF', | |||
labelStyle: '#B2E9FE' | |||
}) | }) | ||
} | } | ||
</script> | </script> | ||
</html> | </html> | ||