User:Polygnotus/Scripts/ScrollToMe.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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/ScrollToMe.
// =====================================================================
// Scroll-to-username on search results
// Adds a link above each search result title. The target username is
// passed via URL parameter so new-tab navigation works correctly.
// =====================================================================
( function () {
'use strict';
var USERNAME = mw.config.get( 'wgUserName' );
var PARAM_NAME = 'wpScrollTo';
if ( !USERNAME ) {
return;
}
// ------------------------------------------------------------------
// PART 1 – On the destination page: scroll to the first occurrence
// ------------------------------------------------------------------
function checkAndScroll() {
var target = mw.util.getParamValue( PARAM_NAME );
if ( !target ) {
return;
}
mw.hook( 'wikipage.content' ).add( function () {
scrollToFirstOccurrence( target );
} );
}
function scrollToFirstOccurrence( username ) {
var contentRoot = document.getElementById( 'mw-content-text' ) || document.body;
var titleHeading = document.getElementById( 'firstHeading' );
var walker = document.createTreeWalker(
contentRoot,
NodeFilter.SHOW_TEXT,
{
acceptNode: function ( node ) {
var tag = node.parentElement && node.parentElement.tagName;
if ( tag === 'SCRIPT' || tag === 'STYLE' ) {
return NodeFilter.FILTER_REJECT;
}
// Skip anything inside the page title heading.
if ( titleHeading && titleHeading.contains( node ) ) {
return NodeFilter.FILTER_REJECT;
}
return node.nodeValue.indexOf( username ) !== -1
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
}
},
false
);
var node = walker.nextNode();
if ( !node ) {
return;
}
var el = node.parentElement;
el.scrollIntoView( { behavior: 'smooth', block: 'center' } );
var originalBg = el.style.backgroundColor;
el.style.transition = 'background-color 0.3s';
el.style.backgroundColor = '#ff9';
setTimeout( function () {
el.style.backgroundColor = originalBg;
}, 2500 );
}
// ------------------------------------------------------------------
// PART 2 – On the search results page: add scroll links
// ------------------------------------------------------------------
function addScrollLinks() {
var resultHeadings = document.querySelectorAll( '.mw-search-result-heading' );
resultHeadings.forEach( function ( heading ) {
var titleLink = heading.querySelector( 'a' );
if ( !titleLink ) {
return;
}
// Build the destination URL with the scroll parameter baked in.
var destUrl = new URL( titleLink.href );
destUrl.searchParams.set( PARAM_NAME, USERNAME );
var scrollLink = document.createElement( 'a' );
scrollLink.href = destUrl.href;
scrollLink.title = 'Go to this page and scroll to "' + USERNAME + '"';
scrollLink.textContent = '↓ scroll to "' + USERNAME + '"';
scrollLink.style.cssText = [
'display: block',
'margin-bottom: 0.25em',
'font-size: 0.82em',
'font-weight: normal'
].join( ';' );
// Insert above the title link, not after it.
heading.insertBefore( scrollLink, titleLink );
} );
}
// ------------------------------------------------------------------
// Entry point
// ------------------------------------------------------------------
var specialPage = mw.config.get( 'wgCanonicalSpecialPageName' );
if ( specialPage === 'Search' ) {
var query = mw.util.getParamValue( 'search' ) || '';
if ( query.toLowerCase().indexOf( USERNAME.toLowerCase() ) !== -1 ) {
$( function () {
addScrollLinks();
} );
}
} else {
checkAndScroll();
}
}() );