User:Shizhao/relist.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/* fork [[User:Xiplus/js/close-rfpp.js]], use in [[template:relist]] */
/*
存在的问题:
1. 直接重新提交一个批量提删(含有三级标题的二级标题)的存废讨论后,再提交一个其他存废讨论可能会出错,因为之前的提交改变了章节目录结构
*/
(function() {
if (typeof Relist == 'undefined')
Relist = {};
if (typeof Relist.summarySuffix == 'undefined') {
Relist.summarySuffix = ' via [[User:Shizhao/relist.js|relist.js]]';
}
if (!/^Wikipedia:頁面存廢討論\/記錄\/\d+\/\d+\/\d+$/.test(mw.config.get('wgPageName'))
|| mw.config.get('wgAction') !== 'view'
|| mw.config.get('wgRevisionId') !== mw.config.get('wgCurRevisionId')) {
return;
}
mw.util.addCSS(`
a.relist-link {
color: orange;
font-weight: bold;
}
a.relist-link-closed {
color: gray;
}
`);
var getPageContent = new Promise(function(resolve, reject) {
new mw.Api().get({
action: 'query',
prop: 'revisions',
rvprop: ['content', 'timestamp'],
titles: mw.config.get('wgPageName'),
formatversion: '2',
curtimestamp: true,
}).then(function(data) {
var page, revision;
if (!data.query || !data.query.pages) {
mw.notify('未能抓取頁面內容(unknown)',{ type: 'error' });
reject('unknown');
}
page = data.query.pages[0];
if (!page || page.invalid) {
mw.notify('未能抓取頁面內容(invalidtitle)',{ type: 'error' });
reject('invalidtitle');
}
if (page.missing) {
mw.notify('未能抓取頁面內容(nocreate-missing)',{ type: 'error' });
reject('nocreate-missing');
}
revision = page.revisions[0];
var content = revision.content;
var basetimestamp = revision.timestamp;
var curtimestamp = data.curtimestamp;
resolve({
content: content,
basetimestamp: basetimestamp,
curtimestamp: curtimestamp,
});
});
});
function showRelistButton() {
var titles = $('#mw-content-text').find('.mw-heading2 h2, .mw-heading3 h3');
titles.each(function(key, current) {
var title = $(current)[0].id;
var relistLink = document.createElement('a');
relistLink.href = '#';
relistLink.className = 'relist-link';
relistLink.innerText = '重提';
$(relistLink).on('click', function() {
processRelist(key+1, title);
return false;
});
var node = $(current)[0].nextSibling;
var delDivider = document.createElement('span');
delDivider.appendChild(document.createTextNode(' | '));
node.insertBefore(delDivider, node.childNodes[1]);
node.insertBefore(relistLink, node.childNodes[1]);
});
}
function processRelist(sectionid, title) {
mw.loader.using(['jquery.ui'], function() {
var html = '<div>';
html += '{{Relist}}理由(可选)<br>';
html += '<input type="text" class="comment" size="85"><br>';
html += '編輯摘要<br>';
html += '<input type="text" class="summary" size="85" value="重新提交">';
html += '</div>';
$(html).dialog({
title: '重新提交存废讨论 - ' + title,
minWidth: 590,
minHeight: 150,
buttons: [{
text: '確定',
click: function() {
processEdit(
sectionid,
title,
$(this).find('.comment').val(),
$(this).find('.summary').val()
);
$(this).dialog('close');
},
}, {
text: '取消',
click: function() {
$(this).dialog('close');
},
}],
});
});
}
function processEdit(sectionid, title, comment, summary) {
todayVfdpage = 'Wikipedia:頁面存廢討論/記錄/' + $.datepicker.formatDate('yy/mm/dd', new Date());
new mw.Api().get({
action: 'parse',
prop: 'wikitext',
page: mw.config.get('wgPageName'),
section: sectionid,
}).then(function(data){
//console.log(sectionid);
section = data.parse.wikitext['*'].trim();
//console.log(section);
if (/^===(\s*.+\s*)===\n/.test(section)){
newsection = section.replace(/^===(\s*.+\s*)===\n/,'==$1==\n');
} else {
newsection = section;
}
newsection = newsection.replace(/<section\s*.+=backlog\s*\/>/,'');
response = new mw.Api().get( {
action: 'query',
format: 'json',
prop: 'revisions',
titles: todayVfdpage,
}).then(function(response){
exists = typeof response.query.pages['-1'] === 'undefined';
if (exists){
new mw.Api().edit(todayVfdpage, function(revision) {
//var section = sectionContent(content,sectionid).trim();
newtext = revision.content + '\n' + newsection;
comment = '{{subst:' + 'Relist|' + comment + '|from='+ mw.config.get('wgPageName') +'#' + title + '}}';
comment = comment.trim();
newtext = newtext + '\n' + comment;
summary = '重新提交自[['+ mw.config.get('wgPageName') + ']]';
return {
text: newtext,
basetimestamp: revision.timestamp,
summary: '/* ' + title + ' */ ' + summary +Relist.summarySuffix,
};
});
}else{
summary = '重新提交自[['+ mw.config.get('wgPageName') + ']]';
newtext = '{{subst:'+ 'SafeAfdHead}}' + '\n' + newsection;
comment = '{{subst:' + 'Relist|' + comment + '|from='+ mw.config.get('wgPageName') +'#' + title + '}}';
comment = comment.trim();
newtext = newtext + '\n' + comment;
new mw.Api().create(
todayVfdpage,
{summary: '/* ' + title + ' */ ' + summary +Relist.summarySuffix},
newtext
);
}
});
}).then(function() {
new mw.Api().edit(mw.config.get('wgPageName'), function(revision) {
//var newtext = sectionContent(revision.content,sectionid).trim();
//获取章节
//console.log(sectionid);
//console.log(section);
newtext = section.split('\n',1)[0];
relistSummary = '已重新提交到[[' + todayVfdpage + '#' + title + ']]';
newcontent = '{{delh|relist}}\n' + '{{Relisted}}在[[' + todayVfdpage + '#' + title + ']]\n'+ '{{delf}}\n';
newtext = newtext + '\n' + newcontent;
$($('#mw-content-text').find('.mw-heading2, .mw-heading3')[sectionid-1]).find('.relist-link').addClass('relist-link-closed');
return {
text: newtext,
section: sectionid,
basetimestamp: revision.timestamp,
summary: '/* ' + title + ' */ ' + relistSummary +Relist.summarySuffix,
};
});
}).then(function() {
mw.notify('已重新提交 ' + title +'到[['+todayVfdpage+']]',{ type: 'success',autoHideSeconds: 3 });
}, function(e) {
if (e == 'editconflict') {
mw.notify('修改 ' + title + ' 時發生編輯衝突',{ type: 'error' });
} else {
mw.notify('修改 ' + title + ' 時發生未知錯誤:' + e,{ type: 'error' });
}
});
}
getPageContent.then(function(result) {
window.content = result.content;
var lenintext = result.content.split(/^==/gm).length - 1;
var leninhtml = mw.util.$content.find( '.mw-heading:has(> h2), .mw-heading:has(> h3)' ).length;
if (leninhtml !== lenintext) {
mw.notify('抓取章節錯誤,在HTML找到 ' + leninhtml + ' 個章節,在原始碼找到 ' + lenintext + ' 個章節',{ type: 'error' });
} else {
showRelistButton();
}
});
}
)();