如何知道 MediWiki "Personal script" 正在编辑哪个部分(及其父部分)?
How to know which section (and its parents) is being edited from a MediWiki "Personal script"?
来自 MediaWiki 站点上的自定义用户脚本 运行,例如 User:Username/common.js
或 User:Username/vector.js
我希望在用户编辑部分时添加一些功能。
但是为了获得正确的上下文,我需要知道他们正在按名称编辑哪个部分以及 section/subsection 层次结构中的父部分。
我知道我可以解析 URL 以获得节号,但我找不到从该编号到节名或其在节结构中的位置的直接方法。我知道有很多变量可以通过 JavaScript 以各种方式访问,名称以 wg
开头,但到目前为止我找不到任何相关的变量。
明显的技巧是通过 AJAX 加载相关页面并解析目录(如果有)或页面布局。这看起来很昂贵,如果有部分使用 <h3>X</h3>
标签而不是 ===X===
风格的维基文本,甚至可能不会那么容易。
也许在当前的 MediWiki JavaScript 界面中有些东西我没能找到?
(用例是英语维基词典,与维基百科不同,相同的部分名称可以出现多次,例如 "Noun" 在 "English" 和 "Italian" 下,等等)
您可以使用 Parsing API 获取当前页面的部分。 prop=sections
就是您想要的。然后就是循环遍历结果并得到你想要的东西了。
table 页面的示例输出:
{
"parse": {
"title": "table",
"sections": [
{
"toclevel": 1,
"level": "2",
"line": "English",
"number": "1",
"index": "1",
"fromtitle": "table",
"byteoffset": 21,
"anchor": "English"
},
{
"toclevel": 2,
"level": "3",
"line": "Etymology",
"number": "1.1",
"index": "2",
"fromtitle": "table",
"byteoffset": 223,
"anchor": "Etymology"
},
// ...
]
}
}
下面应该给你一个包含相关部分的数组(相反:当前部分是 sections[0]
,父部分是 sections[1]
等等)
$(document).ready(function(){
// Whitelist allowable Namespaces here
if( mw.config.get('wgNamespaceNumber') !== 0 ) return;
if( ['edit','submit'].indexOf(mw.config.get('wgAction')) > -1 ) {
var sectionId = $("#editform input[name=wpSection]").val();
if( !sectionId || isNaN(sectionId) || sectionId == "0" ) return;
mw.loader.using( 'mediawiki.api', function () {
( new mw.Api() ).get( {
action: 'parse',
page: mw.config.get('wgPageName'),
prop: 'sections',
} ).done( function ( data ) {
var sections = [];
// Might want to check for errors here first
var allSections = data.parse.sections;
for( var i = 0, l = allSections.length; i < l; ++i) {
var thisSection = allSections[i];
if( thisSection.index == sectionId ) {
sections.push(thisSection);
// Loop back for the parents
var toclevel = thisSection.toclevel;
while( toclevel > 1 && i > 0 ) {
--i;
var possibleParent = allSections[i];
if( possibleParent.toclevel < toclevel ) {
sections.push(possibleParent);
toclevel = possibleParent.toclevel;
}
}
break;
}
}
// Use sections here
console.log(sections);
} );
} );
}
});
未在 Wiktionary 本身上测试,但适用于 1.24 本地 wiki。
编辑:正如评论中提到的,如果页面有带部分的模板,索引将不匹配,因此最好手动找到具有正确索引的部分。
来自 MediaWiki 站点上的自定义用户脚本 运行,例如 User:Username/common.js
或 User:Username/vector.js
我希望在用户编辑部分时添加一些功能。
但是为了获得正确的上下文,我需要知道他们正在按名称编辑哪个部分以及 section/subsection 层次结构中的父部分。
我知道我可以解析 URL 以获得节号,但我找不到从该编号到节名或其在节结构中的位置的直接方法。我知道有很多变量可以通过 JavaScript 以各种方式访问,名称以 wg
开头,但到目前为止我找不到任何相关的变量。
明显的技巧是通过 AJAX 加载相关页面并解析目录(如果有)或页面布局。这看起来很昂贵,如果有部分使用 <h3>X</h3>
标签而不是 ===X===
风格的维基文本,甚至可能不会那么容易。
也许在当前的 MediWiki JavaScript 界面中有些东西我没能找到?
(用例是英语维基词典,与维基百科不同,相同的部分名称可以出现多次,例如 "Noun" 在 "English" 和 "Italian" 下,等等)
您可以使用 Parsing API 获取当前页面的部分。 prop=sections
就是您想要的。然后就是循环遍历结果并得到你想要的东西了。
table 页面的示例输出:
{
"parse": {
"title": "table",
"sections": [
{
"toclevel": 1,
"level": "2",
"line": "English",
"number": "1",
"index": "1",
"fromtitle": "table",
"byteoffset": 21,
"anchor": "English"
},
{
"toclevel": 2,
"level": "3",
"line": "Etymology",
"number": "1.1",
"index": "2",
"fromtitle": "table",
"byteoffset": 223,
"anchor": "Etymology"
},
// ...
]
}
}
下面应该给你一个包含相关部分的数组(相反:当前部分是 sections[0]
,父部分是 sections[1]
等等)
$(document).ready(function(){
// Whitelist allowable Namespaces here
if( mw.config.get('wgNamespaceNumber') !== 0 ) return;
if( ['edit','submit'].indexOf(mw.config.get('wgAction')) > -1 ) {
var sectionId = $("#editform input[name=wpSection]").val();
if( !sectionId || isNaN(sectionId) || sectionId == "0" ) return;
mw.loader.using( 'mediawiki.api', function () {
( new mw.Api() ).get( {
action: 'parse',
page: mw.config.get('wgPageName'),
prop: 'sections',
} ).done( function ( data ) {
var sections = [];
// Might want to check for errors here first
var allSections = data.parse.sections;
for( var i = 0, l = allSections.length; i < l; ++i) {
var thisSection = allSections[i];
if( thisSection.index == sectionId ) {
sections.push(thisSection);
// Loop back for the parents
var toclevel = thisSection.toclevel;
while( toclevel > 1 && i > 0 ) {
--i;
var possibleParent = allSections[i];
if( possibleParent.toclevel < toclevel ) {
sections.push(possibleParent);
toclevel = possibleParent.toclevel;
}
}
break;
}
}
// Use sections here
console.log(sections);
} );
} );
}
});
未在 Wiktionary 本身上测试,但适用于 1.24 本地 wiki。
编辑:正如评论中提到的,如果页面有带部分的模板,索引将不匹配,因此最好手动找到具有正确索引的部分。