跳转到内容

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

来自电棍ottowiki
第1行: 第1行:
// MediaWiki:Gadget-AudioText.js
// MediaWiki:Gadget-AudioText.js
(function() {
(function() {
     function init() {
     mw.hook('wikipage.content').add(function() {
         alert("JS已加载!"); // [!code focus]
         alert("🎉 JS脚本已加载!"); // 1. 脚本加载确认
       
 
         document.querySelectorAll('.audio-trigger').forEach(element => {
         document.querySelectorAll('.audio-trigger').forEach(element => {
             element.onclick = function() {
             element.addEventListener('click', function() {
                 alert("点击事件触发!"); // [!code focus]
                 alert("🖱️ 点击事件已触发!"); // 2. 点击事件确认
                  
                  
                 const audio = new Audio("https://wiki.ottohub.cn/images/c/ca/%E5%93%87%E8%A2%84.ogg"); // 硬编码URL
                 const audioFile = element.dataset.audioFile;
                audio.play().then(() => {
                alert("📁 音频文件名: " + audioFile); // 3. 显示模板参数
                    alert("播放成功!");
 
                }).catch(e => {
                // 构造API请求URL
                    alert("播放失败: " + e.message);
                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. 全局错误捕获
                    });
             });
         });
         });
     }
     });
   
    mw.hook('wikipage.content').add(init);
    mw.loader.using('mediawiki.api').then(init);
})();
})();

2025年5月24日 (六) 21:59的版本

// 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. 全局错误捕获
                    });
            });
        });
    });
})();