_ _ _ _____ ___ __ __ _(_) | _(_)___ / ( _ ) / /_ ___ ___ _ __ ___ \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ \ V V /| | <| |___) | (_) | (_) | (_| (_) | | | | | | \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|
StripSearchInWikicode.js: strips search results down to bare pagenames and adds bullet list wikicode formatting for easy copying and pasting into articles. For Vector skin only.
StripSearchInWikicode.js: strips search results down to bare pagenames and adds bullet list wikicode formatting for easy copying and pasting into articles. It also removes redirected entries, and is especially useful for "intitle:" searches. 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. It adds * ]
to each entry so they look like this:
* ]
* ]
* ]
* ]
* ]
This makes it easier to copy and paste the links from search results into articles.
Once installed, the script automatically processes your Wikipedia search results.
To install, add this line to your vector.js page:
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/StripSearchInWikicode.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.
If you want the detail back in your search results, remove that line, or comment it out by placing two forward slashes (//) at the beginning of it.
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:
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.
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/
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...
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 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()
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) {
There is no prep work in this script. This would be the declaration of global variables and so on.
This is the part that 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/
This looks up the value for skin (the internal name of the currently used skin) saved in MediaWiki's configuration file.
"===
" means "equal value and equal type"
// hide elements of Results from sister projects (per http://api.jquery.com/hide)
$( ".iw-headline" ).hide();
$( ".iw-results" ).hide();
$( ".iw-resultset" ).hide();
$( ".iw-result__title" ).hide();
$( ".iw-result__content" ).hide();
$( ".iw-result__footer" ).hide();
I went through the pagesource looking for the classes of the data displayed in the right-hand column, and inserted them into the code above. (I assume "iw" stands for "interwiki").
The elements that I wish to change have the class mw-search-result-heading.
Each one has an anchor element within it. Perhaps those can be sandwiched with the desired wikicode (between the double square brackets).
Maybe using .splice could work, if regex could be applied somehow.
for (var i = 0; i < x.length; i++) {
// if current array item matches "searchalttitle"
// remove it from array
// x.splice(i)
// i = i--
}
In the loop above, splicing (removing) the current item would shift the next item into its position. When the loop iterates to the next item, it will have inadvertently skipped one. After splicing, you'd have to decrement i by one.
Or use forEach, and...
push all non-matches in a new array, and at the end of forEach replace the original array with the new one.
Or, using standard for loop...
iterate over the array index and decrease the loop index i-- whenever you find a match
The search results are double spaced, which shows up as a blank line between each list item when you cut and paste to an edit window.
First, it might help to be able to see the control characters (like linefeed, \n). One way to look for them is with this:
// Inspect the raw text, so you can look for \n linefeeds
$(".mw-search-results").each(function(index) {
let mwsr_text = JSON.stringify($(this).text());
alert(mwsr_text);
});
This showed the text, but didn't show the linefeeds (\n). Logically, they must be there. The linefeed characters don't show up in the editor I cut and pasted them into. But the editor's search/replace is still able to find/replace them. Therefore, it might be possible to use regex in JS to get rid of them on the web page.
So, I tried the following code to remove linefeeds (\n), but it didn't work.
var str = $(".mw-search-results").html();
var regex = /\n/gi;
$(".mw-search-results").html(str.replace(regex, ""));
I tried it on \s, and it got rid of the linefeeds along with all the other white space characters. Which means they may be specifically accessible.
Post messages below.
I've written a script called StripSearch.js that unclutters search results to make them bare lists of page names. .
Now I'm writing a sequel to it called StripSearchInWikicode.js.
I would like the output of search results to look like this:
* ]
* ]
* ]
* ]
* ]
...for easy copying and pasting into articles.
I'm having trouble manipulating the elements of class "mw-search-result-heading".
I gather that you put them into an array like this:
var x = document.getElementsByClassName("mw-search-result-heading");
I'd like to subject the items in that array to a regex, using the jQuery .each method, or the .each function, but I don't know how. The documentation is confusing as hell.
I think the search string (<a.*a>)
and replacement string * ]
ought to work.
Any pointers would be most appreciated.
Sincerely, The Transhumanist 12:58, 29 September 2017 (UTC)
$(".mw-search-result-heading").prepend('* ]');
$(".mw-search-result-heading").children().before('* ]');
]]
) - Evad37 13:53, 29 September 2017 (UTC)mw-search-result-heading
entries that include searchalttitle
inside their divs. I would like to remove just those instances of mw-search-result-heading
..not
work for this, to hide divs with the class mw-search-result-heading
except for those that do not contain searchalttitle
? // Strip out redirected entries
var x = document.getElementsByClassName("mw-search-result-heading");
for (i = 0; i < x.length; i++) {
// somehow remove this entry if
// it contains element of class "searchalttitle"
}
// Sort the search results
var x = document.getElementsByClassName("mw-search-result-heading");
x.sort();
$(".mw-search-result-heading").children().filter(':first-child').before('* ]');
$(".searchalttitle").remove();
$(".mw-search-result-heading").children().filter(':first-child').before('* ]');
.has()
:$(".mw-search-result-heading").has(".searchalttitle").remove();
$(".mw-search-result-heading").children().before('* ]');
$(".mw-search-result-heading").has(".searchalttitle").remove().end().children().before('* ]');
.hide()
instead of .remove()
if you want to be able to show those elements again at some point. - Evad37 02:52, 30 September 2017 (UTC)Wow. You make it looks so easy. So, you chain methods to a selector. Nice. That sure is convenient. jQuery is simpler than I thought. When you chain methods to a class, they work on all the elements of that class. I was doing that with hide, but was just copying the examples and didn't really grasp the underlying structure. Thank you. And on retrospect, with loops and regex, it looks like I was trying to conduct surgery with an icecream scoop. :)
I try to follow along in the documentation during these discussions, so that I can grasp the jargon. While doing so, I noticed this:
$(".searchalttitle").remove();
$(".mw-search-result-heading").children().filter(':first-child').before('* ]');
can be refactored to this:
$(".searchalttitle").remove();
$(".mw-search-result-heading").children(':first-child').before('* ]');
It seems to work!
The script is now operational, thanks to you. But, I came across an unforeseen obstacle. The results look great on the search results page, but when you copy and paste them into an edit page, there is a blank line between all the entries. That requires that the user regex them all out in WikEd. I'd like to eliminate that manual operation by removing the blank lines in the search results.
Also, when we remove the .mw-search-result-heading entries that contain .searchalttitle, additional blank lines are left behind. Is that a clue that can help us track those newlines (\n) down?
It is not apparent where the newlines are inserted in the page source for the search results page. So, I assume they are specified on a style sheet somewhere. What is the most effective way to hunt down the style sheet which defines a particular class used on a Wikipedia page? The Transhumanist 21:24, 30 September 2017 (UTC)
Extended content
|
---|
There is a page named "Genre" on Wikipedia * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] |
Extended content
|
---|
There is a page named "Genre" on Wikipedia * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] |
Extended content
|
---|
There is a page named "Genre" on Wikipedia * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] * ] |
// nuke "li" instead of ".mw-search-result-heading"
$("li").has(".searchalttitle").remove();
var str = $(".mw-search-results").html();
var regex = /\n/gi;
$(".mw-search-results").html(str.replace(regex, ""));
\s
is equivalent to
, so one of those should work. There are various regex-testing website you could use to test, analyse, explain, and experiment with regex patterns – I use https://regex101.com/ (just need to make sure the 'flavor' is javascript), but there are others out there.I'm wondering why the double spacing (extra newline) between list items doesn't show up in the page source.– Since I didn't have the problem with Chrome on Win 7, and FF/IE had different problems to what you're describing, I think its basically down to either browser bugs (or "features") – possibly MediaWiki is serving up (or the JavaScript modification is making) non-standard/non-compliant code, and the browsers have to decide for themselves how to handle it (thus some insert phantom spaces, others don't). - Evad37 13:21, 4 October 2017 (UTC)
User:The Transhumanist/StripSearchInWikicode.js – the recent script you helped me on, which strips WP search results down to a bare list of links, and inserts wikilink formatting for ease of insertion of those links into lists. This is useful for gathering links for outlines. It still has the interlaced CR/LFs problem. Aside from that, I'd like this script to sort its results. So, if you know how, or know someone who knows how, please let me know.
*]
, and have the whole thing within a block tag like <div> or <p>. Do that for each item in the array, and then you can add them all to (or next to) an element on the page using a jQuery method like .before(), .after(), .prepend(), or .append(), each of which can take an array as the input. - Evad37 02:40, 27 October 2017 (UTC)