MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
/ | // Wait for the page elements to be ready | ||
$(function () { | $(function() { | ||
// Use mw.loader.using to ensure necessary MediaWiki JS utilities are loaded | |||
console.log(' | mw.loader.using( 'mediawiki.util' ).then( function () { | ||
console.log( '[ReportAbuseLink] Attempting to add link...' ); // Log message for console | |||
// Use the selector for the list containing personal tools based on inspection | |||
const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' ); | |||
// Use the correct ID for the login list item based on inspection | |||
const | const loginItem = document.querySelector( '#pt-login-2' ); | ||
if (!personalToolsList) { | // Check if we found the list where links should go | ||
console.log(' | if ( !personalToolsList ) { | ||
return | console.log( '[ReportAbuseLink] Could not find personal tools list (#p-personal ul.vector-menu-content-list).' ); | ||
return; // Exit if list element is not found | |||
} | } | ||
console.log( '[ReportAbuseLink] Found personal tools list.' ); | |||
// --- Configuration --- | |||
const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if your wiki page has a different name | |||
const linkText = 'Report Abuse'; // Text displayed for the link | |||
// --- End Configuration --- | |||
// Create the new list item | // Create the new list item (<li>) | ||
const li = document.createElement('li'); | const li = document.createElement( 'li' ); | ||
li.id = 'pt-reportabuse'; | li.id = 'pt-reportabuse'; // Set an ID for the new list item | ||
const link = document.createElement('a'); | // Create the link element (<a>) | ||
link.href = | const link = document.createElement( 'a' ); | ||
link.textContent = | link.href = mw.util.getUrl( reportPageName ); // Generates correct URL for the wiki page | ||
link.title = 'Report animal abuse'; | link.textContent = linkText; | ||
// link.title = 'Report animal abuse'; // Optional: Add a tooltip title | |||
li.appendChild(link); | // Add the link inside the list item | ||
li.appendChild( link ); | |||
// Try to insert before Log in | // Try to insert the new list item before the 'Log in' list item | ||
if ( loginItem && loginItem.parentNode === personalToolsList ) { | |||
if (loginItem && loginItem.parentNode === personalToolsList) { | console.log( '[ReportAbuseLink] Found login item (#pt-login-2), inserting link before it.' ); | ||
personalToolsList.insertBefore(li, loginItem); | personalToolsList.insertBefore( li, loginItem ); | ||
} else { | } else { | ||
personalToolsList.appendChild(li); | // Fallback if login item isn't found (e.g., user is logged in) or has unexpected parent | ||
// Try appending to the end of the list as a fallback | |||
console.log( '[ReportAbuseLink] Login item (#pt-login-2) not found or has wrong parent. Appending link to end.' ); | |||
personalToolsList.appendChild( li ); | |||
// Note: Inserting before 'Preferences' or after 'ULS' dynamically when logged in | |||
// requires finding those elements reliably too. Appending is a simpler fallback. | |||
} | } | ||
}); | |||
} | |||
}); | }); |
Revision as of 04:37, 21 April 2025
// Wait for the page elements to be ready $(function() { // Use mw.loader.using to ensure necessary MediaWiki JS utilities are loaded mw.loader.using( 'mediawiki.util' ).then( function () { console.log( '[ReportAbuseLink] Attempting to add link...' ); // Log message for console // Use the selector for the list containing personal tools based on inspection const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' ); // Use the correct ID for the login list item based on inspection const loginItem = document.querySelector( '#pt-login-2' ); // Check if we found the list where links should go if ( !personalToolsList ) { console.log( '[ReportAbuseLink] Could not find personal tools list (#p-personal ul.vector-menu-content-list).' ); return; // Exit if list element is not found } console.log( '[ReportAbuseLink] Found personal tools list.' ); // --- Configuration --- const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if your wiki page has a different name const linkText = 'Report Abuse'; // Text displayed for the link // --- End Configuration --- // Create the new list item (<li>) const li = document.createElement( 'li' ); li.id = 'pt-reportabuse'; // Set an ID for the new list item // Create the link element (<a>) const link = document.createElement( 'a' ); link.href = mw.util.getUrl( reportPageName ); // Generates correct URL for the wiki page link.textContent = linkText; // link.title = 'Report animal abuse'; // Optional: Add a tooltip title // Add the link inside the list item li.appendChild( link ); // Try to insert the new list item before the 'Log in' list item if ( loginItem && loginItem.parentNode === personalToolsList ) { console.log( '[ReportAbuseLink] Found login item (#pt-login-2), inserting link before it.' ); personalToolsList.insertBefore( li, loginItem ); } else { // Fallback if login item isn't found (e.g., user is logged in) or has unexpected parent // Try appending to the end of the list as a fallback console.log( '[ReportAbuseLink] Login item (#pt-login-2) not found or has wrong parent. Appending link to end.' ); personalToolsList.appendChild( li ); // Note: Inserting before 'Preferences' or after 'ULS' dynamically when logged in // requires finding those elements reliably too. Appending is a simpler fallback. } }); });