跳转到内容

MediaWiki:Gadget-AudioText.js:修订间差异

来自电棍ottowiki
第2行: 第2行:
(function() {
(function() {
     mw.hook('wikipage.content').add(function() {
     mw.hook('wikipage.content').add(function() {
        alert("🎉 JS脚本已加载!"); // 1. 脚本加载确认
         document.querySelectorAll('.audio-trigger').forEach(element => {
         document.querySelectorAll('.audio-trigger').forEach(element => {
             element.addEventListener('click', function() {
             element.addEventListener('click', async function(event) {
                 alert("🖱️ 点击事件已触发!"); // 2. 点击事件确认
                 event.preventDefault();
                  
                  
                // 1. 立即创建音频对象并触发播放请求(占位)
                const audio = new Audio();
                audio.autoplay = true; // 关键:声明自动播放意图
                audio.play().catch(() => {}); // 静默忽略首次错误
                // 2. 异步获取真实URL
                 const audioFile = element.dataset.audioFile;
                 const audioFile = element.dataset.audioFile;
                alert("📁 音频文件名: " + audioFile); // 3. 显示模板参数
                // 构造API请求URL
                 const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' +  
                 const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' +  
                     encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json';
                     encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json';
                alert("🔗 即将请求API: \n" + apiUrl); // 4. 显示API地址


                 // 发送API请求
                 try {
                fetch(apiUrl)
                    const response = await fetch(apiUrl);
                     .then(response => {
                     const data = await response.json();
                        alert("📡 API响应状态: " + response.status); // 5. HTTP状态码
                     const pages = data.query.pages;
                        return response.json();
                    const pageId = Object.keys(pages)[0];
                     })
                    const audioUrl = pages[pageId].imageinfo[0].url;
                    .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. 最终音频地址


                        // 尝试播放
                    // 3. 替换音频源并重新播放
                        const audio = new Audio(audioUrl);
                    audio.src = audioUrl;
                        audio.play()
                    await audio.play();
                            .then(() => alert("✅ 播放成功!"))
                } catch (error) {
                            .catch(e => alert("❌ 播放失败: " + e.message)); // 8. 播放结果
                    alert("❌ 终极错误: " + error.message);
                    })
                }
                    .catch(error => {
                        alert("💥 发生严重错误: " + error.message); // 9. 全局错误捕获
                    });
             });
             });
         });
         });
     });
     });
})();
})();

2025年5月24日 (六) 22:15的版本

// MediaWiki:Gadget-AudioText.js
(function() {
    mw.hook('wikipage.content').add(function() {
        document.querySelectorAll('.audio-trigger').forEach(element => {
            element.addEventListener('click', async function(event) {
                event.preventDefault();
                
                // 1. 立即创建音频对象并触发播放请求(占位)
                const audio = new Audio();
                audio.autoplay = true; // 关键:声明自动播放意图
                audio.play().catch(() => {}); // 静默忽略首次错误

                // 2. 异步获取真实URL
                const audioFile = element.dataset.audioFile;
                const apiUrl = mw.util.wikiScript('api') + '?action=query&titles=' + 
                    encodeURIComponent(audioFile) + '&prop=imageinfo&iiprop=url&format=json';

                try {
                    const response = await fetch(apiUrl);
                    const data = await response.json();
                    const pages = data.query.pages;
                    const pageId = Object.keys(pages)[0];
                    const audioUrl = pages[pageId].imageinfo[0].url;

                    // 3. 替换音频源并重新播放
                    audio.src = audioUrl;
                    await audio.play();
                } catch (error) {
                    alert("❌ 终极错误: " + error.message);
                }
            });
        });
    });
})();