User:Equazcion/UniversalTransclusionPreviews.js

Source: 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.
if (mw.config.get('wgNamespaceNumber') != 10) {	//It's already in Template space, so do nothing there.

	if ((mw.config.get('wgAction') == "edit") || (mw.config.get('wgAction') == "submit")){ 
		
		var hidden = '';
		
		$("#templatesandbox-editform").remove();	 
		//Determine if we're on a transclusion preview page already. 
		//If so, fill in transclusion preview input line with host page title and unhide preview line
		var prevTitle = $('div.previewnote a').attr('title');
		if (typeof prevTitle == "undefined"){ 
			prevTitle = ''; 
			hidden = ' hidden="hidden"';
		} else {
			hidden = '';
		}
	
		//Code to add:
		var prevCode = '<fieldset id="templatesandbox-editform"' + hidden + '><legend>Preview a page with this page transcluded</legend>' + 
					'<span class="mw-templatesandbox-page" id="wpTemplateSandboxPageLabel">' +
					'<label for="wpTemplateSandboxPage">Page title:</label></span> ' + 
					'<input id="wpTemplateSandboxPage" tabindex="8" size="60" spellcheck="true" name="wpTemplateSandboxPage" ' +
					'value="' + prevTitle + '">' +
					'<input id="wpTemplateSandboxPreview" name="wpTemplateSandboxPreview" tabindex="9" value="Show preview" type="submit">' + 
					'</fieldset>';
		
		//Insert the code under the main edit stuff.
		$('.editOptions').append(prevCode); 
		
		//Insert toggle link
		$('.editButtons').append(' | <a class="transTog" href="#azb">Toggle Transclusion Preview</a>');
	 
		// Set link function
		$('.transTog').click(function(){
			if ($('#templatesandbox-editform').attr('hidden') == "hidden"){
				$('#templatesandbox-editform').prop('hidden', false);
			} else {
				$('#templatesandbox-editform').attr('hidden','');
			}
		});
	
	}
}