跳转到内容

MediaWiki:Common.js:修订间差异

来自天明的百科全书
Ztm0929留言 | 贡献
无编辑摘要
标签已被回退
Ztm0929留言 | 贡献
无编辑摘要
 
(未显示同一用户的23个中间版本)
第29行: 第29行:
});
});


$(function() {
    $('#searchInput').attr('accesskey', '/');
});


/* 更新页脚“最后编辑时间” —— 稳定版 */
// logo 切换逻辑
(function () {
$(function () {
  'use strict';
    // 获取当前“实际使用”的主题:'dark' 或 'light'
    function getEffectiveTheme() {
        var cls = document.documentElement.classList;
        if (cls.contains('skin-theme-clientpref-night')) return 'dark';
        if (cls.contains('skin-theme-clientpref-day')) return 'light';
        if (cls.contains('skin-theme-clientpref-os')) {
            if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                return 'dark';
            }
            return 'light';
        }
        // 兜底:根据系统偏好判断
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            return 'dark';
        }
        return 'light';
    }


  // 仅在普通查看页生效
    // 如果元素存在就设置 src(并记录日志)
  if (mw.config.get('wgAction') !== 'view') return;
    function setSrcIfExists(selector, src) {
        var $el = $(selector);
        if ($el.length) {
            $el.attr('src', src);
            console.log('[logo-switch] set', selector, '->', src);
        } else {
            console.log('[logo-switch] not found', selector);
        }
    }


  var weekdays = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
    // 主更新函数 —— 每次都重新 select 元素
  function pad(n){ return n < 10 ? '0' + n : '' + n; }
    function updateLogos() {
  function daysDiff(a, b) {
        var theme = getEffectiveTheme();
    var A = new Date(a.getFullYear(), a.getMonth(), a.getDate());
        if (theme === 'dark') {
    var B = new Date(b.getFullYear(), b.getMonth(), b.getDate());
            setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa-dark.svg');
    return Math.round((B - A) / 86400000);
            setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki-dark.svg');
  }
            setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai-dark.svg');
            setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw-dark.svg');  
        } else {
            setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa.svg');
            setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki.svg');
            setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai.svg');
            setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw.svg');  
        }
    }


  function updateLastmod() {
     // 首次运行
     var ts = mw.config.get('wgRevisionTimestamp'); // 形如 '20250904022639'
     updateLogos();
    var $lastmod = $('#footer-info-lastmod');
     if (!$lastmod.length || !ts) return;


     var = +ts.slice(0, 4);
    // 监听 <html> class 变化(用户在 UI 切换浅/深/自动)
    var mo = +ts.slice(4, 6) - 1;
     var htmlObserver = new MutationObserver(function (mutations) {
    var d  = +ts.slice(6, 8);
        for (var i = 0; i < mutations.length; i++) {
    var hh = +ts.slice(8,10);
            if (mutations[i].attributeName === 'class') {
     var mm = +ts.slice(10,12);
                updateLogos();
     var editDate = new Date(y, mo, d, hh, mm);
                break;
    var now = new Date();
            }
        }
     });
     htmlObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });


     var diff = daysDiff(editDate, now);
    // 监听 body 的 DOM 变动,防止 footer 异步插入或被替换
    var rel = diff === 0 ? '今天' : (diff === 1 ? '昨天' : diff + '天前');
     var bodyObserver = new MutationObserver(function () {
     var text = '此页面最后编辑于' + rel + ',' +
        updateLogos();
              y + '年' + pad(mo + 1) + '月' + pad(d) +
     });
              '日(' + weekdays[editDate.getDay()] + ') ' +
    bodyObserver.observe(document.body, { childList: true, subtree: true });
              pad(hh) + ':' + pad(mm);


     $lastmod.text(text);
     // 监听系统主题变化(适用于 skin-theme-clientpref-os)
  }
    if (window.matchMedia) {
        var mq = window.matchMedia('(prefers-color-scheme: dark)');
        if (typeof mq.addEventListener === 'function') {
            mq.addEventListener('change', updateLogos);
        } else if (typeof mq.addListener === 'function') {
            // 兼容旧浏览器
            mq.addListener(updateLogos);
        }
    }


  function schedule() {
     // 如果存在 MediaWiki 的 hook(例如偏好保存后),也触发一次
     // 让位于皮肤的异步更新先完成,再改文案
     if (window.mw && mw.hook) {
     setTimeout(updateLastmod, 0);
        mw.hook('user.preferencesSaved').add(updateLogos);
    (window.requestIdleCallback || setTimeout)(updateLastmod, 200);
    }
  }


  // 首次加载
    // 兜底:页面 load 后、以及延迟 1.5s 再试一次
  $(schedule);
    $(window).on('load', updateLogos);
    setTimeout(updateLogos, 1500);
});


  // 每次内容区加载(包括 AJAX 导航)
  mw.hook('wikipage.content').add(schedule);


  // 前进/后退
  window.addEventListener('popstate', schedule);


  // 监听 pushState(Vector 内部导航)
  (function patchPushState(){
    var ps = history.pushState;
    history.pushState = function() {
      var ret = ps.apply(this, arguments);
      schedule();
      return ret;
    };
  })();


  // 兜底:若皮肤异步替换了页脚节点,观察到后再更新
// 移除深色模式反馈提示
  var footer = document.getElementById('footer');
$(function () {
  if (footer && window.MutationObserver) {
     function removeBetaNotice() {
     new MutationObserver(function(muts){
        var $node = $('#skin-theme-beta-notice');
      for (var i = 0; i < muts.length; i++) {
         if ($node.length) {
        var added = muts[i].addedNodes;
            $node.remove();
         for (var j = 0; j < added.length; j++) {
            // 如果移除了,就断开观察器(可选)
          var n = added[j];
            if (observer) {
          if ((n.id && n.id === 'footer-info-lastmod') ||
                observer.disconnect();
              (n.querySelector && n.querySelector('#footer-info-lastmod'))) {
             }
            schedule();
             return;
          }
         }
         }
      }
    }
     }).observe(footer, { childList: true, subtree: true });
 
  }
    // 第一次尝试
})();
    removeBetaNotice();
 
    // 设置 MutationObserver 观察 body 或 document.body,监视 childList 增删
    var observer = new MutationObserver(function (mutations) {
        // 每次改变 DOM 时尝试移除
        removeBetaNotice();
     });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
 
    // 作为兜底:在页面加载完成后一段时间再试一次
    $(window).on('load', function () {
        removeBetaNotice();
    });
    // 再加一个延时
    setTimeout(removeBetaNotice, 2000);
});
 
$(function () {
    // 移除上传文件、固定链接、引用此页
    $('#t-upload, #t-permalink, #t-cite').remove();
});

2025年10月2日 (四) 15:57的最新版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// Cloudflare Web Analytics
var script = document.createElement('script');
script.defer = true;
script.src = 'https://static.cloudflareinsights.com/beacon.min.js';
script.setAttribute('data-cf-beacon', '{"token": "6180aa28fe1943b48c4059a5056f4738"}');
document.head.appendChild(script);
// End Cloudflare Web Analytics

$(function () {
    // 只在 Special:Search 页面运行
    if (mw.config.get("wgCanonicalSpecialPageName") === "Search") {
        const searchTerm = mw.util.getParamValue("search");
        const googleUrl = "https://www.google.com/search?q=" + encodeURIComponent(searchTerm);

        const $googleLink = $("<p>").html(
            `你也可以到 <a href="${googleUrl}" target="_blank" rel="noopener">Google 中搜索“${searchTerm}”</a>。`
        );

        // 优先在有搜索结果时插入
        if ($(".mw-search-results").length) {
            $googleLink.insertAfter(".mw-search-results");
        }
        // 否则在没有结果提示后插入
        else if ($(".mw-search-nonefound").length) {
            $googleLink.insertAfter(".mw-search-nonefound");
        }
    }
});


// logo 切换逻辑
$(function () {
    // 获取当前“实际使用”的主题:'dark' 或 'light'
    function getEffectiveTheme() {
        var cls = document.documentElement.classList;
        if (cls.contains('skin-theme-clientpref-night')) return 'dark';
        if (cls.contains('skin-theme-clientpref-day')) return 'light';
        if (cls.contains('skin-theme-clientpref-os')) {
            if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                return 'dark';
            }
            return 'light';
        }
        // 兜底:根据系统偏好判断
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            return 'dark';
        }
        return 'light';
    }

    // 如果元素存在就设置 src(并记录日志)
    function setSrcIfExists(selector, src) {
        var $el = $(selector);
        if ($el.length) {
            $el.attr('src', src);
            console.log('[logo-switch] set', selector, '->', src);
        } else {
            console.log('[logo-switch] not found', selector);
        }
    }

    // 主更新函数 —— 每次都重新 select 元素
    function updateLogos() {
        var theme = getEffectiveTheme();
        if (theme === 'dark') {
            setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa-dark.svg');
            setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki-dark.svg');
            setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai-dark.svg');
            setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw-dark.svg'); 
        } else {
            setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa.svg');
            setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki.svg');
            setSrcIfExists('#footer-assistedbyico img', '/resources/assets/assistedby-ai.svg');
            setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw.svg'); 
        }
    }

    // 首次运行
    updateLogos();

    // 监听 <html> class 变化(用户在 UI 切换浅/深/自动)
    var htmlObserver = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            if (mutations[i].attributeName === 'class') {
                updateLogos();
                break;
            }
        }
    });
    htmlObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });

    // 监听 body 的 DOM 变动,防止 footer 异步插入或被替换
    var bodyObserver = new MutationObserver(function () {
        updateLogos();
    });
    bodyObserver.observe(document.body, { childList: true, subtree: true });

    // 监听系统主题变化(适用于 skin-theme-clientpref-os)
    if (window.matchMedia) {
        var mq = window.matchMedia('(prefers-color-scheme: dark)');
        if (typeof mq.addEventListener === 'function') {
            mq.addEventListener('change', updateLogos);
        } else if (typeof mq.addListener === 'function') {
            // 兼容旧浏览器
            mq.addListener(updateLogos);
        }
    }

    // 如果存在 MediaWiki 的 hook(例如偏好保存后),也触发一次
    if (window.mw && mw.hook) {
        mw.hook('user.preferencesSaved').add(updateLogos);
    }

    // 兜底:页面 load 后、以及延迟 1.5s 再试一次
    $(window).on('load', updateLogos);
    setTimeout(updateLogos, 1500);
});




// 移除深色模式反馈提示
$(function () {
    function removeBetaNotice() {
        var $node = $('#skin-theme-beta-notice');
        if ($node.length) {
            $node.remove();
            // 如果移除了,就断开观察器(可选)
            if (observer) {
                observer.disconnect();
            }
        }
    }

    // 第一次尝试
    removeBetaNotice();

    // 设置 MutationObserver 观察 body 或 document.body,监视 childList 增删
    var observer = new MutationObserver(function (mutations) {
        // 每次改变 DOM 时尝试移除
        removeBetaNotice();
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 作为兜底:在页面加载完成后一段时间再试一次
    $(window).on('load', function () {
        removeBetaNotice();
    });
    // 再加一个延时
    setTimeout(removeBetaNotice, 2000);
});

$(function () {
    // 移除上传文件、固定链接、引用此页
    $('#t-upload, #t-permalink, #t-cite').remove();
});