User talk:The Transhumanist/StripSearchSimple.js

In the article that follows, we will delve into the fascinating world of User talk:The Transhumanist/StripSearchSimple.js. From its impact on current society to its historical origins, through its multiple applications in different areas, we will immerse ourselves in an exhaustive analysis that will allow us to fully understand the importance of User talk:The Transhumanist/StripSearchSimple.js in our daily lives. With interviews with experts, relevant data and case studies, this article aims to shed light on a fundamental aspect that has marked a milestone in world history. Join us on this tour of User talk:The Transhumanist/StripSearchSimple.js and discover everything behind this phenomenon that is so relevant today.
This is the workshop support page for the user script StripSearchSimple.js. Comments and requests concerning the program are most welcome. Please post discussion threads below the section titled Discussions. Thank you. By the way, the various scripts I have written are listed at the bottom of the page.
This script is functional

StripSearchSimple.js: strips search results down to bare pagenames. It has no menu item to turn it off – it changes all search results pages. It's an early version of StripSearch.js. For Vector skin only.

Script's workshop

This is the work area for developing the script, and for presenting its documentation. The talk page portion of this page starts at #Discussions, below.

Description / instruction manual

This script is functional

StripSearchSimple.js: strips search results down to bare pagenames. It has no menu item to turn it off – it changes all search results pages. It's an early version of StripSearch.js. For Vector skin only.

This reduces the search results to a list of links. It strips out the data between the page names, including that annoying "from redirect" note. This makes it easier to copy and paste the links from search results into lists.

Once installed, it automatically reformats all Wikipedia search results.

How to install this script

Important: this script was developed for use with the Vector skin (it's Wikipedia's default skin), and might not work with other skins. See the top of your Preferences appearance page, to be sure Vector is the chosen skin for your account.

To install this script, add this line to your vector.js page:

importScript("User:The Transhumanist/StripSearchSimple.js");

Save the page and bypass your cache to make sure the changes take effect. By the way, only logged-in users can install scripts.

Explanatory notes (source code walk-through)

This section explains the source code, in detail. It is for JavaScript programmers, and for those who want to learn how to program in JavaScript. Hopefully, this will enable you to adapt existing source code into new user scripts with greater ease, and perhaps even compose user scripts from scratch.

You can only use so many comments in the source code before you start to choke or bury the programming itself. So, I've put short summaries in the source code, and have provided in-depth explanations here.

My intention is Threefold:

  1. to thoroughly document the script so that even relatively new JavaScript programmers can understand what it does and how it works, including the underlying programming conventions. This is so that the components and approaches can be modified, or used again and again elsewhere, with confidence. (I often build scripts by copying and pasting code that I don't fully understand, which often leads to getting stuck). To prevent getting stuck, the notes below include extensive interpretations, explanations, instructions, examples, and links to relevant documentation and tutorials, etc. Hopefully, this will help both you and I grok the source code and the language it is written in (JavaScript).
  2. to refresh my memory of exactly how the script works, in case I don't look at the source code for weeks or months.
  3. to document my understanding, so that it can be corrected. If you see that I have a misconception about something, please let me know!

In addition to plain vanilla JavaScript code, this script relies heavily on the jQuery library.

If you have any comments or questions, feel free to post them at the bottom of this page under Discussions. Be sure to {{ping}} me when you do.

General approach

The script uses the jQuery method .hide() for stripping the elements by class name. Here's an example of stripping out elements with the class name "searchalttitle":

$( ".searchalttitle" ).hide();

Learn about methods at https://www.w3schools.com/js/js_object_methods.asp

Learn about .hide at http://api.jquery.com/hide/

Aliases

An alias is one string defined to mean another. Another term for "alias" is "shortcut". In the script, the following aliases are used:

$ is the alias for jQuery (the jQuery library)

mw is the alias for mediawiki (the mediawiki library)

These two aliases are set up like this:

( function ( mw, $ ) {}( mediaWiki, jQuery ) );

That also happens to be a "bodyguard function", which is explained in the section below...

Bodyguard function

The bodyguard function assigns an alias for a name within the function, and reserves that alias for that purpose only. For example, if you want "t" to be interpreted only as "transhumanist".

Since the script uses jQuery, we want to defend jQuery's alias, the "$". The bodyguard function makes it so that "$" means only "jQuery" inside the function, even if it means something else outside the function. That is, it prevents other javascript libraries from overwriting the $() shortcut for jQuery within the function. It does this via scoping.

The bodyguard function is used like a wrapper, with the alias-containing source code inside it, typically, wrapping the whole rest of the script. Here's what a jQuery bodyguard function looks like:

1 ( function($) {
2     // you put the body of the script here
3 } ) ( jQuery );

See also: bodyguard function solution.

To extend that to lock in "mw" to mean "mediawiki", use the following (this is what the script uses):

1 ( function(mw, $) {
2     // you put the body of the script here
3 } ) (mediawiki, jQuery);

For the best explanation of the bodyguard function I've found so far, see: Solving "$(document).ready is not a function" and other problems   (Long live Spartacus!)

The ready() event listener/handler

The ready() event listener/handler makes the rest of the script wait until the page (and its DOM) is loaded and ready to be worked on. If the script tries to do its thing before the page is loaded, there won't be anything there for the script to work on (such as with scripts that will have nowhere to place the menu item mw.util.addPortletLink), and the script will fail.

In jQuery, it looks like this: $( document ).ready(function() {});

You can do that in jQuery shorthand, like this:

$().ready( function() {} );

Or even like this:

$(function() {});

The part of the script that is being made to wait goes inside the curly brackets. But you would generally start that on the next line, and put the ending curly bracket, closing parenthesis, and semicolon following that on a line of their own), like this:

1 $(function() {
2     // Body of function (or even the rest of the script) goes here, such as a click handler.
3 });

This is all explained further at the jQuery page for .ready()

For the plain vanilla version see: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

Activation filters

I didn't know what else to call these. I wanted the program to only work when intended, and only on intended pages (search result pages). So, I applied the conditional, if.

I use the Vector skin, and haven't tested the script on any other skin, so the script basically says "if the vector skin is in use, do what's between the curly brackets". (Which includes the entire rest of the program).

// Only activate on Vector skin
        if ( mw.config.get( 'skin' ) === 'vector' ) {

	        // Run this script only if " - Search results - Wikipedia" is in the page title
			if (document.title.indexOf(" - Search results - Wikipedia") != -1) {

Prep work

There is no prep work in this script. This would be the declaration of global variables and so on.

Core program

This is the part controls the main flow of the script (decides what to do under what circumstances):

            if ( mw.config.get( 'skin' ) === 'vector' ) {
                $( function() {

                    // hide elements by class per http://api.jquery.com/hide
                    $( ".searchalttitle" ).hide();
                    $( ".searchresult" ).hide();
                    $( ".mw-search-result-data" ).hide();

                } );
            }

So, what this does is 4 things:

First, it checks if the Vector skin is being used and runs the rest of the script only if it is.

Then it applies the jQuery method .hide on all elements labeled as any of these 3 classes: searchalttitle, searchresult, or mw-search-result-data.

To use an object method, you append it to the end of an element, as is done with .hide() 3 times above. Don't forget the parentheses, and be sure to end your statements with a semicolon.

Learn more about .hide at http://api.jquery.com/hide/

mw.config.get ( 'skin' )

This looks up the value for skin (the internal name of the currently used skin) saved in MediaWiki's configuration file.

logical operators

"===" means "equal value and equal type"

Strip out the sister project results

	// Hide interwiki results (per http://api.jquery.com/hide)
	$('#mw-interwiki-results').hide();

To write the above, I searched the pagesource for the classes of the data displayed in the right-hand column, traced back to their parent "id="mw-interwiki-results", wrapped that in a jQuery object ("$" means "jQuery"), and then attached the hide method to it.

Change log

  • 2017-09-25
    • Started script
    • Version 1.0 complete
      • Removes lines with page data from the search results
      • Removes content excerpts from the search results
      • Removes "from redirect" comments from the search results
    • Strips out sister project results (from right-hand column)

Task list

Bug reports

Desired/completed features

Completed features are marked with  Done

Improvements that would be nice:

  • Remove lines with page data from search results  Done
  • Remove content excerpts from search results  Done
  • Remove "from redirect" comments from search results  Done
  • Strip sister project results  Done
  • Remove the drawn lines between the search result entries
  • Remove alternating background colors from behind the search results
  • Reduce spacing between result entries
  • A note at the top of the search column "search results formatted by StripSearch.js", linked to the instruction manual section of the script's workshop page.
  • Remove redirected entries from the search results

Development notes

Remove lines between the search result entries

The lines seem to be part of the configuration of li, or one of the classes.

Perhaps whichever one it is can be replaced with another element with different formatting.

Reduce spacing between result entries

Add note to search results page

Add a note at the top of the search column "search results formatted by StripSearch.js, linked to the instruction manual section of the script's workshop page.

See also

Discussions

Post new discussion threads below.

  1. ^