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

删除的内容 添加的内容
无编辑摘要
 
(未显示1个用户的2个中间版本)
第1行:
/**
(function($, mw) {
* SPDX-License-Identifier: CC-BY-SA-4.0
// 不要太激进,只修复一些明显的输入错误
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
// 例如,将中文冒号“:”修正为英文冒号“:”,这在命名空间搜索中很常见
*
* @base {@link https://zh.wikipedia.org/wiki/MediaWiki:Gadget-fullwidth-search-fix.js}
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/FullwidthSearchFix}
* @license CC-BY-SA-4.0 {@link https://www.qiuwenbaike.cn/wiki/H:CC-BY-SA-4.0}
*/
/**
* +------------------------------------------------------------+
* | === WARNING: GLOBAL GADGET FILE === |
* +------------------------------------------------------------+
* | All changes should be made in the repository, |
* | otherwise they will be lost. |
* +------------------------------------------------------------+
* | Changes to this page may affect many users. |
* | Please discuss changes by opening an issue before editing. |
* +------------------------------------------------------------+
*/
/* <nowiki> */
 
(() => {
// 从URL中获取用户输入的搜索关键词
var searchTerm = mw.util.getParamValue('search');
 
"use strict";
// 检查条件:
// 1. 确实存在搜索关键词 (searchTerm !== null)
// 2. 并且不是全文搜索 (fulltext === null),以避免干扰高级搜索功能
if (searchTerm !== null && mw.util.getParamValue('fulltext') === null) {
// 查找搜索关键词中是否包含中文冒号“:”
var colonIdx = searchTerm.indexOf(':');
 
// dist/FullwidthSearchFix/FullwidthSearchFix.js
// 如果找到了中文冒号
//! src/FullwidthSearchFix/modules/redirect.ts
if (colonIdx != -1) {
var import_ext_gadget2 = require("ext.gadget.Util");
//! src/FullwidthSearchFix/modules/i18n.ts
// 将关键词按中文冒号分割成两部分
var import_ext_gadget = require("ext.gadget.i18n");
// 第一部分可能是一个命名空间(如“文件”、“用户”)
var getI18nMessages = () => {
var ns = searchTerm.substring(0, colonIdx);
return {
// 第二部分是具体的页面名称
"Redirecting to": (0, import_ext_gadget.localize)({
var page = searchTerm.substring(colonIdx + 1);
en: "Redirecting to $1",
ja: "「$1」にリダイレクト中",
"zh-hans": "正在重定向至“$1”",
"zh-hant": "正在重新導向至“$1”"
})
};
};
var i18nMessages = getI18nMessages();
var getMessage = (key) => {
return i18nMessages[key] || key;
};
//! src/FullwidthSearchFix/modules/redirect.ts
var import_ext_gadget3 = require("ext.gadget.Toastify");
var redirect = (nameSpace, pageName) => {
(0, import_ext_gadget3.toastify)({
text: getMessage("Redirecting to").replace("$1", "".concat(nameSpace, ":").concat(pageName)),
duration: -1
}, "info");
const {
wgScript
} = mw.config.get();
const href = new import_ext_gadget2.MwUri(wgScript, location.origin).extend({
search: "".concat(nameSpace, ":").concat(pageName)
}).toString();
location.href = href;
};
//! src/FullwidthSearchFix/FullwidthSearchFix.ts
(function fullwidthSearchFix() {
const URL_FULLTEXT = mw.util.getParamValue("fulltext");
const URL_SEARCH = mw.util.getParamValue("search");
if (URL_FULLTEXT || !URL_SEARCH) {
return;
}
const colonIndex = URL_SEARCH.indexOf(":");
if (colonIndex === -1) {
return;
}
const nameSpace = URL_SEARCH.slice(0, Math.max(0, colonIndex));
const {
wgNamespaceIds
} = mw.config.get();
if (!wgNamespaceIds[nameSpace.toLowerCase()]) {
return;
}
const pageName = URL_SEARCH.slice(Math.max(0, colonIndex + 1));
redirect(nameSpace, pageName);
})();
 
})();
// 验证第一部分是否为一个有效的命名空间
// mw.config.get('wgNamespaceIds') 包含了所有命名空间的ID映射
// 我们将其转换为小写进行不区分大小写的匹配
if (mw.config.get('wgNamespaceIds')[ns.toLowerCase()]) {
// 如果是有效的命名空间,则构造一个修正后的URL
// 使用英文冒号“:”重新组合命名空间和页面名称
var correctedSearchTerm = ns + ':' + page;
var url = mw.config.get('wgScript') + '?search=' + encodeURIComponent(correctedSearchTerm);
 
/* </nowiki> */
// 向用户显示一条通知,告知其将要跳转到修正后的搜索页面
mw.notify('检测到可能的中文冒号输入错误,正在跳转到修正后的搜索链接:' + mw.html.escape(url));
// 执行页面跳转
window.location.href = url;
}
}
}
})(jQuery, mediaWiki);