Created
March 28, 2025 13:46
-
-
Save ternera/af8efd121564a58bbf6e035e2a231ef2 to your computer and use it in GitHub Desktop.
Script for Wikimedia projects to quickly access the delete page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add delete button to pages where the user has delete permissions | |
(function() { | |
'use strict'; | |
// Check if user has delete permissions | |
function hasDeletePermission() { | |
// Look for delete tab or link in the page | |
const deleteLinks = document.querySelectorAll('a[href*="action=delete"], li#ca-delete'); | |
return deleteLinks.length > 0; | |
} | |
// Main function to add the delete button | |
function addDeleteButton() { | |
if (!hasDeletePermission()) { | |
return; // Exit if user doesn't have delete permissions | |
} | |
// Get current URL | |
const currentUrl = window.location.href; | |
// Create delete button | |
const deleteButton = document.createElement('div'); | |
deleteButton.style.position = 'fixed'; | |
deleteButton.style.bottom = '20px'; | |
deleteButton.style.right = '20px'; | |
deleteButton.style.backgroundColor = '#c2252c'; | |
deleteButton.style.color = 'white'; | |
deleteButton.style.padding = '10px 16px'; // Increased padding | |
deleteButton.style.borderRadius = '4px'; | |
deleteButton.style.cursor = 'pointer'; | |
deleteButton.style.zIndex = '9999'; | |
deleteButton.style.fontSize = '16px'; // Increased font size | |
deleteButton.style.boxShadow = '0 2px 5px rgba(0,0,0,0.2)'; | |
deleteButton.style.opacity = '0.8'; | |
deleteButton.innerHTML = '🗑️ Delete'; | |
deleteButton.title = 'Quick delete this page'; | |
// Add hover effect | |
deleteButton.addEventListener('mouseover', function() { | |
this.style.opacity = '1'; | |
}); | |
deleteButton.addEventListener('mouseout', function() { | |
this.style.opacity = '0.8'; | |
}); | |
// Add click event to navigate to delete action | |
deleteButton.addEventListener('click', function() { | |
// Construct the delete URL | |
let deleteUrl = currentUrl; | |
// If the URL already has parameters, add the action=delete parameter | |
if (deleteUrl.includes('?')) { | |
if (!deleteUrl.includes('action=')) { | |
deleteUrl += '&action=delete'; | |
} | |
} else { | |
// If there are no parameters, add the action=delete parameter | |
deleteUrl += '?action=delete'; | |
} | |
// Navigate to the delete URL | |
window.location.href = deleteUrl; | |
}); | |
// Add button to the page | |
document.body.appendChild(deleteButton); | |
} | |
// Wait for page to fully load using MediaWiki's hook system | |
mw.hook('wikipage.content').add(function() { | |
// Short delay to ensure permissions are loaded | |
setTimeout(addDeleteButton, 500); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment