Jump to content

User:Awesome Aasim/customsearch.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.
window.customsearch = function(engineurl, enginename) {
	engineurl = engineurl ? engineurl : "https://duckduckgo.com/";
	enginename = enginename ? enginename : "DuckDuckGo";
	var i18n = {
		"en": {
			searchdialog: "Choose how you want to search $1."
		}
	};
	$(document).ready(function(e) {
		$("#mw-searchButton").hide();
		$("#searchButton").off("click");
		$("#searchButton").attr("type", "button");
		function customSearch() {
			$('<div id="searchdialogbox"></div>').text(i18n[mw.config.get("wgUserLanguage")].searchdialog ? i18n[mw.config.get("wgUserLanguage")].searchdialog.replace("$1", mw.config.get("wgSiteName")).replace("$2", enginename) : i18n["en"].searchdialog.replace("$1", mw.config.get("wgSiteName")).replace("$2", enginename)).dialog({
				buttons:  [
					{
						text: enginename,
						click: function() {
							window.location.href = engineurl + "?q=site:" + window.location.host + " " + $("#searchInput").val();
						}
					},
					{
						text: mw.config.get("wgSiteName"),
						click: function() {
							window.location.href = mw.config.get("wgScriptPath") + "/index.php?search=" + $("#searchInput").val();
						}
					}
				],
				modal: true
			});
		}
		$("#searchButton").click(customSearch);
		$("#searchInput").off("keydown");
		$("#searchInput").off("keypress");
		$("#searchInput").on("keypress", function(e) {
			if (e.which == 13) {
				e.preventDefault();
				customSearch();
			}
		});
	});
}