MediaWiki:Common.js:修订间差异

来自AG1444
无编辑摘要
无编辑摘要
第1行: 第1行:
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// ==== 全局背景音乐 ====
// ===== 全局背景音乐(稳定版)=====
window.addEventListener('load', () => {
mw.loader.using('mediawiki.util', function () {
 
     // 创建音频对象
     // 创建音频对象
     let bgm = new Audio('/bgm/music.mp3'); // <-- 替换成你的音乐URL
     const bgm = new Audio('/bgm/music.mp3'); // ← 确认这个 URL
     bgm.loop = true;       // 循环播放
     bgm.loop = true;
     bgm.volume = 0.5;     // 音量 0.0~1.0
     bgm.volume = 0.5;


     // 尝试自动播放
     // 创建按钮
     bgm.play().catch(() => {
     const btn = document.createElement('button');
        console.log('自动播放被浏览器阻止,需要用户点击按钮');
    btn.id = 'bgm-toggle-btn';
     });
     btn.textContent = '🎵 播放 BGM';


     // 创建右下角按钮
     // 按钮样式(右下角)
    let btn = document.createElement('button');
    btn.id = 'bgm-toggle-btn';
    btn.textContent = '🎵 BGM 开';
     btn.style.position = 'fixed';
     btn.style.position = 'fixed';
     btn.style.bottom = '10px';
     btn.style.right = '12px';
     btn.style.right = '10px';
     btn.style.bottom = '12px';
     btn.style.zIndex = '9999';
     btn.style.zIndex = '9999';
     btn.style.padding = '5px 10px';
     btn.style.padding = '6px 10px';
     btn.style.backgroundColor = '#444';
     btn.style.background = '#333';
     btn.style.color = '#fff';
     btn.style.color = '#fff';
     btn.style.border = 'none';
     btn.style.border = 'none';
     btn.style.borderRadius = '5px';
     btn.style.borderRadius = '6px';
     btn.style.cursor = 'pointer';
     btn.style.cursor = 'pointer';
    btn.style.opacity = '0.8';


     // 按钮点击事件:播放/暂停切换
     // 点击播放 / 暂停
     btn.addEventListener('click', () => {
     btn.addEventListener('click', function () {
         if (bgm.paused) {
         if (bgm.paused) {
             bgm.play();
             bgm.play();
             btn.textContent = '🎵 BGM ';
             btn.textContent = '⏸ 暂停 BGM';
         } else {
         } else {
             bgm.pause();
             bgm.pause();
             btn.textContent = '🎵 BGM ';
             btn.textContent = '🎵 播放 BGM';
         }
         }
     });
     });


     // 添加按钮到页面
     // 插入页面
     document.body.appendChild(btn);
     document.body.appendChild(btn);
    // 尝试自动播放(可能会被浏览器阻止,但不影响按钮)
    bgm.play().catch(() => {
        console.log('自动播放被浏览器阻止,等待用户点击');
    });
});
});

2026年1月27日 (二) 00:47的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// ===== 全局背景音乐(稳定版)=====
mw.loader.using('mediawiki.util', function () {

    // 创建音频对象
    const bgm = new Audio('/bgm/music.mp3'); // ← 确认这个 URL
    bgm.loop = true;
    bgm.volume = 0.5;

    // 创建按钮
    const btn = document.createElement('button');
    btn.id = 'bgm-toggle-btn';
    btn.textContent = '🎵 播放 BGM';

    // 按钮样式(右下角)
    btn.style.position = 'fixed';
    btn.style.right = '12px';
    btn.style.bottom = '12px';
    btn.style.zIndex = '9999';
    btn.style.padding = '6px 10px';
    btn.style.background = '#333';
    btn.style.color = '#fff';
    btn.style.border = 'none';
    btn.style.borderRadius = '6px';
    btn.style.cursor = 'pointer';
    btn.style.opacity = '0.8';

    // 点击播放 / 暂停
    btn.addEventListener('click', function () {
        if (bgm.paused) {
            bgm.play();
            btn.textContent = '⏸ 暂停 BGM';
        } else {
            bgm.pause();
            btn.textContent = '🎵 播放 BGM';
        }
    });

    // 插入页面
    document.body.appendChild(btn);

    // 尝试自动播放(可能会被浏览器阻止,但不影响按钮)
    bgm.play().catch(() => {
        console.log('自动播放被浏览器阻止,等待用户点击');
    });
});