// <nowiki>
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {
$(document).ready(function() {
var config = mw.config.get([
'wgArticleId',
'wgNamespaceNumber',
'wgPageName',
'wgAction',
'wgIsMainPage'
]);
if (config.wgIsMainPage) {
return;
}
if (config.wgNamespaceNumber == -1) {
return;
}
function addRemoveSectionLinks() {
$('.mw-editsection a').not('.mw-editsection-visualeditor').each(function() {
var editSectionUrl = $(this).attr('href');
var sectionReg = /§ion=(\d+)/;
var sectionRaw = sectionReg.exec(editSectionUrl);
if (sectionRaw != null) {
var sectionNumber = parseInt(sectionRaw[1]);
var sectionTitle = $(this).closest('h2').text().trim();
var checkbox = $('<input>')
.attr('type', 'checkbox')
.attr('data-section', sectionNumber)
.attr('data-title', sectionTitle)
.css({
'margin-left': '10px',
'cursor': 'pointer',
'width': '17px',
'display': 'none',
'height': '17px'
})
.change(function() {
var headerElement = $(this).closest('h2');
if ($(this).is(':checked')) {
headerElement.css('background-color', 'yellow');
} else {
headerElement.css('background-color', '');
}
});
$(this).parent().append(checkbox);
}
});
var removeButton = $('<button>')
.text('Remove the selected sections')
.attr('id', 'remove-selected-sections')
.css({
'position': 'sticky',
'bottom': '7px',
'width': '100%',
'font-size': '150%',
'color': 'white',
'background-color': '#607593',
'border-radius': '5px',
'border-block-color': 'unset',
'border-block-start-color': 'unset',
'display': 'none'
})
.click(function(e) {
e.preventDefault();
var selectedSections = [];
$('input[type="checkbox"]:checked').each(function() {
selectedSections.push($(this).data('section'));
});
if (selectedSections.length === 0) {
alert('No sections are selected.');
return;
}
if (confirm('Are you sure you want to remove the selected sections?')) {
removeSections(selectedSections);
}
});
$(document.body).append(removeButton);
}
function removeSections(sectionNumbers) {
var pageid = config.wgArticleId;
var api = new mw.Api();
var sectionContents = [];
var initialRevId;
function getSectionContent(index) {
if (index >= sectionNumbers.length) {
api.get({
action: 'query',
prop: 'revisions',
pageids: pageid,
rvprop: 'content|ids',
indexpageids: 1
}).done(function(response) {
var pageContent = response.query.pages[pageid].revisions[0]['*'];
var initialRevId = response.query.pages[pageid].revisions[0].revid;
sectionContents.forEach(function(sectionContent) {
var sectionReg = new RegExp(sectionContent.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '\n*', 'g');
pageContent = pageContent.replace(sectionReg, '');
});
api.postWithToken('csrf', {
action: 'edit',
pageid: pageid,
text: pageContent,
summary: 'Removed sections (using [[User:DreamRimmer/SectionRemover|SectionRemover.js]])'
}).done(function(postResponse) {
var newRevisionId = postResponse.edit.newrevid;
alert('Selected sections removed successfully.');
location.href = mw.util.getUrl(config.wgPageName) + '?diff=' + newRevisionId;
}).fail(function() {
alert('Failed to remove the selected sections.');
});
});
return;
}
var sectionNumber = sectionNumbers[index];
api.get({
action: 'query',
prop: 'revisions',
pageids: pageid,
rvsection: sectionNumber,
rvprop: 'content',
indexpageids: 1
}).done(function(response) {
var sectionContent = response.query.pages[pageid].revisions[0]['*'];
sectionContents.push(sectionContent);
getSectionContent(index + 1);
});
}
getSectionContent(0);
}
if (config.wgAction === 'view' && !config.wgPageName.includes('History') && !config.wgPageName.includes('Contributions')) {
mw.util.addPortletLink(
'p-cactions',
'#',
'Remove sections',
'ca-enable-section-removal',
'Enable the ability to remove sections'
);
$('#ca-enable-section-removal').on('click', function (e) {
e.preventDefault();
$('input[type="checkbox"]').css('display', 'inline');
$('#remove-selected-sections').css('display', 'block');
});
addRemoveSectionLinks();
}
});
});
// </nowiki>