User:DreamRimmer/SectionRemover.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:DreamRimmer/SectionRemover. |
// <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': '20px',
'display': 'none',
'height': '20px',
'margin-left': '20px'
})
.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 section(s) using [[User:DreamRimmer/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>