MediaWiki:Gadget-AudioText.js:修订间差异
外观
![]() OctoberSama(留言 | 贡献) 小 |
![]() OctoberSama(留言 | 贡献) 小 |
||
第1行: | 第1行: | ||
// MediaWiki:Gadget-AudioText.js | // MediaWiki:Gadget-AudioText.js | ||
(function() { | (function() { | ||
// 预加载音频URL缓存 | |||
const audioUrlCache = new Map(); | |||
// 统一处理播放逻辑 | |||
function handlePlay(element) { | |||
const originalText = element.textContent; | |||
// 显示加载状态 | |||
element.textContent = "加载中..."; | |||
element.style.color = "#999"; | |||
const audioFile = element.dataset.audioFile; | |||
// 优先使用缓存 | |||
if (audioUrlCache.has(audioFile)) { | |||
playAudio(audioUrlCache.get(audioFile), element, originalText); | |||
return; | |||
} | |||
// 通过API获取音频真实URL | |||
const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' + | |||
encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json'; | |||
fetch(apiUrl) | |||
.then(response => response.json()) | |||
.then(data => { | |||
const pages = data.query.pages; | |||
const pageId = Object.keys(pages)[0]; | |||
const audioUrl = pages[pageId].imageinfo[0]?.url; | |||
if (!audioUrl) { | |||
throw new Error("未找到音频文件"); | |||
} | |||
// 缓存URL并播放 | |||
audioUrlCache.set(audioFile, audioUrl); | |||
playAudio(audioUrl, element, originalText); | |||
}) | |||
.catch(() => { | |||
// 失败状态提示 | |||
element.textContent = "加载失败,点击重试"; | |||
element.style.color = "red"; | |||
}); | |||
} | |||
function playAudio(url, element, originalText) { | |||
const audio = new Audio(url); | |||
audio.play().then(() => { | |||
// 播放成功恢复状态 | |||
element.textContent = originalText; | |||
element.style.color = "blue"; | |||
}).catch(() => { | |||
// 静默失败,不干扰用户 | |||
element.textContent = originalText; | |||
element.style.color = "blue"; | |||
}); | |||
} | |||
// 事件绑定(支持移动端) | |||
function initAudioTriggers() { | |||
document.querySelectorAll('.audio-trigger').forEach(element => { | |||
if (element._audioBound) return; | |||
element._audioBound = true; | |||
const handler = (event) => { | |||
event.preventDefault(); | |||
handlePlay(element); | |||
}; | |||
element.addEventListener('touchstart', handler, { passive: false }); | |||
element.addEventListener('click', handler); | |||
}); | }); | ||
}); | } | ||
// 初始化 | |||
mw.hook('wikipage.content').add(initAudioTriggers); | |||
mw.loader.using('mediawiki.api').then(initAudioTriggers); | |||
})(); | })(); |
2025年5月24日 (六) 23:19的版本
// MediaWiki:Gadget-AudioText.js (function() { // 预加载音频URL缓存 const audioUrlCache = new Map(); // 统一处理播放逻辑 function handlePlay(element) { const originalText = element.textContent; // 显示加载状态 element.textContent = "加载中..."; element.style.color = "#999"; const audioFile = element.dataset.audioFile; // 优先使用缓存 if (audioUrlCache.has(audioFile)) { playAudio(audioUrlCache.get(audioFile), element, originalText); return; } // 通过API获取音频真实URL const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' + encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json'; fetch(apiUrl) .then(response => response.json()) .then(data => { const pages = data.query.pages; const pageId = Object.keys(pages)[0]; const audioUrl = pages[pageId].imageinfo[0]?.url; if (!audioUrl) { throw new Error("未找到音频文件"); } // 缓存URL并播放 audioUrlCache.set(audioFile, audioUrl); playAudio(audioUrl, element, originalText); }) .catch(() => { // 失败状态提示 element.textContent = "加载失败,点击重试"; element.style.color = "red"; }); } function playAudio(url, element, originalText) { const audio = new Audio(url); audio.play().then(() => { // 播放成功恢复状态 element.textContent = originalText; element.style.color = "blue"; }).catch(() => { // 静默失败,不干扰用户 element.textContent = originalText; element.style.color = "blue"; }); } // 事件绑定(支持移动端) function initAudioTriggers() { document.querySelectorAll('.audio-trigger').forEach(element => { if (element._audioBound) return; element._audioBound = true; const handler = (event) => { event.preventDefault(); handlePlay(element); }; element.addEventListener('touchstart', handler, { passive: false }); element.addEventListener('click', handler); }); } // 初始化 mw.hook('wikipage.content').add(initAudioTriggers); mw.loader.using('mediawiki.api').then(initAudioTriggers); })();