User:GrabUp/External Links Remover.js
//
(function() {
function removeExternalLinks() {
const validNamespaces = [0, 100, 2, 118];
if (!validNamespaces.includes(mw.config.get('wgNamespaceNumber'))) {
alert("This tool can only be used on article, sandbox, user, or draft pages.");
return;
}
const confirmationContainer = document.createElement('div');
confirmationContainer.innerHTML = `
Are you sure you want to remove external links from this article?
`;
document.body.appendChild(confirmationContainer);
const confirmYes = document.getElementById('confirm-yes');
const confirmNo = document.getElementById('confirm-no');
if (confirmYes) {
confirmYes.onclick = function() {
document.body.removeChild(confirmationContainer);
promptForComment();
};
}
if (confirmNo) {
confirmNo.onclick = function() {
document.body.removeChild(confirmationContainer);
};
}
}
function promptForComment() {
const commentContainer = document.createElement('div');
commentContainer.innerHTML = `
Would you like to add additional edit summaries? If yes, please enter below:
`;
document.body.appendChild(commentContainer);
document.getElementById('submit-comment').onclick = function() {
const additionalSummaries = document.getElementById('edit-summary').value.trim();
let editSummary = 'Removed external links, Using External Links Remover';
if (additionalSummaries) {
editSummary += ' | ' + additionalSummaries.split('|').map(summary => summary.trim()).join(' | ');
}
processEdit(editSummary);
document.body.removeChild(commentContainer);
};
document.getElementById('skip-comment').onclick = function() {
const editSummary = 'Removed external links, Using External Links Remover';
processEdit(editSummary);
document.body.removeChild(commentContainer);
};
document.getElementById('cancel').onclick = function() {
document.body.removeChild(commentContainer);
};
}
function processEdit(editSummary) {
const api = new mw.Api();
api.get({
action: 'query',
prop: 'revisions',
rvprop: 'content',
titles: mw.config.get('wgPageName'),
formatversion: 2
}).done(function(data) {
const page = data.query.pages[0];
if (!page || !page.revisions) {
alert("Failed to fetch page content.");
return;
}
let text = page.revisions[0].content;
const externalLinkPattern = /(?
const refPattern = /]*>[\s\S]*?<\/ref>/g;
const citeWebPattern = /{{\s*(Cite\s+web|Cite\s+news|Cite\s+book|Cite\s+journal).*?}}/g;
const urlTemplatePattern = /{{\s*(URL|url)\s*\|\s*[^\}]+\s*}}/g;
const officialWebsitePattern = /{{\s*official website\s*\|\s*[^\}]+\s*}}/g;
const sectionHeadingPattern = /(==\s*(References|External links)\s*==\n?)/gi;
const refPlaceholders = [];
const citePlaceholders = [];
const urlPlaceholders = [];
const officialWebsitePlaceholders = [];
text = text.replace(refPattern, match => {
refPlaceholders.push(match);
return `REF_PLACEHOLDER_${refPlaceholders.length - 1}`;
});
text = text.replace(citeWebPattern, match => {
citePlaceholders.push(match);
return `CITE_PLACEHOLDER_${citePlaceholders.length - 1}`;
});
text = text.replace(urlTemplatePattern, match => {
urlPlaceholders.push(match);
return `URL_PLACEHOLDER_${urlPlaceholders.length - 1}`;
});
text = text.replace(officialWebsitePattern, match => {
officialWebsitePlaceholders.push(match);
return `OFFICIAL_WEBSITE_PLACEHOLDER_${officialWebsitePlaceholders.length - 1}`;
});
let linkRemoved = false;
const externalLinksSectionIndex = text.search(/==\s*External links\s*==/i);
if (externalLinksSectionIndex !== -1) {
let beforeExternalLinks = text.slice(0, externalLinksSectionIndex);
const afterExternalLinks = text.slice(externalLinksSectionIndex);
beforeExternalLinks = beforeExternalLinks.replace(externalLinkPattern, (match, p1, p2) => {
linkRemoved = true;
return `${p2 ? p2.trim() : ''}`;
});
text = beforeExternalLinks + afterExternalLinks;
} else {
text = text.replace(externalLinkPattern, (match, p1, p2) => {
linkRemoved = true;
return `${p2 ? p2.trim() : ''}`;
});
}
refPlaceholders.forEach((ref, index) => {
text = text.replace(`REF_PLACEHOLDER_${index}`, ref);
});
citePlaceholders.forEach((cite, index) => {
text = text.replace(`CITE_PLACEHOLDER_${index}`, cite);
});
urlPlaceholders.forEach((url, index) => {
text = text.replace(`URL_PLACEHOLDER_${index}`, url);
});
officialWebsitePlaceholders.forEach((website, index) => {
text = text.replace(`OFFICIAL_WEBSITE_PLACEHOLDER_${index}`, website);
});
text = text.replace(sectionHeadingPattern, (match, heading) => heading.trim() + '\n');
if (linkRemoved) {
showPreview(text.trim(), editSummary);
} else {
const noLinksContainer = document.createElement('div');
noLinksContainer.innerHTML = `
No external links found to remove
`;
document.body.appendChild(noLinksContainer);
document.getElementById('close-no-links').onclick = function() {
document.body.removeChild(noLinksContainer);
};
}
});
}
function showPreview(previewContent, editSummary) {
// Create a preview container with a black background and high-contrast text.
const previewContainer = document.createElement('div');
previewContainer.innerHTML = `
Preview Changes
${previewContent.replace(/background-color: yellow;/g, 'background-color: blue;')}
`;
document.body.appendChild(previewContainer);
// Confirm and cancel buttons functionality
document.getElementById('confirm-save').onclick = function() {
const finalContent = previewContent.replace(/(.*?)<\/span>/g, '$1');
const api = new mw.Api();
api.postWithEditToken({
action: 'edit',
title: mw.config.get('wgPageName'),
text: finalContent, // Save without any highlighting
summary: editSummary
}).done(function(data) {
const newRevId = data.edit.newrevid;
window.location.href = mw.util.getUrl(mw.config.get('wgPageName'), { diff: newRevId });
}).fail(function() {
alert("Failed to save changes.");
});
document.body.removeChild(previewContainer);
};
document.getElementById('cancel-preview').onclick = function() {
document.body.removeChild(previewContainer);
};
}
mw.loader.using('mediawiki.api', function() {
var link = mw.util.addPortletLink(
'p-cactions',
'#',
'Remove External Links',
'ca-remove-external-links',
'Removes all external links from the article'
);
if ( !link ) {
return;
}
link.onclick = removeExternalLinks;
});
})();
//