MediaWiki:Common.js
MediaWiki界面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl + F5或Ctrl + R(Mac为⌘ R)
- Google Chrome:按Ctrl + Shift + R(Mac为⌘ Shift R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl + F5。
/* <pre> */
"use strict";
(async () => {
/* 函数定义体 */
const { wgUserGroups, wgServer, wgScriptPath, wgPageName, wgAction, wgNamespaceNumber, wgMainPageTitle } = mw.config.get();
/* 以下为允许添加版权声明的命名空间列表 */
const copyRightsNameSpaces = [
0, // (主)
4, // Vocawiki
8, // MediaWiki
10, // Template
12, // Help
274, // Widget
828, // Module
];
/* 检查是否为管理员 */
const allowedInGroup = wgUserGroups.includes('sysop');
/* MediaViewer#populateStatsFromXhr 错误屏蔽 */
const { getResponseHeader } = XMLHttpRequest.prototype;
XMLHttpRequest.prototype.getResponseHeader = function (name) {
return `\n${this.getAllResponseHeaders().toLowerCase()}`.includes(`\n${name.toLowerCase()}: `) ? getResponseHeader.bind(this)(name) : (console.info(`Refused to get unsafe header "${name}"\n`, this, "\n", new Error().stack), null);
};
// 页顶公告自动滚动
function startScroll() {
$("#siteNotice .scrollDiv:not(.scrolling)").addClass("scrolling").each(function (_, ele) {
var self = $(ele);
self.children().each(function (_, child) {
if (child.innerHTML.trim() === "") {
child.remove();
}
});
var children = self.children();
if (children.length === 0) {
return;
}
var firstChild = children.first();
var firstChildHeight = firstChild.outerHeight();
self.height(firstChildHeight);
children.slice(1).css("top", "".concat(Math.ceil(firstChildHeight), "px"));
firstChild.css("top", "0");
});
}
function autoScroll() {
setInterval(function () {
if (!document.hidden) {
$("#siteNotice .scrollDiv.scrolling").each(function (_, ele) {
var self = $(ele);
if (self.css("font-weight") === "700") {
return;
}
var children = self.children();
var all = self.add(children);
if (children.length === 1) {
return;
}
var firstChild = children.first();
var firstChildHeight = firstChild.outerHeight();
var secondChild = firstChild.next();
var secondChildHeight = secondChild.outerHeight();
var otherChild = children.slice(2);
var maxHeight = Math.max(firstChildHeight, secondChildHeight);
otherChild.each(function (_, child) {
maxHeight = Math.max(maxHeight, $(child).outerHeight());
});
all.addClass("animation");
self.height(maxHeight);
firstChild.css("top", "-".concat(firstChildHeight, "px"));
secondChild.css("top", "".concat((maxHeight - secondChildHeight) / 2, "px"));
otherChild.css("top", "".concat(Math.ceil(maxHeight), "px"));
setTimeout(function () {
all.removeClass("animation");
firstChild.appendTo(self).css("top", Math.ceil(maxHeight));
}, 400);
});
}
}, 5000);
}
// Extension:MultimediaViewer的半透明化修改,用于保持背景文字处于原位,本应修改插件达成的,暂时先用JavaScript应急处理下
const multimediaViewer = () => {
const _scrollTo = window.scrollTo;
let flag = location.hash.startsWith("#/media/");
window.scrollTo = (xORoption, y) => {
if (flag) {
console.info("Prevent multimediaViewer called");
}
else if (y === undefined) {
_scrollTo(xORoption);
}
else {
_scrollTo(xORoption, y);
}
};
setInterval(() => {
$("a.image img[data-file-width], a.image img[data-file-height], .mw-mmv-filepage-buttons a.mw-mmv-view-expanded").not(".multimediaViewerScrollSet").each((_, ele) => {
ele.addEventListener("click", (e) => {
if ($(e.target).closest(".TabLabelText")[0] || $(e.target).closest("a").closest(".mw-customtoggle")[0]) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
$($(e.target).closest(".TabLabelText")[0] || $(e.target).closest("a").closest(".mw-customtoggle")[0]).trigger("click");
return false;
}
flag = true;
}, {
capture: true,
});
$(ele).addClass("multimediaViewerScrollSet");
});
if (document.querySelector(".mw-mmv-close")) {
if (mw.config.get("wgMultimediaViewerInjected") !== "on") {
mw.config.set("wgMultimediaViewerInjected", "on");
$(".mw-mmv-image").off("click")[0].addEventListener("click", (e) => {
if ($(e.target).is("img")) {
window.open($(e.target).attr("src").replace(/(voca\.wiki\/images\/)thumb\/([a-z\d]+\/[a-z\d]+\/)([^/]+)\/\d+px-\3/i, "$1$2$3"), "_blank").focus();
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
return false;
}
else if ($(e.target).closest(".error-box")[0]) {
return;
}
$(".mw-mmv-close").trigger("click");
}, {
capture: true,
});
}
}
else if (mw.config.get("wgMultimediaViewerInjected") === "on") {
flag = false;
mw.config.set("wgMultimediaViewerInjected", "off");
}
}, 137);
};
// 跨站重定向页顶链接
const crossDomainLink = (url) => {
const link = url.query.title;
const domain = url.host;
const crossDomain = $("<div/>");
const anchor = $("<a/>");
crossDomain.text("<");
anchor.attr("href", `${url}`).text(`${link} 【来自 ${domain} 的跨站重定向】`);
crossDomain.append(anchor);
$("#contentSub").prepend(crossDomain);
};
// 复制修改内容
const copyRights = async () => {
await mw.loader.using("mediawiki.util");
const div = $("<div>", {
"class": "visually-hidden",
html: `<pre></pre><br>\n阅读更多:${/%/.test(mw.util.wikiUrlencode(wgPageName)) ? `${wgPageName}(${wgServer}${wgScriptPath}/${encodeURIComponent(wgPageName)} )` : `${wgServer}${wgScriptPath}/${wgPageName}`}<br>\n本文引自Vocawiki(${wgServer.replace(/^\/\//, "https://")}),文字内容默认使用《知识共享 署名—非商业性使用—相同方式共享 4.0 国际》协议。`,
}).appendTo("body"), valueNode = div.find("pre");
$("#mw-content-text").on("copy", () => {
const selection = window.getSelection(), value = selection.toString(), range = selection.getRangeAt(0);
if (!value.length || value.length < 128 // 当复制内容为空或长度小于定值时不添加声明
|| $(selection.anchorNode).add(selection.basenode).add(selection.focusNode).closest(".Wikiplus-InterBox")[0]) {
return;
}
valueNode.text(value);
selection.selectAllChildren(div[0]);
window.setTimeout(() => { // 以下将还原选区
selection.removeAllRanges();
selection.addRange(range);
valueNode.empty();
}, 0);
});
};
// 跨站重定向页顶链接
const crossDomainDetect = async () => {
await mw.loader.using(["mediawiki.Uri"]);
const { rdfrom } = new mw.Uri().query;
if (rdfrom) {
const rdfromUri = new mw.Uri(rdfrom);
if (!rdfromUri.host.includes([104, 109, 111, 101].map((n) => String.fromCharCode(n)).join(""))) {
const { query } = rdfromUri;
if (query.title && query.redirect === "no") {
crossDomainLink(rdfromUri);
}
}
}
};
// 修正hash跳转错误
const hashJump = async () => {
await mw.loader.using(["jquery.makeCollapsible"]);
$(".mw-collapsible").makeCollapsible();
const { hash } = location;
location.hash = "";
location.hash = hash;
};
/* 函数执行体 */
await $.ready;
// 页顶公告自动滚动
startScroll();
autoScroll();
// Extension:MultimediaViewer的半透明化修改
multimediaViewer();
// Add "mainpage" class to the body element
if (wgMainPageTitle === wgPageName && wgAction === "view") {
$("body").addClass("mainpage");
}
// 复制内容版权声明
if (window.getSelection && !allowedInGroup && !["edit", "submit"].includes(wgAction) && copyRightsNameSpaces.includes(wgNamespaceNumber)) {
copyRights();
}
// 修复代码编辑器$.ucFirst引用错误
$.extend({
ucFirst: (_s) => {
const s = `${_s}`;
return s.charAt(0).toUpperCase() + s.substring(1);
},
});
// 注释内列表
$(".reference-text > ul,.reference-text > ol").each((_, ele) => {
if (ele.parentElement.childNodes[0] === ele) {
$(ele).addClass("listInRef");
}
});
// 修正hash跳转错误
if ($(".mw-collapsible")[0] && location.hash !== "") {
hashJump();
}
// 跨站重定向页顶链接
crossDomainDetect();
// 禁止移动被挂删的页面
if (!allowedInGroup && $(".regToDel")[0]) {
$("#ca-move").remove();
}
})();
/* </pre> */