MediaWiki:Gadget-AudioText.js:修订间差异
外观
![]() OctoberSama(留言 | 贡献) 小 |
![]() 小 |
||
第1行: | 第1行: | ||
// MediaWiki:Gadget-AudioText.js | // MediaWiki:Gadget-AudioText.js | ||
(function() { | (function() { | ||
mw.hook('wikipage.content').add(function() { | |||
alert("🎉 JS脚本已加载!"); // 1. 脚本加载确认 | |||
document.querySelectorAll('.audio-trigger').forEach(element => { | |||
element.addEventListener('click', function() { | |||
alert("🖱️ 点击事件已触发!"); // 2. 点击事件确认 | |||
const audioFile = element.dataset.audioFile; | |||
alert("📁 音频文件名: " + audioFile); // 3. 显示模板参数 | |||
// 构造API请求URL | |||
const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' + | |||
encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json'; | |||
alert("🔗 即将请求API: \n" + apiUrl); // 4. 显示API地址 | |||
// 发送API请求 | |||
fetch(apiUrl) | |||
.then(response => { | |||
alert("📡 API响应状态: " + response.status); // 5. HTTP状态码 | |||
return response.json(); | |||
}) | |||
.then(data => { | |||
alert("📦 API返回原始数据: \n" + JSON.stringify(data)); // 6. 原始API数据 | |||
const pages = data.query.pages; | |||
const pageId = Object.keys(pages)[0]; | |||
const audioUrl = pages[pageId].imageinfo[0].url; | |||
alert("🎵 解析出的音频URL: \n" + audioUrl); // 7. 最终音频地址 | |||
// 尝试播放 | |||
const audio = new Audio(audioUrl); | |||
audio.play() | |||
.then(() => alert("✅ 播放成功!")) | |||
.catch(e => alert("❌ 播放失败: " + e.message)); // 8. 播放结果 | |||
}) | |||
.catch(error => { | |||
alert("💥 发生严重错误: " + error.message); // 9. 全局错误捕获 | |||
}); | |||
}); | }); | ||
}); | }); | ||
} | }); | ||
})(); | })(); |
2025年5月24日 (六) 23:21的版本
// MediaWiki:Gadget-AudioText.js (function() { mw.hook('wikipage.content').add(function() { alert("🎉 JS脚本已加载!"); // 1. 脚本加载确认 document.querySelectorAll('.audio-trigger').forEach(element => { element.addEventListener('click', function() { alert("🖱️ 点击事件已触发!"); // 2. 点击事件确认 const audioFile = element.dataset.audioFile; alert("📁 音频文件名: " + audioFile); // 3. 显示模板参数 // 构造API请求URL const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' + encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json'; alert("🔗 即将请求API: \n" + apiUrl); // 4. 显示API地址 // 发送API请求 fetch(apiUrl) .then(response => { alert("📡 API响应状态: " + response.status); // 5. HTTP状态码 return response.json(); }) .then(data => { alert("📦 API返回原始数据: \n" + JSON.stringify(data)); // 6. 原始API数据 const pages = data.query.pages; const pageId = Object.keys(pages)[0]; const audioUrl = pages[pageId].imageinfo[0].url; alert("🎵 解析出的音频URL: \n" + audioUrl); // 7. 最终音频地址 // 尝试播放 const audio = new Audio(audioUrl); audio.play() .then(() => alert("✅ 播放成功!")) .catch(e => alert("❌ 播放失败: " + e.message)); // 8. 播放结果 }) .catch(error => { alert("💥 发生严重错误: " + error.message); // 9. 全局错误捕获 }); }); }); }); })();