User:Erutuon/scripts/scriptTitles.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, $ */

//

'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);			}		}); });

//