User:Nthep/googletitle.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.
//Add Google search - modified from User:Writ Keeper/Scripts/googleTitle.js to include articles in Draft namespace//
$(googleTitle);
 
function googleTitle()
{
		if(mw.config.get('wgCanonicalNamespace') === ""||mw.config.get('wgCanonicalNamespace') =='Draft')
		{
			var subjectName;
			if(mw.config.get('wgPageName').charAt(mw.config.get('wgPageName').length - 1) == ")")
			{
				subjectName = mw.config.get('wgPageName').substring(0, mw.config.get('wgPageName').lastIndexOf("(") - 1);
			}
			else
			{
				subjectName = mw.config.get('wgPageName');
			}
			if(mw.config.get('wgCanonicalNamespace') =='Draft') // this if statement strips Draft: from the start of the search string //
			{
				subjectName = mw.config.get('wgPageName').substring(6);
			}
			var searchNode = document.createElement("a");
			searchNode.href = "https://www.google.com/search?q=" + encodeURIComponent(subjectName.replace(/_/g, " ")) + "+-wikipedia.org";
			searchNode.target = "_blank";
			searchNode.innerHTML = "<span style='font-size:x-small;'> Search Google</span>";
			document.getElementById("firstHeading").appendChild(searchNode);
		}
}