User:JAAqqO/EnhancedSearchPage.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* jshint maxerr:10000 */ // per [[phab:T1092]]
/*

Installation:
	See the documentation page at [[User:JAAqqO/EnhancedSearchPage]] or
	https://en.wikipedia.org/wiki/User:JAAqqO/EnhancedSearchPage

What this does:
	This script adds the following features to the search page [[Special:Search]]:
		1. Adds "Show 5000 results" into the two "Show x results" menus
		2. Adds a shortened results list and four copy buttons to quickly copy the results to the clipboard (either plain text, wikitext or PetScan links)
		3. Adds buttons to quickly scroll up and down
		4. Adds "x results shown on the page" next to the results info
		5. Adds (or brings back?) the red color and slightly bigger font for the "No results found for/Did you mean" notification

Updated: 15 March 2024

*/
//---------------------- BEGINS: this makes a copy of the search page's "Show x results" to top (disabled on 12 June 2023 because this is now a default feature of MediaWiki)
//$(".mw-search-pager-bottom").clone().prependTo(".mw-search-results").css("padding", "5px 0px 10px 0px");
//---------------------- ENDS: this makes a copy of the search page's "Show x results" to top (disabled on 12 June 2023 because this is now a default feature of MediaWiki)
//
//
//
//
//---------------------- BEGINS: this adds an option for 5000 into the two "Show x results" menus
var JAAqqOfirst = $('<span> | </span>').insertAfter('.mw-numlink:eq(4)');

$('<a>', {
	text: '5000',
	title: 'Show 5000 results per page',
	href: "" + window.location.href + "&limit=5000",
}).insertAfter('.mw-numlink:eq(4)').before(JAAqqOfirst);

var JAAqqOsecond = $('<span> | </span>').insertAfter('.mw-numlink:eq(9)');

$('<a>', {
	text: '5000',
	title: 'Show 5000 results per page',
	href: "" + window.location.href + "&limit=5000",
}).insertAfter('.mw-numlink:eq(9)').before(JAAqqOsecond);
//---------------------- ENDS: this adds an option for 5000 into the two "Show x results" menus
//
//
//
//
//---------------------- BEGINS: this adds a shortened results list and four copy buttons
var JAAqqOhide = $("<div></div>").css({
	"width": "30%",
	"border": "1px solid grey",
	"margin": "5px",
	"padding": "5px",
	"background-color": "lightyellow",
	"borderRadius": "4px",
}).hover(function() {
	$(this).css("background-color", "#ffffef");
}, function() {
	$(this).css("background-color", "lightyellow");
});

var JAAqqOexistelmnt = document.getElementById("mw-interwiki-results");
if (typeof(JAAqqOexistelmnt) != 'undefined' && JAAqqOexistelmnt != null) {
	$(JAAqqOhide).css({
	"float": "right",
	"clear": "right",
}).insertBefore("#mw-interwiki-results");
} else {
	$(JAAqqOhide).css({
    "position": "absolute",
    "top": "20em",
    "right": "0",
}).insertBefore(".searchresults");
}

var JAAqqOhowmany = $( ".mw-search-result-data" ).length;

var JAAqqOhowmany2 = $( ".results-info" ).attr("data-mw-num-results-total").replace(/[0-9](?=(?:[0-9]{3})+(?![0-9]))/g, "$&,");

var JAAqqOtitle = $("<div>Show a shortened list of<br />this page's " + JAAqqOhowmany + " results <small>(of total of " + JAAqqOhowmany2 + ")</small></div>").css({
	"text-align": "center",
	"font-weight": "bold",
	"cursor": "pointer"
}).appendTo(JAAqqOhide);

var JAAqqOplace = $("<div></div>").css({
	"width": "250x",
	"height": "200px"
}).appendTo(JAAqqOhide).hide();

var JAAqqOclone = $(".mw-search-result-heading").clone().appendTo(JAAqqOplace);

var JAAqqOcss = {
    "text-align": "center",
    "font-weight": "bold",
    "width": "15%",
    "border": "1px solid grey",
    "margin": "5px",
    "padding": "5px",
    "background-color": "lightyellow",
    "borderRadius": "4px",
    "cursor": "pointer"
};

var JAAqqOnote1 = $("<div>Copy this page\'s " + JAAqqOhowmany + " results to the clipboard as...</div>").css({
	"text-align": "center",
	"font-weight": "bold",
	"width": "30%",
	"padding": "5px"
}).insertAfter(JAAqqOhide).hide();

var JAAqqOnote2 = $("<div>...plain text<sup> (?)</sup></div>").css(JAAqqOcss).insertAfter(JAAqqOnote1).hide().hover(function() {
    $(this).css("background-color", "#ffffef");
    $(this).attr('title', 'This button copies this page\'s ' + JAAqqOhowmany + ' results to the clipboard as plain text, i.e. without wikitext markup. Since this copies only the results shown on this search page, make sure you have clicked the "view 20, 50, 100, 250, 500, 5000" link. This format is compatible with PetScan and AutoWikiBrowser (AWB) for example.'); // [[Wikipedia:AutoWikiBrowser]]
}, function() {
    $(this).css("background-color", "lightyellow");
});

var JAAqqOnote3 = $("<div>...wikitext<sup> (?)</sup></div>").css(JAAqqOcss).insertAfter(JAAqqOnote2).hide().hover(function() {
    $(this).css("background-color", "#ffffef");
    $(this).attr('title', 'This button copies this page\'s ' + JAAqqOhowmany + ' results to the clipboard and also adds wikitext markup, like this:\n* [[Article]]\n* [[Article 2]]\nSince this copies only the results shown on this search page, make sure you have clicked the "view 20, 50, 100, 250, 500, 5000" link.');
}, function() {
    $(this).css("background-color", "lightyellow");
});

var JAAqqOnote4 = $("<div>...PetScan URL<sup> (?)</sup></div>").css(JAAqqOcss).insertAfter(JAAqqOnote3).hide().hover(function() {
	$(this).css("background-color", "#ffffef");
	$(this).attr('title', 'This button copies a PetScan URL with this page\'s ' + JAAqqOhowmany + ' results to the clipboard. Note that if the PetScan URL becomes too long (~250+ pages), the URL might not work. For a link with this search query\'s all ' + JAAqqOhowmany2 + ' results, click the button below instead.');
}, function() {
	$(this).css("background-color", "lightyellow");
}); // [[Wikipedia:PetScan]]

var JAAqqOnote5 = $("<div>...or this search query's total results of " + JAAqqOhowmany2 + "<sup> (?)</sup> as...</div>").css({
	"text-align": "center",
	"font-weight": "bold",
	"width": "30%",
	"padding": "5px"
}).insertAfter(JAAqqOnote4).hide().hover(function() {
	$(this).attr('title', 'Note that the actual limit is 10,000, which is MediaWiki\'s maximum limit.');
});

var JAAqqOnote6 = $("<div>...PetScan URL<sup> (?)</sup></div>").css(JAAqqOcss).insertAfter(JAAqqOnote5).hide().hover(function() {
	$(this).css("background-color", "#ffffef");
	$(this).attr('title', 'This button copies a PetScan URL with all ' + JAAqqOhowmany2 + ' results to the clipboard. Note that the maximum limit is 10,000 due to MediaWiki\'s restriction. Also, because this PetScan URL\'s default is the article namespace, you will have to change the namespace in PetScan\'s "Page properties" tab for pages in other namespaces.');
}, function() {
	$(this).css("background-color", "lightyellow");
});

var JAAqqOnote7 = $("<div><sup>(?)</sup> = hover over for tooltip details</div>").css({
	"text-align": "right",
	"font-weight": "bold",
	"width": "30%",
	"padding": "5px"
}).insertAfter(JAAqqOnote6).hide();

const JAAqqOfloat = {
    "float": "right",
    "clear": "right"
};

const JAAqqOabsolute = (JAAqqOpercentage) => ({
    "position": "absolute",
    "top": JAAqqOpercentage,
    "right": "0"
});

if (typeof(JAAqqOexistelmnt) !== 'undefined' && JAAqqOexistelmnt !== null) {
    $(JAAqqOnote1).css(JAAqqOfloat);
    $(JAAqqOnote2).css(JAAqqOfloat);
    $(JAAqqOnote3).css(JAAqqOfloat);
    $(JAAqqOnote4).css(JAAqqOfloat);
    $(JAAqqOnote5).css(JAAqqOfloat);
    $(JAAqqOnote6).css(JAAqqOfloat);
    $(JAAqqOnote7).css(JAAqqOfloat);
} else {
    $(JAAqqOnote1).css(JAAqqOabsolute("30em"));
    $(JAAqqOnote2).css(JAAqqOabsolute("33em"));
    $(JAAqqOnote3).css(JAAqqOabsolute("36em"));
    $(JAAqqOnote4).css(JAAqqOabsolute("39em"));
    $(JAAqqOnote5).css(JAAqqOabsolute("42em"));
    $(JAAqqOnote6).css(JAAqqOabsolute("45em"));
    $(JAAqqOnote7).css(JAAqqOabsolute("48em"));
}

function JAAqqOcopy(element) {
	var $temp = $("<textarea>");
	$("body").append($temp);
	var html = $(element).html();
	const reg1 = /" data-serp-pos="\d+">.*?<\/a>    <\/div>/g;
	html = html.replace(reg1, "\r\n");
	const reg2 = /" data-serp-pos="\d+"><span class="searchmatch">.*<\/span>.*?<\/a>    <\/div>/g;
	html = html.replace(reg2, "\r\n");
	const reg3 = /<div class="mw-search-result-heading"><a href="\/wiki\/.*?" title="/g;
	html = html.replace(reg3, "");
	console.log(html);
	$temp.val(html).select();
	document.execCommand("copy");
	$temp.remove();
}

var JAAqqOlanguage = mw.config.get( 'wgContentLanguage' );

function JAAqqOcopy2(element) {
	var $temp = $("<textarea>");
	$("body").append($temp);
	var html = $(element).html();
	const reg1 = /" data-serp-pos="\d+">.*?<\/a>    <\/div>/g;
	html = html.replace(reg1, "]]\r\n* [[");
	const reg2 = /" data-serp-pos="\d+"><span class="searchmatch">.*<\/span>.*?<\/a>    <\/div>/g;
	html = html.replace(reg2, "]]\r\n* [[");
	const reg3 = /<div class="mw-search-result-heading"><a href="\/wiki\/.*?" title="/g;
	html = html.replace(reg3, "");
	html = html.replace(/^/, "This is a list of [" + window.location.href + " these search results] \(made with the script [[" + JAAqqOlanguage + "User:JAAqqO/EnhancedSearchPage|EnhancedSearchPage]]):\r\n* [[");
	html = html.replace(/\[enUser:/, "[User:");
	html = html.replace(/\[\[.{2,}User:/, "[[:en:User:");
//	html = html.replace(/^/, "This is a list of [" + window.location.href + " these search results] \(made with the script {\{subst:#ifexist: User:JAAqqO/EnhancedSearchPage | [[User:JAAqqO/EnhancedSearchPage|EnhancedSearchPage]] | [[:en:User:JAAqqO/EnhancedSearchPage|EnhancedSearchPage]] }}):\r\n* [[");
	const reg4 = /\[\[(Category|File|Luokka|Tiedosto):/g;
	html = html.replace(reg4, "[[:$1:");
	html = html.replace(/....$/, "");
	console.log(html);
	$temp.val(html).select();
	document.execCommand("copy");
	$temp.remove();
}

function JAAqqOcopy3(element) {
	var $temp = $("<textarea>");
	$("body").append($temp);
	var html = $(element).html();
	const reg1 = /" data-serp-pos="\d+">.*?<\/a>    <\/div>/g;
	html = html.replace(reg1, "%0A");
	const reg2 = /" data-serp-pos="\d+"><span class="searchmatch">.*<\/span>.*?<\/a>    <\/div>/g;
	html = html.replace(reg2, "%0A");
	const reg3 = /<div class="mw-search-result-heading"><a href="\/wiki\/.*?" title="/g;
	html = html.replace(reg3, "");
	html = html.replace(/ /g, "%20");
	html = html.replace(/&/g, "%26");
	html = html.replace(/^/, "https://petscan.wmflabs.org/?active_tab=tab_other_sources&manual_list=");
	html = html.replace(/%0A$/, "&manual_list_wiki=" + JAAqqOlanguage + "wiki&project=wikipedia&language=" + JAAqqOlanguage + "&doit=");
	console.log(html);
	$temp.val(html).select();
	document.execCommand("copy");
	$temp.remove();
}

function JAAqqOcopy4() {
	var $temp = $("<textarea>");
	$("body").append($temp);
	var value = document.getElementById("ooui-php-1").value;
	value = value.replace(/ /g, "%20");
	value = value.replace(/^/, "https://petscan.wmflabs.org/?active_tab=tab_other_sources&ns%5B0%5D=1&search_query=");
	value = value.replace(/$/, "&search_wiki=" + JAAqqOlanguage + "wiki&search_max_results=10000&project=wikipedia&language=" + JAAqqOlanguage + "&doit=");
	console.log(value);
	$temp.val(value).select();
	document.execCommand("copy");
	$temp.remove();
}

var JAAqqOend = $(JAAqqOhide).click(function() {
	$(JAAqqOplace).slideDown();
	$(".searchalttitle").remove();
	$(JAAqqOhide).css({
		"fontSize": "10px",
		"overflow": "auto",
		"resize": "vertical"
	});
	$(JAAqqOtitle).remove();
	$(JAAqqOnote1).show();
	$(JAAqqOnote2).show();
	$(JAAqqOnote3).show();
	$(JAAqqOnote4).show();
	$(JAAqqOnote5).show();
	$(JAAqqOnote6).show();
	$(JAAqqOnote7).show();
});

function JAAqqOcopied(event) {
    var posX = event.pageX;
    var posY = event.pageY;

    var $copiedMessage = $("<div class='copied-message'>Copied!</div>").appendTo("body");
    $copiedMessage.css({
        position: "absolute",
        top: posY + 10 + "px",
        left: posX + 10 + "px",
        backgroundColor: "#f0f0f0",
        border: "1px solid grey",
        padding: "5px 10px",
        borderRadius: "5px",
    });

    setTimeout(function() {
        $copiedMessage.fadeOut("slow", function() {
            $(this).remove();
        });
    }, 500); // duration in milliseconds
}

$(JAAqqOnote2).click(function(event) {
    $(JAAqqOcopy(JAAqqOplace));
    JAAqqOcopied(event);
    $(this).css("transform", "scaleY(0.9)").animate({
        opacity: '0.5',
    }, "fast", function() {
        $(this).animate({
            opacity: '1',
        }, "slow").css("transform", "scaleY(1)");
    });
});

$(JAAqqOnote3).click(function(event) {
    $(JAAqqOcopy2(JAAqqOplace));
    JAAqqOcopied(event);
    $(this).css("transform", "scaleY(0.9)").animate({
        opacity: '0.5',
    }, "fast", function() {
        $(this).animate({
            opacity: '1',
        }, "slow").css("transform", "scaleY(1)");
    });
});

$(JAAqqOnote4).click(function(event) {
    $(JAAqqOcopy3(JAAqqOplace));
    JAAqqOcopied(event);
    $(this).css("transform", "scaleY(0.9)").animate({
        opacity: '0.5',
    }, "fast", function() {
        $(this).animate({
            opacity: '1',
        }, "slow").css("transform", "scaleY(1)");
    });
});

$(JAAqqOnote6).click(function(event) {
    $(JAAqqOcopy4);
    JAAqqOcopied(event);
    $(this).css("transform", "scaleY(0.9)").animate({
        opacity: '0.5',
    }, "fast", function() {
        $(this).animate({
            opacity: '1',
        }, "slow").css("transform", "scaleY(1)");
    });
});
//---------------------- ENDS: this adds a shortened results list and four copy buttons
//
//
//
//
//---------------------- BEGINS: this adds buttons to scroll up and down
var JAAqqOpagedown = $("<div><br /><br />[ ↓ ]</div>").css({
	"fontSize": "20px",
	"color": "#447ff5",
	"position": "absolute",
	"right": "40%",
	"font-weight": "bold",
	"cursor": "pointer"
}).hover(function() {
	$(this).css("color", "#36c");
	$(this).attr('title', 'Go to the bottom of the page');
}, function() {
	$(this).css("color", "#447ff5");
}).insertBefore('.mw-search-result:eq(0)');

var JAAqqOpageup = $("<div>[ ↑ ]</div>").css({
	"fontSize": "20px",
	"color": "#447ff5",
	"position": "absolute",
	"right": "40%",
	"font-weight": "bold",
	"cursor": "pointer"
}).hover(function() {
	$(this).css("color", "#36c");
	$(this).attr('title', 'Go to the top of the page');
}, function() {
	$(this).css("color", "#447ff5");
}).insertAfter('.mw-search-result:eq(-1)');

$(JAAqqOpagedown).click(function(event) {
	event.preventDefault();
	var JAAqqOheight = $(document).height();
	$('html, body').animate({scrollTop:JAAqqOheight}, 500);
});

$(JAAqqOpageup).click(function(event) {
	$('html, body').animate({scrollTop:0}, 500);
});
//---------------------- ENDS: this adds buttons to scroll up and down
//
//
//
//
//---------------------- BEGINS: this adds "x shown on the page" next to the results info
$("<span>&nbsp;&nbsp;&nbsp;(<b>" + JAAqqOhowmany + "</b> shown on the page)</span>").appendTo('.results-info');
//---------------------- ENDS: this adds "x shown on the page" next to the results info
//
//
//
//
//---------------------- BEGINS: this adds/brings back the red color and slighlty bigger font for the "No results found for/Did you mean" text
var JAAqqOdidyou = document.querySelector('.searchdidyoumean');
JAAqqOdidyou.style.color = '#b32424';
JAAqqOdidyou.style.fontSize = '20px';
//---------------------- ENDS: this adds/brings back the red color and slighlty bigger font for the "No results found for/Did you mean" text