MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 951: | Line 951: | ||
wireUllekhaLinks(); | wireUllekhaLinks(); | ||
} | } | ||
}() ); | }() );/* ── Search result highlight — highlight query term on arrival ── | ||
/* ── Search result highlight — highlight query term on arrival ── | |||
* When user clicks a search result, we pass the query in the URL | * When user clicks a search result, we pass the query in the URL | ||
* hash as #gr-search:QUERY or read it from sessionStorage. | * hash as #gr-search:QUERY or read it from sessionStorage. | ||
| Line 963: | Line 962: | ||
function storeQueryForLink( url, query ) { | function storeQueryForLink( url, query ) { | ||
try { | try { | ||
/* Store just the pathname so protocol/host differences don't break matching */ | |||
var a = document.createElement( 'a' ); | |||
a.href = url; | |||
sessionStorage.setItem( 'gr_search_hl', JSON.stringify({ | sessionStorage.setItem( 'gr_search_hl', JSON.stringify({ | ||
query: query, | query: query, | ||
pathname: a.pathname // e.g. "/Brahmasutra" | |||
}) ); | }) ); | ||
} catch(e) {} | } catch(e) {} | ||
| Line 978: | Line 980: | ||
if ( !stored || !stored.query ) return; | if ( !stored || !stored.query ) return; | ||
/* Check | /* Check pathname matches — lenient comparison */ | ||
var | var currentPath = window.location.pathname; | ||
if ( | var storedPath = stored.pathname || ''; | ||
/* Normalize: remove trailing slash, decode */ | |||
function normPath(p) { return decodeURIComponent(p).replace(/\/+$/, ''); } | |||
if ( storedPath && normPath(storedPath) !== normPath(currentPath) ) { | |||
/* Different page — clear and bail */ | /* Different page — clear and bail */ | ||
try { sessionStorage.removeItem( 'gr_search_hl' ); } catch(e) {} | try { sessionStorage.removeItem( 'gr_search_hl' ); } catch(e) {} | ||
| Line 992: | Line 997: | ||
try { sessionStorage.removeItem( 'gr_search_hl' ); } catch(e) {} | try { sessionStorage.removeItem( 'gr_search_hl' ); } catch(e) {} | ||
/* Wait for content to | /* Wait for content + Common.js tagTextNodes to finish rendering */ | ||
setTimeout( function () { | var delays = [400, 900, 1500]; | ||
delays.forEach( function(ms) { | |||
} | setTimeout( function () { | ||
/* Only run if not already highlighted */ | |||
if ( !document.querySelector( '.gr-search-hl' ) ) { | |||
highlightText( query ); | |||
} | |||
}, ms ); | |||
} ); | |||
} | } | ||