User:YFdyh000/category item description.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
* 本JS在分类页面上工作,
* 将该分类下的条目加上一句话的描述,该描述取自条目正文第一段第一句。
* 描述长度已限制为32个字,外用small标签包裹(字体会显得小一点)。
* TODO: 更新以上描述和整理代码。
* mw.loader.load('/w/index.php?title=User:YFdyh000/category_item_description.js&action=raw&ctype=text/javascript');
*/
(function () {
//console.log(isCategory);
if (mw.config.get('wgNamespaceNumber') != 14) { //14是分类。https://www.mediawiki.org/wiki/Manual:Namespace
//console.log("停止运行");
document.execCommand("stop");
return 'This is not an error. This is just to abort javascript';
}
$(".mw-category-group ul li a").each(function () {
var $node = $(this);
var pageName = $node.attr("href");
pageName = pageName.substring(pageName.indexOf("/wiki/") + 6, pageName.length);
//console.log(pageName);
getArticleDescription(pageName, $node);
});
}() );
function getArticleDescription(pageName, $node) {
$.ajax({
url: mw.config.get('wgScriptPath') + "/api.php?action=query&prop=extracts&exintro&format=json&converttitles&titles=" + pageName + "&variant="+mw.config.get('wgUserVariant'),
//async: false,
success: function (data) {
const showNameOnly = true; // 仅显示转换后的条目名
const hideNoDiff = true; // 与条目名无差异则不显示
//console.log(pageName);
//if (data.query.converted) {
// console.log(data.query.converted);
//}
//console.log(data);
for (key in data.query.pages) {
//console.log(key);
data = data.query.pages[key];
break;
}
//console.log(data);
//console.log(data.title);
pageName = data.title;//输入的pageName可能是简体,但实际上页面名可能是繁体,需要url上converttitles来转换。
var content = data.extract;
//console.log(content);
//取第一段...
var i = content.indexOf("<p>"); //正常段落的p标签是没有 class="xxx"之类的属性的。
var j = content.indexOf("</p>", i);
if (i == -1 || j == -1) {
var e = "条目" + pageName + "的正文里没有段落";
return;
console.log(e + ":" + content);
throw new Error(e);
}
//if (i > j)
// throw new Error("条目" + pageName + "的正文p标签不正常");
var firstParagraph = "<span>" + content.substring(i + 3, j) + "</span>";
//console.log(firstParagraph);
var $firstParagraph = $($.parseHTML(firstParagraph));
var r = removeName($firstParagraph, showNameOnly);
if (showNameOnly) {
var description = r ? r.text().trim() : "";
} else {
//...的第一句。
var description = $firstParagraph.text();
i = description.indexOf("。");
//console.log(i);
if (i > -1) {
description = description.substring(0, i);
//console.log(description);
}
//去除括号。
description = description.replace(/[\((].*?[\))]/g, '');
if (description[0] == ",")
description = description.substring(1, description.length);
description = limitDescriptionLength(description, 32);
}
if (!(description.length == 0
|| (hideNoDiff && pageName == description) )) {
$node.parent().append(":");
$node.parent().append($("<small>" + description + "</small>"));
}
}
});
return;
$.getJSON("https://wikicn.playgoteam.workers.dev/w/api.php?action=parse&page=" + pageName + "&format=json§ion=0&prop=text", function (data) {
console.log(pageName);
var content = data.parse.text["*"];
//取第一段...
var i = content.indexOf("<p>"); //正常段落的p标签是没有 class="xxx"之类的属性的。
var j = content.indexOf("</p>", i);
if (i == -1 || j == -1) {
var e = "条目" + pageName + "的正文里没有段落";
console.log(e + ":" + content);
throw new Error(e);
}
//if (i > j)
// throw new Error("条目" + pageName + "的正文p标签不正常");
var firstParagraph = "<span>" + content.substring(i + 3, j) + "</span>";
console.log(firstParagraph);
var $firstParagraph = $($.parseHTML(firstParagraph));
removeName($firstParagraph);
//...的第一句。
var description = $firstParagraph.text();
i = description.indexOf("。");
//console.log(i);
if (i > -1) {
description = description.substring(0, i);
//console.log(description);
}
//去除括号。
description = description.replace(/[\((].*?[\))]/g, '');
if (description[0] == ",")
description = description.substring(1, description.length);
description = limitDescriptionLength(description, 32);
if (description.length > 0) {
$node.parent().append(":");
$node.parent().append($("<small>" + description + "</small>"));
}
//alert(data.length);
});
//document.execCommand("stop");
}
function removeName($paragraph, reverse) {
//console.log($paragraph);
var node = $paragraph.contents()[0];
//console.log(node.nodeType);
if (node.nodeType == 1) //ELEMENT_NODE
{
var $pageName = $(node);
if ($pageName.prop("tagName") == "B" && !reverse)
$pageName.detach();
else if ($pageName.prop("tagName") == "B" && reverse)
return $pageName
//$pageName.detach();
}
else if (node.nodeType == 3) {
//console.log(node.data);
if (node.data == "《" && !reverse) {
$(node).detach();
removeName($paragraph, reverse);
node = $paragraph.contents()[0];
if (node.nodeType == 3 && node.data[0] == '》') {
node.data = node.data.substring(1, node.data.length);
//console.log(node);
}
}
//TODO
}
}
function limitDescriptionLength(description, length) {
if (description.length > length)
return description.substring(0, length) + "…";
else
return description;
}
//[[分类:维基脚本]] 复制後请删除此列避免分类