MediaWiki:Common.js
跳转到导航
跳转到搜索
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */ function importScriptURI (url) { mw.loader.load(url); } function importScript (page) { var uri = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + mw.util.wikiUrlencode(page) + '&action=raw&ctype=text/javascript'; return importScriptURI(uri); } function importStylesheetURI (url) { mw.loader.load(url, 'text/css'); } function importStylesheet (page) { var uri = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + mw.util.wikiUrlencode(page) + '&action=raw&ctype=text/css'; return importStylesheetURI(uri); } // 时间差计算 $(document).ready(function() { function formatTimeDiff(diff) { var parts = []; if (diff.years > 0) parts.push(diff.years + "年"); if (diff.months > 0) parts.push(diff.months + "个月"); if (diff.days > 0) parts.push(diff.days + "天"); parts.push(diff.hours + "小时"); parts.push(diff.minutes + "分"); parts.push(diff.seconds + "秒"); return parts.join(""); } function calculateDiff(targetDate) { var now = new Date(); var diff = { years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0 }; // 计算时间差异的绝对值 var timeDiff = now.getTime() - targetDate.getTime(); if (timeDiff < 0) return diff; // 处理未来时间 // 计算年、月、日(基于UTC日期) var startUTC = Date.UTC(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate()); var endUTC = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()); // 临时变量用于逐步计算 var tempDate = new Date(targetDate); diff.years = now.getUTCFullYear() - tempDate.getUTCFullYear(); tempDate.setUTCFullYear(tempDate.getUTCFullYear() + diff.years); if (tempDate > now) { diff.years--; tempDate.setUTCFullYear(tempDate.getUTCFullYear() - 1); } diff.months = now.getUTCMonth() - tempDate.getUTCMonth(); if (diff.months < 0) { diff.months += 12; } tempDate.setUTCMonth(tempDate.getUTCMonth() + diff.months); if (tempDate > now) { diff.months--; tempDate.setUTCMonth(tempDate.getUTCMonth() - 1); } diff.days = Math.floor((now.getTime() - tempDate.getTime()) / (1000 * 60 * 60 * 24)); // 计算小时、分钟、秒 var remainder = now.getTime() - tempDate.getTime() - diff.days * 1000 * 60 * 60 * 24; diff.hours = Math.floor(remainder / (1000 * 60 * 60)); remainder %= (1000 * 60 * 60); diff.minutes = Math.floor(remainder / (1000 * 60)); remainder %= (1000 * 60); diff.seconds = Math.floor(remainder / 1000); return diff; } function updateAllTimeDifferences() { $('.time-difference').each(function() { var $this = $(this); var targetDateStr = $this.data('target-date'); var targetDate = new Date(targetDateStr); if (isNaN(targetDate.getTime())) { $this.text("日期格式无效"); return; } var diff = calculateDiff(targetDate); $this.text(formatTimeDiff(diff)); }); } // 初始更新并设置定时器 updateAllTimeDifferences(); setInterval(updateAllTimeDifferences, 1000); }); // 折叠功能实现 $(document).ready(function() { $('.custom-collapsible .collapsible-header').click(function() { const $header = $(this); const isOpen = $header.attr('data-state') === 'open'; $header .attr('data-state', isOpen ? 'closed' : 'open') .closest('.custom-collapsible') .find('.collapsible-content') .toggleClass('force-show'); }); }); // 世界观wiki接力 $(document).ready(function() { $('.random-button').click(function() { var container = $(this).closest('.random-container'); var urls = container.data('urls').split('|'); var randomIndex = Math.floor(Math.random() * urls.length); window.location.href = urls[randomIndex]; }); }); // 随机页面盒子 // 调取API及切换按钮 $(function() { function loadRandomContent($container) { var api = new mw.Api(); // 第一步:获取随机页面标题 api.get({ action: 'query', generator: 'random', grnnamespace: 0, grnlimit: 1, formatversion: 2 }).then(function(data) { var title = data.query.pages[0].title; // 第二步:获取页面解析后的HTML return api.get({ action: 'parse', page: title, prop: 'text', formatversion: 2 }); }).done(function(data) { $container.html(data.parse.text); $container.attr('data-loaded', 'true'); }).fail(function(e) { $container.html('❌ 加载失败: ' + String(e)); }); } // 为所有随机页面容器绑定事件 $('.common-random-box').each(function() { var $box = $(this); var $content = $box.find('.common-random-content'); var $button = $box.find('.common-random-refresh'); // 初始加载 if ($content.attr('data-loaded') === 'false') { loadRandomContent($content); } // 按钮点击事件 $button.on('click', function() { $content.attr('data-loaded', 'false').html('⏳ 加载中...'); loadRandomContent($content); }); }); }); // 修改common.js的loadRandomContent函数 function loadRandomContent($container) { var $box = $container.closest('.common-random-box'); var $header = $box.find('.common-random-header'); var $navButton = $box.find('.common-random-nav'); var api = new mw.Api(); // 重置状态 $box.removeClass('loaded'); $navButton.hide().attr('href', '#'); api.get({ action: 'query', generator: 'random', grnnamespace: 0, grnlimit: 1, formatversion: 2 }).then(function(data) { var page = data.query.pages[0]; // 更新标题和导航按钮 $header.html('📄 ' + page.title); var pageTitle = new mw.Title(page.title); $navButton.attr('href', pageTitle.getUrl()).show(); return api.get({ action: 'parse', page: page.pageid, prop: 'text', formatversion: 2 }); }).done(function(data) { $container.html(data.parse.text); $container.attr('data-loaded', 'true'); $box.addClass('loaded'); // 显示跳转按钮 }).fail(function(e) { $header.html('❌ 加载失败'); $container.html('错误详情:' + String(e)); }); } // 显示用户名称 ;(function ($, mw) { 'use strict'; var username = mw.config.get('wgUserName'); if ( window.disableUsernameReplace || !username ) { return; } window.disableUsernameReplace = true; var $rail = $('#WikiaRail'), customSelector = window.UsernameReplaceSelector ? ', ' + window.UsernameReplaceSelector : ''; function inputUsername($content) { $content.find('.InputUsername, .insertusername' + customSelector).text(username); } mw.hook('wikipage.content').add(inputUsername); if ($rail.hasClass('loaded')) { inputUsername($rail); } else if ($rail.length) { $rail.on('afterLoad.rail', inputUsername.bind(null, $rail) ); } })(window.jQuery, window.mediaWiki);