Content deleted Content added
== Task list == === Bugs === === Desired/completed features === : ''Completed features are marked with {{done}}'' |
→Change log: the |
||
(30 intermediate revisions by the same user not shown) | |||
Line 1:
{{User:The Transhumanist/Workshop boilerplate/Lead hatnote}}
: '''''This script is
[[User:The Transhumanist/StripSearchSorted.js|StripSearchSorted.js]]:
= Script's workshop =
Line 10:
== Description / instruction manual ==
: '''''This script is operational''''', ''but there is a quirk in wikEd: When results are copied/pasted into wikEd, the results are erroneously double spaced. Clicking on undo in wikEd reverts it to single spaced as initially intended. (I don't know why. If you do, please tell me.)''
[[User:The Transhumanist/
<nowiki>* [[</nowiki>[[Brad Pitt]]<nowiki>]]</nowiki></br>
<nowiki>* [[</nowiki>[[Clint Eastwood]]<nowiki>]]</nowiki></br>
<nowiki>* [[</nowiki>[[Dwayne Johnson]]<nowiki>]]</nowiki></br>
<nowiki>* [[</nowiki>[[John Wayne]]<nowiki>]]</nowiki></br>
<nowiki>* [[</nowiki>[[Tom Cruise]]<nowiki>]]</nowiki></br>
Once installed, the menu item "SR sort" will appear in the side bar tools menu, specifying what action it is ready to perform (either "turn on" or "turn off").
{{User:The Transhumanist/Workshop boilerplate/Install}}
=== Known issues ===
Quirk in wikEd: When results are copied/pasted into wikEd, the results are erroneously double spaced. Clicking on undo in wikEd reverts it to single spaced as initially intended. (I don't know why. If you do, please tell me.)
=== General approach ===
The script uses the jQuery method .hide() for
<syntaxhighlight lang="javascript">
$( ".searchalttitle" ).hide();
</syntaxhighlight>
Learn about methods at https://www.w3schools.com/js/js_object_methods.asp
Line 56 ⟶ 54:
=== 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 [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals conditional, '''''if'''''], as follows...
==== Vector skin activation filter ====
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 rest of the main program. Note that functions, aka subroutines, follow after the main program.).
<syntaxhighlight lang="javascript">
// Only activate on Vector skin
if ( mw.config.get( 'skin' ) === 'vector' ) {
// The rest of the script goes here
}
</syntaxhighlight>
===== <div style="font-size:90%">mw.config.get ( 'skin' )</div> =====
This looks up the value for skin (the internal name of the currently used skin) saved in MediaWiki's configuration file.
* [https://www.w3schools.com/jquery/ajax_get.asp jQuery get() Method]
* [https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config mw.config]
===== <div style="font-size:90%">logical operators</div> =====
"<code>===</code>" means "equal value and equal type"
* [https://www.w3schools.com/js/js_comparisons.asp JavaScript Comparison and Logical Operators]
==== Page title activation filter ====
<syntaxhighlight lang="javascript">
// Run this script only if " - Search results - Wikipedia" is in the page title
if (document.title.indexOf(" - Search results - Wikipedia") != -1) {
// The rest of the script goes here
}
</syntaxhighlight>
Line 132 ⟶ 153:
* 2017-10-27
** Forked script from a copy of [[User:The Transhumanist/StripSearchInWikicode.js]], and forked this workshop from a copy of [[User talk:The Transhumanist/StripSearchInWikicode.js]].
* 2017-
** Evad37 provided sequence for sorting the search results
* 2017-11-09
** Add toggle switch (dual menu item)
** Apply class of "Stripped" to the modified results, so that they can be removed to make way for original results
** Make switch swap out results between original and modified, and vice versa
* 2018-01-20
** Added TrueMatch function (intitle bug workaround)
*** Evad37 provided the 2 key lines
== Task list ==
===
=== Desired/completed features ===
Line 152 ⟶ 181:
Run the function if Title includes "intitle:"
Parse the Title with regex to get the intitle string. The string may be a single word or a phrase within double quotation marks. Use regex pipe for or.
Then keep only the search results that include that string. One way to do this is use a regex to inverse match via negative look-arounds. <code><nowiki>^((?!hi there).
See annotationToggle for how to wrap entries in classed span tags, and then hide those spans. But do it with jQuery instead.
Line 501 ⟶ 530:
== Adding a filter to StripSearchSorted.js ==
:''Originally posted to Evad37's talk page:''
There's a really annoying design flaw in WP's search's intitle feature. Common words like "of" are ignored, even though they are included within a quoted phrase. So, intitle:"of Boston" is interpreted as just intitle:Boston. And the search results are filled with non-matching results. To make matters worse, the search results include matches of the phrase in the contents of pages, watering the results down even more to inlcude pages that don't even have "Boston" in the title. What I need is for results to strictly match the term provided after "intitle:".
Line 574 ⟶ 603:
</syntaxhighlight>
:- <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 02:34, 8 December 2017 (UTC)
:: So, let me see if I got this straight...
:: You store the search's intitle value in a variable, and if there isn't one, the variable's value would just be null.
:: Then, in the chain, filter out non-matching entries. If the variable has a null value, meaning that intitle wasn't included in the search, all entries would match.
:: Is that correct? ''[[User talk:The Transhumanist|The Transhumanist]]'' 21:50, 10 December 2017 (UTC)
:::Not quite... <code>null</code> doesn't match anything, so no entries would match. To get all entries to match, you either have to set the variable to something that does actually match any entry (<code>intitleString = '<nowiki></nowiki>'</code> or <code>intitlePatt = /./</code> depending on whether you use indexOf or regex matching inside the filter); or else have an explicit check inside the filter which will just return true if the variable is null. - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 03:02, 11 December 2017 (UTC)
:::: So, there is no way to match null in regex? So you can't match null or whatever the string is, using the pipe character? ''[[User talk:The Transhumanist|The Transhumanist]]'' 04:26, 11 December 2017 (UTC)
:::::If you want to check if a variable is null or undefined, just do <code>someVar == null</code> (gives true if someVar is null/undefined, false otherwise). You can combine this with other logical tests using <code>||</code> , <code>&&</code> , <code>!</code> as usual. - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 04:37, 11 December 2017 (UTC)
== Adding TrueMatch to StripSearchSorted ==
:''Originally posted to Evad37's talk page''
I'm in the process of trying to fix the intitle bug in Wikipedia's search, by providing the solution as a function within [[User:The Transhumanist/StripSearchSorted.js|StripSearchSorted.js]].
The intitle bug is that when you enter a search phrase in WP's search box with a common word (like this: intitle:"in Germany"), the titles in the search results don't match.
I'm almost done, but I can't figure out how to get :contains to accept a variable:
<syntaxhighlight lang="javascript">
function TrueMatch() {
// The purpose of this function is to filter out non-matches
// Activation filter:
// Run this function only if 'intitle:"' is in the page title
// Notice the lone " after intitle:
if (document.title.indexOf('intitle:"') != -1) {
// Body of function
// Create variable with page title
var docTitle = document.title;
// Display on screen for checking
//alert ( docTitle );
// Extract the intitle search string from the html page title
// We want the part between the quotation marks
var regexIntitle = new RegExp('intitle:"(.+?)(")(.*)','i');
var intitle;
intitle = docTitle.replace(regexIntitle,"$1");
//alert ( intitle );
// Filter out search results that do not match
$( "li" ).not( 'li:contains(" + intitle + ")' ).remove();
}
}
</syntaxhighlight>
It works fine up until that last line. I want to remove all li elements that do not contain the text in the intitle variable. ''[[User talk:The Transhumanist|The Transhumanist]]'' 07:51, 19 January 2018 (UTC)
:Instead of passing a single string for the selector, you need to build a string up around the variable:
:<syntaxhighlight lang="javascript">$( "li" ).not( 'li:contains("' + intitle + '")' ).remove();</syntaxhighlight>
:The <code>'li:contains("' + intitle + '")'</code> gets processed first, and the result is passed through to <code>.not()</code>. Or if you wanted to be a bit more explicit, you could do something like
:<syntaxhighlight lang="javascript">
var intitle_selector = 'li:contains("' + intitle + '")';
$( "li" ).not( intitle_selector ).remove();
</syntaxhighlight>
: - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 13:59, 19 January 2018 (UTC)
:: I tried both methods you posted above. I tested it on intitle:"of Australia". The script runs, and strips out the details as it is supposed to. And it is sorting the results. But it isn't removing the non-matches. It's like it's matching everything, and therefore removing nothing. (When it matches nothing, like in my version above, it removes everything, leaving the results blank). I reactivated the alerts, and those show up fine. It's still the last line that isn't working. When you replace it with <code>$( "li" ).remove();</code>, it removes all results. ''[[User talk:The Transhumanist|The Transhumanist]]'' 02:16, 20 January 2018 (UTC)
:: Testing further on the "of Australia" search...
:: <code>$( "li").not('li:contains( "of" )').remove();</code> resulted in blank results (ie, none).
:: <code>$( "li").not('li:contains( of )').remove();</code> resulted in no matches (ie results unaffected).
:: So, it looks like the first one is matching nothing, causing all li elements to be removed, while the second one is matching everything, causing no li elements to be removed. ''[[User talk:The Transhumanist|The Transhumanist]]'' 03:11, 20 January 2018 (UTC)
:: I stared at the current page source, and discovered spans with the class "searchmatch", the contents of which appear to have been causing false matches. So, I blasted those with:
::<syntaxhighlight lang="javascript">
// First, strip out the searchmatch class elements (they match).
$( 'li').find( '.searchmatch').remove();
</syntaxhighlight>
:: Then, with the above line in place, I tested your solution again, but it didn't work:
::<syntaxhighlight lang="javascript">
$( "li" ).not( 'li:contains("' + intitle + '")' ).remove();
</syntaxhighlight>
:: The results turned up blank, which means it removed everything.
:: Ironically, doing the opposite works:
::<syntaxhighlight lang="javascript">
$( 'li:contains("' + intitle + '")').remove();
</syntaxhighlight>
:: Unfortunately, this removes precisely the entries the user wants to keep. ''[[User talk:The Transhumanist|The Transhumanist]]'' 08:26, 20 January 2018 (UTC)
:::I think we need to be more specific, and target the main link of each result - since the value of <code>intitle</code> will be somewhere within the <code>li</code>, just not neccesarily in the title. Plus we can limit the searching of <code>li</code>s to just the search results, rather than the whole page:
:::<syntaxhighlight lang="javascript">
// Mark true results with a class
$('.mw-search-results').find('li').has( 'div > a:contains("' + intitle + '")' ).addClass('truematch');
// Remove other results
$('.mw-search-results').find('li').not('.truematch').remove();
</syntaxhighlight>
:::Which basically means: In the <code>mw-search-results</code>, find the <code>li</code>s which have a <code>div</code> that itself has (as a direct child element) an <code>a</code> that contains the text <code>intitle</code>, and add the class <code>truematch</code> to those <code>li</code>s. Then, in the <code>mw-search-results</code>, find the <code>li</code>s which do not have the class <code>truematch</code>, and remove them. - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 09:25, 20 January 2018 (UTC)
:::: That did the trick. It works beautifully. Thank you.
:::: This leads the way to the development of two related programs:
::::# StripSearchFilter.js – will allow the user to enter additional search terms to filter down the results, including a term to keep or a term to discard. Can use it multiple times to further refine the result.
::::# SearchSuite.js – will put selected features on their own switches so they can be turned on and off. Like the details stripping, and the inserted wikicode. It will also include the search filter feature mentioned above.
:::: I'll keep you posted. ''[[User talk:The Transhumanist|The Transhumanist]]'' 11:28, 20 January 2018 (UTC)
|