User:The Transhumanist/StripSearch.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 user script seems to have a documentation page at User:The Transhumanist/StripSearch. |
// <syntaxhighlight lang="javascript">
/*
StripSearch.js: strips Wikipedia search results down to bare pagenames. It provides
the "SR detail" menu item to hide/show the search results' details and the sister
project results. Search results are preprocessed to remove all redirected entries
and members of matching categories, so that they do not show up at all.
Version 1.4 -- operational.
This script uses the core control structure and subroutines from
[[User:The Transhumanist/OutlineViewAnnotationToggler.js]]:
https://en.wikipedia.org/w/index.php?title=User:The_Transhumanist/OutlineViewAnnotationToggler.js&oldid=807301505
Brief comments are provided within the sourcecode below. For extensive explanatory
notes on what the source code does and how it works, see the Script's workshop on
the talk page. (Not ready yet.)
*/
// ============== Set up ==============
// Start off with a bodyguard function to reserve the aliases mw and $
( function ( mw, $ ) {
// we can now rely on mw and $ within the safety of our “bodyguard” function, to mean
// "mediawiki" and "jQuery", respectively (they follow the closing curly bracket - see the end of the script)
// ============== Load dependencies ==============
mw.loader.using( ['mediawiki.util'], function () {
// ============== ready() event listener/handler ==============
// below is jQuery short-hand for $(document).ready(function() { ... });
// it makes the rest of the script wait until the page's DOM is loaded and ready
$(function() {
// ============== activation filters ==============
// Run this script only if " - Search results - Wikipedia" is in the page title
if (document.title.indexOf(" - Search results - Wikipedia") != -1) {
// End of set up
// =================== Prep work =====================
// Variable declarations, etc., go here
var SRDetailSwitch;
// ================== Core program ===================
// Strip out redirected entries, and entries from matching categories
$("li").has(".searchalttitle").remove();
// get the value of our status variable from memory
// (this tells us what mode to start in)
// "SR" stands for "search results"
var SRDetailStatus = localStorage.getItem('SRDetailStatus');
// Core control construct:
// run the function corresponding to the current status
if ( SRDetailStatus === "hide" ) {
SRDetailHide();
} else {
SRDetailShow();
}
}
// ======================== Subroutines ===========================
// Functions (aka subroutines) are activated only when they are called.
// Below are the functions called in the core control structure of the program above.
// They are placed at the end of the program, so that the script's flow
// is easier to follow.
// ============ Function to hide search results' detail ==============
function SRDetailHide() {
// store status so it persists across page loads
localStorage.setItem("SRDetailStatus", "hide");
// Hide the search results' details (show elements by class per http://api.jquery.com/hide)
//$( ".searchalttitle" ).hide();
//$( ".searchresult" ).hide();
//$( ".mw-search-result-data" ).hide();
// Replace the search results by hiding the original results and use .after to insert a modified version of those results
// Development note: somehow assign an id of "Stripped" to the contents of the .after chain below, so that it can be accessed later
$('ul.mw-search-results').hide().after(
$('<div id="Stripped"></div>').append(
$('ul.mw-search-results')
.children()
.map( function() {
return $(this).find('a').text();
})
.get()
.map( function(title) {
return $('<div>').append(
$('<a>').attr({
'href':'https://en.wikipedia.org/wiki/'+mw.util.wikiUrlencode(title),
'target':'_blank'
}).text(title),
' ');
})
)
);
// Hide interwiki results (per http://api.jquery.com/hide)
$('#mw-interwiki-results').hide();
// now we have to update the menu item
// (referred to in this script as "SRDetailSwitch").
// To do that, first we remove it (if it exists):
if ( SRDetailSwitch ) {
SRDetailSwitch.parentNode.removeChild(SRDetailSwitch);
}
// and then we create it (or its replacement) from scratch:
SRDetailSwitch = mw.util.addPortletLink( 'p-tb', '#', 'SR detail \(show\)', 'Show search results detail' );
// make the menu item clickable by binding it to a click handler
// (which activates the actions between the curly brackets when clicked):
$( SRDetailSwitch ).click( function ( e ) {
e.preventDefault(); // prevents any default action -- we want only the following action to run:
SRDetailShow();
} );
}
// ============ Function to show search results detail ==============
function SRDetailShow() {
// store status so it persists across page loads
localStorage.setItem("SRDetailStatus", "show");
// Show full results
// But first, we must remove the .after chain to which we assigned the id "Stripped"
$( "#Stripped" ).remove();
// Now show the original results:
$('ul.mw-search-results').show().find('*').show();
// Show interwiki results (per http://api.jquery.com/show)
$('#mw-interwiki-results').show();
// now we have to update the menu item
// (referred to in this script as "SRDetailSwitch").
// To do that, first we remove it (if it exists):
if ( SRDetailSwitch ) {
SRDetailSwitch.parentNode.removeChild(SRDetailSwitch);
}
// and then we create it (or its replacement) from scratch:
SRDetailSwitch = mw.util.addPortletLink( 'p-tb', '#', 'SR detail \(hide\)', 'Hide search results detail' );
$( SRDetailSwitch ).click( function ( e ) {
e.preventDefault(); // prevents any default action -- we want only the following action to run:
SRDetailHide();
} );
}
} );
} );
}( mediaWiki, jQuery ) );
// </syntaxhighlight>