【发布时间】:2016-06-23 20:06:59
【问题描述】:
四处挖掘,通过 mediawiki API 使用 AJAX 添加类别似乎很简单:
var api = new mw.Api();
function addCat( category ) {
api.postWithToken( "edit", {
format: 'json',
action: "edit",
title: mw.config.get( "wgPageName" ),
appendtext: category
} ).done( function( result, jqXHR ) {
mw.log( "Saved successfully" );
location.reload();
} ).fail( function( code, result ) {
if ( code === "http" ) {
mw.log( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
} else if ( code === "ok-but-empty" ) {
mw.log( "Got an empty response from the server" );
} else {
mw.log( "API error: " + code );
}
} );
}
addCat('[[Category:TEST]]');
但我没有看到任何使用 API 删除文本(即上面添加的类别字符串)的好方法。我能看到的唯一方法是拉出整个页面的标记,去掉类别文本,然后通过 API 将剩余的整个文本发回。有没有更干净的方法不会破坏页面?
【问题讨论】:
标签: javascript ajax mediawiki