用户:RedDragon/Test2:修订间差异
来自Rizline中文维基
更多操作
小 |
小 |
||
| 第8行: | 第8行: | ||
<td colspan="2" style="text-align:center;"> | <td colspan="2" style="text-align:center;"> | ||
<img id="song-image" | <img id="song-image" | ||
src="/images/thumb/4/41/%E6%8F%92%E7%94%BB%E5%9B%BE%E6%A0%87.png/ | src="/images/thumb/4/41/%E6%8F%92%E7%94%BB%E5%9B%BE%E6%A0%87.png/300px-%E6%8F%92%E7%94%BB%E5%9B%BE%E6%A0%87.png" | ||
width=" | width="300" height="300"> | ||
</td> | </td> | ||
</tr> | </tr> | ||
| 第35行: | 第35行: | ||
<script> | <script> | ||
document.addEventListener('DOMContentLoaded', | document.addEventListener('DOMContentLoaded', () => { | ||
fetch('https://rizwiki.cn/wiki/ | fetch('https://rizwiki.cn/wiki/曲目列表') | ||
.then( | .then(r => r.ok ? r.text() : Promise.reject('网络错误')) | ||
.then(html => { | .then(html => { | ||
const | const doc = new DOMParser().parseFromString(html, 'text/html') | ||
const songs = [] | const songs = [] | ||
const table = [...doc.querySelectorAll('.wikitable')].find(t => | |||
[...t.querySelectorAll('th')].some(h => | |||
h.textContent.trim().match(/标题|曲目|名称/) | |||
) | |||
) | |||
if (table) { | |||
[...table.querySelectorAll('tr')].slice(1).forEach(row => { | |||
const cells = row.querySelectorAll('td, th') | const cells = row.querySelectorAll('td, th') | ||
if (cells.length >= 4) { | if (cells.length >= 4) { | ||
const titleCell = cells[cells.length >= 7 ? 1 : 0] | |||
const song = { | |||
title: titleCell.textContent.trim(), | |||
ez: (cells[4] || cells[1]).textContent.trim(), | |||
hd: (cells[5] || cells[2]).textContent.trim(), | |||
in: (cells[6] || cells[3]).textContent.trim() | |||
} | } | ||
const img = titleCell.querySelector('img') | |||
if (img) song.image = img.src || img.getAttribute('data-src') | |||
if (song.title && song.title !== '?' && song.title !== '') songs.push(song) | |||
} | } | ||
}) | }) | ||
} | } | ||
songs.length > 0 | |||
? loadSongColors(songs[Math.floor(Math.random() * songs.length)]) | |||
: useMockData() | |||
}) | }) | ||
.catch( | .catch(e => { | ||
console.error('获取曲目列表失败:', | console.error('获取曲目列表失败:', e) | ||
useMockData() | useMockData() | ||
}) | }) | ||
| 第127行: | 第76行: | ||
function loadSongColors(song) { | function loadSongColors(song) { | ||
fetch(`https://rizwiki.cn/wiki/${encodeURIComponent(song.title)}`) | fetch(`https://rizwiki.cn/wiki/${encodeURIComponent(song.title)}`) | ||
.then( | .then(r => r.ok ? r.text() : Promise.reject('页面不存在')) | ||
.then(html => { | .then(html => { | ||
const | const doc = new DOMParser().parseFromString(html, 'text/html') | ||
const getStyle = sel => doc.querySelector(sel)?.style.background | |||
const img = doc.querySelector('.infobox img') | |||
let imageUrl = img?.src || img?.getAttribute('data-src') | |||
const | if (imageUrl?.startsWith('/')) imageUrl = 'https://rizwiki.cn' + imageUrl | ||
const | |||
let imageUrl = | |||
updateSongInfo(song, { | updateSongInfo(song, { | ||
titleStyle: | titleStyle: getStyle('.infobox-title th') || '#94E1FF', | ||
headerStyle: | headerStyle: getStyle('.infobox-header th') || '#A3E5FF', | ||
labelStyle: | labelStyle: getStyle('.infobox-label') || '#B2E9FE', | ||
imageUrl: imageUrl, | imageUrl: imageUrl, | ||
pageUrl: `https://rizwiki.cn/wiki/${encodeURIComponent(song.title)}` | pageUrl: `https://rizwiki.cn/wiki/${encodeURIComponent(song.title)}` | ||
}) | }) | ||
}) | }) | ||
.catch( | .catch(e => { | ||
console.error('获取曲目颜色失败:', | console.error('获取曲目颜色失败:', e) | ||
updateSongInfo(song, { | updateSongInfo(song, { | ||
titleStyle: '#94E1FF', | titleStyle: '#94E1FF', | ||
| 第179行: | 第103行: | ||
}) | }) | ||
}) | }) | ||
} | } | ||
function updateSongInfo(song, data) { | function updateSongInfo(song, data) { | ||
document.querySelector('.infobox-title th').textContent = song.title | |||
document.querySelector('.infobox-title th').style.background = data.titleStyle | |||
document.querySelector('.infobox-header th').style.background = data.headerStyle | |||
document.querySelectorAll('.infobox-label').forEach(label => label.style.background = data.labelStyle) | |||
const container = document.querySelector('.infobox td[colspan="2"]') | |||
container.innerHTML = `<a href="${data.pageUrl}" target="_blank"> | |||
<img id="song-image" width="300" height="300" src="${data.imageUrl || '/images/thumb/4/41/插画图标.png/300px-插画图标.png'}"> | |||
</a>` | |||
window.difficultyArray = [ | window.difficultyArray = [ | ||
| 第245行: | 第120行: | ||
createDiffSpan("HD", song.hd), | createDiffSpan("HD", song.hd), | ||
createDiffSpan("IN", song.in), | createDiffSpan("IN", song.in), | ||
'<span style="width: max-content;text-align:center;display:inline-block;border-radius:1em;color:white;background | '<span style="width:max-content;text-align:center;display:inline-block;border-radius:1em;color:white;background:#53D6FF;padding:0 0.80em;margin:0.1em 0.50em">无限制</span>' | ||
] | ] | ||
| 第253行: | 第128行: | ||
function createDiffSpan(diff, level) { | function createDiffSpan(diff, level) { | ||
const colors = { EZ: '#57E4C4', HD: '#FDBA61', IN: '#FE8661' } | 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 | return `<span style="width:max-content;text-align:center;display:inline-block;border-radius:1em;color:white;background:${colors[diff]};padding:0 0.80em;margin:0.1em 0.50em">${diff} ${level}</span>` | ||
} | } | ||
function generateRandomRestrictions() { | function generateRandomRestrictions() { | ||
const | const speeds = ["无限制", "无限制", "无限制", ...Array.from({ length: 91 }, (_, i) => (i + 10) / 10)] | ||
document.getElementById('difficulty-data').innerHTML = window.difficultyArray[Math.floor(Math.random() * window.difficultyArray.length)] | |||
document.getElementById('speed-data').textContent = speeds[Math.floor(Math.random() * speeds.length)] | |||
document.getElementById('mod-data').textContent = ["无MOD"][Math.floor(Math.random() * 1)] | |||
document.getElementById('other-data').textContent = ["无限制"][Math.floor(Math.random() * 1)] | |||
document.getElementById('speed-data').textContent = | |||
document.getElementById('mod-data').textContent = | |||
document.getElementById('other-data').textContent = | |||
} | } | ||
function useMockData() { | function useMockData() { | ||
updateSongInfo({ | |||
title: "Pastel Lines", | title: "Pastel Lines", | ||
ez: "1", | ez: "1", | ||
hd: "6", | hd: "6", | ||
in: "11" | in: "11" | ||
} | }, { | ||
titleStyle: '#94E1FF', | titleStyle: '#94E1FF', | ||
headerStyle: '#A3E5FF', | headerStyle: '#A3E5FF', | ||
labelStyle: '#B2E9FE' | labelStyle: '#B2E9FE' | ||
}) | }) | ||
} | } | ||
</script> | </script> | ||