MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
$(function() { | $(function() { | ||
mw.loader.using( 'mediawiki.util' ).then( function () { | mw.loader.using( 'mediawiki.util' ).then( function () { | ||
console.log( '[ReportAbuseLink] | console.log( '[ReportAbuseLink] Initializing link insertion...' ); | ||
// | const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed | ||
const | const linkText = 'Report Abuse'; | ||
// | let attempts = 0; | ||
const | const maxAttempts = 50; // Try for 5 seconds (50 * 100ms) | ||
const interval = 100; // Check every 100ms | |||
const tryInsertLink = function() { | |||
attempts++; | |||
console.log( '[ReportAbuseLink] Attempt #' + attempts ); | |||
const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' ); | |||
const loginItem = document.querySelector( '#pt-login-2' ); | |||
if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) { | |||
// Both list and login item found in correct place! | |||
console.log( '[ReportAbuseLink] Found list and #pt-login-2. Inserting link.' ); | |||
const reportPageTitle = mw.Title.newFromText( reportPageName ); // Use mw.Title if available | |||
if (!reportPageTitle) return true; // Stop if title invalid | |||
const li = document.createElement( 'li' ); | |||
li.id = 'pt-reportabuse'; | |||
const link = document.createElement( 'a' ); | |||
link.href = reportPageTitle.getUrl(); // Use mw object method | |||
link.textContent = linkText; | |||
li.appendChild( link ); | |||
personalToolsList.insertBefore( li, loginItem ); | |||
return true; // Indicate success | |||
} else if ( attempts >= maxAttempts ) { | |||
// Max attempts reached, fallback to appending if list found | |||
console.log( '[ReportAbuseLink] Max attempts reached. #pt-login-2 not found correctly. Appending to end if list exists.' ); | |||
if (personalToolsList) { | |||
const reportPageTitle = mw.Title.newFromText( reportPageName ); | |||
if (!reportPageTitle) return true; | |||
// | const li = document.createElement( 'li' ); | ||
li.id = 'pt-reportabuse'; | |||
} | const link = document.createElement( 'a' ); | ||
link.href = reportPageTitle.getUrl(); | |||
link.textContent = linkText; | |||
li.appendChild( link ); | |||
personalToolsList.appendChild(li); | |||
} else { | |||
console.log( '[ReportAbuseLink] Personal tools list not found even on fallback.' ); | |||
} | |||
return true; // Indicate fallback finished | |||
} | |||
return false; // Indicate insertion not yet successful | |||
}; | |||
// Start the interval timer | |||
const checkInterval = setInterval( function() { | |||
if ( tryInsertLink() ) { | |||
clearInterval( checkInterval ); // Stop checking once inserted or max attempts reached | |||
console.log( '[ReportAbuseLink] Link insertion process finished.' ); | |||
} | |||
}, interval ); | |||
}); | }); | ||
}); | }); |
Revision as of 04:47, 21 April 2025
$(function() { mw.loader.using( 'mediawiki.util' ).then( function () { console.log( '[ReportAbuseLink] Initializing link insertion...' ); const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed const linkText = 'Report Abuse'; let attempts = 0; const maxAttempts = 50; // Try for 5 seconds (50 * 100ms) const interval = 100; // Check every 100ms const tryInsertLink = function() { attempts++; console.log( '[ReportAbuseLink] Attempt #' + attempts ); const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' ); const loginItem = document.querySelector( '#pt-login-2' ); if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) { // Both list and login item found in correct place! console.log( '[ReportAbuseLink] Found list and #pt-login-2. Inserting link.' ); const reportPageTitle = mw.Title.newFromText( reportPageName ); // Use mw.Title if available if (!reportPageTitle) return true; // Stop if title invalid const li = document.createElement( 'li' ); li.id = 'pt-reportabuse'; const link = document.createElement( 'a' ); link.href = reportPageTitle.getUrl(); // Use mw object method link.textContent = linkText; li.appendChild( link ); personalToolsList.insertBefore( li, loginItem ); return true; // Indicate success } else if ( attempts >= maxAttempts ) { // Max attempts reached, fallback to appending if list found console.log( '[ReportAbuseLink] Max attempts reached. #pt-login-2 not found correctly. Appending to end if list exists.' ); if (personalToolsList) { const reportPageTitle = mw.Title.newFromText( reportPageName ); if (!reportPageTitle) return true; const li = document.createElement( 'li' ); li.id = 'pt-reportabuse'; const link = document.createElement( 'a' ); link.href = reportPageTitle.getUrl(); link.textContent = linkText; li.appendChild( link ); personalToolsList.appendChild(li); } else { console.log( '[ReportAbuseLink] Personal tools list not found even on fallback.' ); } return true; // Indicate fallback finished } return false; // Indicate insertion not yet successful }; // Start the interval timer const checkInterval = setInterval( function() { if ( tryInsertLink() ) { clearInterval( checkInterval ); // Stop checking once inserted or max attempts reached console.log( '[ReportAbuseLink] Link insertion process finished.' ); } }, interval ); }); });