User:Titodutta/scripts/SearchHelper.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.
/*
The script adds a few options beside article
Main script by Writ and may be seen at https://en.wikipedia.org/wiki/User:Writ_Keeper/Scripts/googleTitle.js
Script improved by User:SD0001, see diff https://en.wikipedia.org/wiki/Special:ComparePages?page1=User%3ATitodutta%2Fscripts%2FSearchHelper.js&rev1=&page2=User%3ASD0001%2FSearchHelper.js&rev2=&action=&diffonly=&unhide=&diffmode=source

Attribution: Code rewritten by Mr. Stradivarius on 8 December 2021 to fix cross-site scripting vulnerability in the Wikidata link, see details here https://en.wikipedia.org/w/index.php?title=User%3ATitodutta%2Fscripts%2FSearchHelper.js&type=revision&diff=1059282493&oldid=1053495203
*/

$(document).ready(function()
{
	function getSubject()
	{
		let pageName = mw.config.get("wgTitle");
		if (pageName.charAt(pageName.length - 1) == ")")
		{
			return pageName.substring(0, pageName.lastIndexOf("(") - 1);
		}
		else
		{
			return pageName;
		}
	}
	
	function escapeSubject(subject)
	{
		return encodeURIComponent(subject.replace(/_/g, " "))
	}
	
	function createLink(url, display)
	{
		return $("<a>")
			.attr("href", url)
			.append($("<span>")
				.css("font-size", "x-small")
				.text(display)
			);
	}

	function appendLinks($node, links)
	{
		$.each(
			links,
			function (index, $link)
			{
				$node.append(document.createTextNode(" "), $link);
			}
		);
	}
	
	$firstHeading = $("#firstHeading");
	
	if(mw.config.get("wgCanonicalNamespace") == "" || mw.config.get("wgCanonicalNamespace") == "Draft")
	{
		let subject = getSubject();
		let links = [
			/*WRS from User:Sam Sailor/Scripts/WRStitle.js*/
			createLink(
				"https://www.google.com/custom?hl=en&cx=007734830908295939403%3Agalkqgoksq0&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + escapeSubject(subject),
				"WRS"
			),
			createLink(
				"http://www.google.com/search?q=" + escapeSubject(subject) + "+-wikipedia.org",
				"Google search"
			),
			createLink(
				"https://news.google.com/search?q=" + escapeSubject(subject) + "+-wikipedia.org",
				"Google News"
			),
			createLink(
				"https://www.google.com/search?tbm=bks&q=" + escapeSubject(subject) + "+-wikipedia.org",
				"Google Books"
			),
			createLink(
				"https://www.google.com/search?tbm=isch&q=" + escapeSubject(subject),
				"Google images"
			),
			createLink(
				"https://search.yahoo.com/search?p=" + escapeSubject(subject),
				"Yahoo"
			),
			createLink(
				"https://duckduckgo.com/?q=" + escapeSubject(subject),
				"DuckDuckGo"
			),
			createLink(
				"https://www.youtube.com/results?search_query=" + escapeSubject(subject),
				"YouTube"
			),
			createLink(
				"https://commons.wikimedia.org/w/index.php?search=" + escapeSubject(subject),
				"Commons"
			),
			createLink(
				"http://www.flickr.com/search/?l=commderiv&q=" + escapeSubject(subject),
				"Flickr (CC)"
			),
			createLink(
				"https://www.imdb.com/find?q=" + escapeSubject(subject),
				"IMDb"
			),
			createLink(
				"https://www.jstor.org/action/doBasicSearch?Query=" + escapeSubject(subject),
				"JSTOR"
			),
			createLink(
				"http://edwardbetts.com/find_link/" + escapeSubject(subject),
				"Find link"
			),
			createLink(
				"https://www.google.com/custom?hl=en&cx=014196815420355252912:fownxmjrhle&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + escapeSubject(subject),
				"Indian newspapers"
			)
		];
		appendLinks( $firstHeading, links );
	}

	if (mw.config.get("wgCanonicalNamespace") == "" && $('#t-wikibase').length )
	{
		let link = createLink(
			$('li#t-wikibase a:first').attr('href'),
			"Wikidata"
		);
		appendLinks( $firstHeading, [link] );
	}

});