MediaWiki:Gadget-AudioText.js:修订间差异
外观
![]() OctoberSama(留言 | 贡献) 小 |
![]() 小 |
||
第1行: | 第1行: | ||
(function() { | (function() { | ||
// | // 加载完成后初始化 | ||
const | mw.hook('wikipage.content').add(function() { | ||
document.querySelectorAll('.audio-trigger').forEach(function(element) { | |||
element.addEventListener('click', function() { | |||
const audioFile = element.getAttribute('data-audio-file'); | |||
// 获取音频文件的真实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; | |||
const audio = new Audio(audioUrl); | |||
audio.play().catch(error => { | |||
alert('播放失败,请点击页面其他位置后再试'); | |||
}); | |||
}); | |||
}); | }); | ||
}); | }); | ||
} | }); | ||
})(); | })(); |
2025年5月24日 (六) 21:18的版本
(function() { // 加载完成后初始化 mw.hook('wikipage.content').add(function() { document.querySelectorAll('.audio-trigger').forEach(function(element) { element.addEventListener('click', function() { const audioFile = element.getAttribute('data-audio-file'); // 获取音频文件的真实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; const audio = new Audio(audioUrl); audio.play().catch(error => { alert('播放失败,请点击页面其他位置后再试'); }); }); }); }); }); })();