User:Erutuon/scripts/scriptTitles.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Adds script classes to the top headers of pages in the main and Talk namespaces. This can make the titles display with better fonts and fix text direction issues. For instance, it makes بند «پ» display correctly, as بند «پ».

To try this script, add importScript("User:Erutuon/scriptTitles.js"); to your common.js.


/* Script-tags titles in the main and Talk namespaces in a language-agnostic
 * way.
 * Uses [[User:Erutuon/scripts/scriptRecognition.js]] for script detection.
*/
/* jshint eqeqeq: true */
/* globals mw, $ */

// <nowiki>

'use strict';

$(function () {
	const namespace = mw.config.values.wgCanonicalNamespace;
	
	if ( ![ '', 'Talk' ].includes(namespace) && mw.config.values.wgAction === 'view' ) {
		console.log('No script class added; not mainspace: ' + namespace + '.');
		return undefined;
	}
	
	let firstHeading = document.getElementById('firstHeading');
	let headerText = firstHeading && firstHeading.firstChild;
	if ( !headerText ) {
		return;
	}
	// This has to be a text node.
	// Latin script doesn't need tagging.
	else if ( headerText.nodeType !== 3 ) {
		// Accommodate span tags created by editable title gadget.
		if ( headerText.nodeName === 'SPAN' ) {
			if ( headerText.classList.length > 0 ) {
				var scriptClassRegex = /^[A-Z][a-z]{3}|polytonic|Latinx|Ruminumerals|musical$/;
				for ( const className of headerText.classList ) {
					if ( scriptClassRegex.test(className) ) {
						return;
					}
				}
			}
			headerText = headerText.firstChild;
		} else {
			return;
		}
	}
	
	if ( !/[\u0370-\uFFFF]/.test(headerText.nodeValue) ) {
		return;
	}
	
	// from https://api.jquery.com/jQuery.getScript
	$.cachedScript = function (url, options) {
			options = $.extend(options || {}, {
				dataType: "script",
				cache: true,
				url: url
			});
		 
			return $.ajax(options);
		};
	
	$.cachedScript(
		'//en.wiktionary.org/w/index.php?title=User:Erutuon/scripts/scriptRecognition.js&action=raw&ctype=text/javascript')
		.done(function () {
			try {
				let newNodeText = namespace === 'Talk' ?
					mw.config.values.wgPageName.match(/^Talk:(.+)$/)[1] :
					mw.config.values.wgPageName;
				
				newNodeText = newNodeText.replace(/_/g, ' ');
				
				const script = window.getScript(newNodeText);
				if (script) {
					headerText.nodeValue = namespace === 'Talk' ? 'Talk:' : '';
					const newNode = document.createElement("span");
					newNode.className = script;
					newNode.innerHTML = newNodeText;
					var parent = headerText.parentNode;
					if (!parent)
						console.log(headerText, firstHeading);
					parent.insertBefore(newNode, headerText.nextSibling);
					console.log(`Added script class ${script} to header ${mw.config.values.wgPageName}.`);
				} else {
					console.log(`No script detected for ${mw.config.values.wgPageName}.`);
				}
			} catch (error) {
				console.log("Error in adding script tagging to title: ", error);
			}
		});
});

// </nowiki>