User:Ricordisamoa/CFD.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. |
![]() | Documentation for this user script can be added at User:Ricordisamoa/CFD. |
/*
* When clicking a link pointing to a CFD subpage from a deleted category,
* search the page for the corresponding section.
*
* @author [[User:Ricordisamoa]]
*/
$( function () {
if (
mw.config.get( 'wgNamespaceNumber' ) !== 4 ||
mw.config.get( 'wgTitle' ).slice( 0, 30 ) !== 'Categories for discussion/Log/' ||
!document.referrer
) {
return;
}
mw.loader.using( [ 'mediawiki.RegExp', 'mediawiki.Title', 'mediawiki.Uri' ] ).done( function () {
var getPageTitle = function ( href ) {
try {
href = new mw.Uri( href );
} catch ( e ) {
return;
}
if ( href.query.hasOwnProperty( 'title' ) ) {
return href.query.title;
} else {
var titleRegex = new RegExp( mw.RegExp.escape( mw.config.get( 'wgArticlePath' ) )
.replace( '\\$1', '(.+)' ) ),
matches = titleRegex.exec( href.path );
return matches ? decodeURIComponent( matches[1] ) : undefined;
}
};
var title = getPageTitle( document.referrer );
if ( !title ) {
return;
}
title = mw.Title.newFromUserInput( title );
if ( title && title.getNamespaceId && title.getNamespaceId() === 14 && title.getPrefixedText ) {
var expectedText = title.getPrefixedText(),
$anchors = mw.util.$content
.find( '.mw-headline' )
.filter( function () {
return this.hasAttribute( 'id' ) && // a real anchor
this.children.length === 0 && // with no Element children
this.childNodes.length === 1 && // but one Node child (we assume Text)
this.firstChild.textContent === expectedText;
} );
if ( $anchors.length === 1 ) {
window.location.hash = $anchors.first().attr( 'id' );
}
}
} );
} );