Misplaced Pages

User:Titodutta/scripts/SearchHelper.js: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Titodutta Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 07:34, 4 November 2021 view sourceTitodutta (talk | contribs)Autopatrolled, Administrators156,709 edits +← Previous edit Revision as of 15:09, 8 December 2021 view source Mr. Stradivarius (talk | contribs)Edit filter managers, Administrators59,192 edits fix cross-site scripting vulnerability in the Wikidata link; I also changed all of the links to use JQuery, as that is less risky than assembling them directly with HTML, which basically makes this a rewriteNext edit →
Line 3: Line 3:
Main script by Writ and may be seen at https://en.wikipedia.org/User:Writ_Keeper/Scripts/googleTitle.js Main script by Writ and may be seen at https://en.wikipedia.org/User:Writ_Keeper/Scripts/googleTitle.js
Script improved by User:SD0001, see diff https://en.wikipedia.org/Special:ComparePages?page1=User%3ATitodutta%2Fscripts%2FSearchHelper.js&rev1=&page2=User%3ASD0001%2FSearchHelper.js&rev2=&action=&diffonly=&unhide=&diffmode=source Script improved by User:SD0001, see diff https://en.wikipedia.org/Special:ComparePages?page1=User%3ATitodutta%2Fscripts%2FSearchHelper.js&rev1=&page2=User%3ASD0001%2FSearchHelper.js&rev2=&action=&diffonly=&unhide=&diffmode=source
*/
*/$(document).ready(function()

$(document).ready(function()
{ {
function getSubject()
if(mw.config.get("wgCanonicalNamespace") == "" || mw.config.get("wgCanonicalNamespace") == "Draft")
{
let pageName = mw.config.get("wgTitle");
if (pageName.charAt(pageName.length - 1) == ")")
{ {
return pageName.substring(0, pageName.lastIndexOf("(") - 1);
var subjectName;
}
var pageName = mw.config.get("wgTitle");
else
if(pageName.charAt(pageName.length - 1) == ")")
{
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);
subjectName = pageName.substring(0, pageName.lastIndexOf("(") - 1);
}
else
{
subjectName = pageName;
} }
);
}
$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/search/?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 )
var newNode = " <a href='https://www.google.com/custom?hl=en&cx=007734830908295939403%3Agalkqgoksq0&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>WRS</span></a>"; /*WRS from User:Sam Sailor/Scripts/WRStitle.js*/
{
$("#firstHeading").append(newNode);
let link = createLink(
$('li#t-wikibase a:first').attr('href'),
"Wikidata"
);
appendLinks( $firstHeading, );
}


var newNode = " <a href='http://www.google.com/search?q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "+-wikipedia.org' target='_blank'><span style='font-size:x-small;'>Google search</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://news.google.com/search?q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "+-wikipedia.org' target='_blank'><span style='font-size:x-small;'>Google News</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.google.com/search?tbm=bks&q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "+-wikipedia.org' target='_blank'><span style='font-size:x-small;'>Google Books</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Google images</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://search.yahoo.com/search?p=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Yahoo</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://duckduckgo.com/?q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>DuckDuckGo</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.youtube.com/results?search_query=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>YouTube</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://commons.wikimedia.org/search/?search=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Commons</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='http://www.flickr.com/search/?l=commderiv&q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Flickr (CC)</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.imdb.com/find?q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>IMDb</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.jstor.org/action/doBasicSearch?Query=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>JSTOR</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='http://edwardbetts.com/find_link/" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Find link</span></a>"
$("#firstHeading").append(newNode);

var newNode = " <a href='https://www.google.com/custom?hl=en&cx=014196815420355252912:fownxmjrhle&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=" + encodeURIComponent(subjectName.replace(/_/g, " ")).replace(/'/g,"%27") + "' target='_blank'><span style='font-size:x-small;'>Indian newspapers</span></a>";
$("#firstHeading").append(newNode);

}
});
if ( $('#t-wikibase').length ){
console.log('Exist');
link = $('li#t-wikibase a:first').attr('href');
console.log( link );
}

$(document).ready(function()
{
if(mw.config.get("wgCanonicalNamespace") == "" && $('#t-wikibase').length )
{
console.log('Exist');
link = $('li#t-wikibase a:first').attr('href');

var newNode = "<a href='" + link + "' target='_blank'><span style='font-size:x-small;'> Wikidata</span></a>";
$("#firstHeading").append(newNode);
}
}); });

Revision as of 15:09, 8 December 2021

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

$(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/search/?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,  );
	}

});
User:Titodutta/scripts/SearchHelper.js: Difference between revisions Add topic