Jump to content

MediaWiki:Gadget-ShowIDs.js

From Wiktionary, the free dictionary

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • 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.


// <nowiki>
/* jshint maxerr:1048576, strict:true, undef:true, latedef:true, esversion:6 */
/* global $, mw */

/**
 * Display sense and etym IDs.
 * 
 * Note: this gadget also loads ShowIDs-pagestyles, which offers a
 * CSS-only fallback to this gadget that does not require JavaScript.
 * The CSS is nevertheless required for this JavaScript gadget
 * to display correctly.
 * CSS: [[MediaWiki:Gadget-ShowIDs-pagestyles.css]]
 *
 * Author(s): Surjection
 * Last updated: 2024-12-12
 */

$(document).ready(() => {
	// for all senseid and etymid...
    $(".senseid, .etymid").each(function () {
		const e = $(this);
		let templateName;
		// find class
		if (e.hasClass("senseid")) {
			templateName = "senseid";
		} else if (e.hasClass("etymid")) {
			templateName = "etymid";
		} else {
			return;
		}
		
		const displayedClass = `${templateName}-displayed`; 
		if (e.hasClass(displayedClass))
			return;

		const lang = e.attr("lang");
		const id = e.data("id");
		const tooltip = (lang && id) && `{{${templateName}|${lang}|${id}}}`;
		if (e.is("li") || e.text().trim()) {
			// with li or text: create a new span instead.
			const newSpan = $("<span></span>");
			newSpan.addClass(displayedClass);
			newSpan.text(id);
			if (tooltip) newSpan.attr("title", tooltip);
			if (!e.text().match(/^\s/))
				e.prepend(" ");
			newSpan.prependTo(e);
			
		} else {
			e.addClass(displayedClass);
			// copy data-id to text so that it becomes copyable
			e.text(id);
			if (tooltip) e.attr("title", tooltip);
	
			// fix paragraph placement; if the senseid/etymid is the only child of a p,
			// which is immediately followed by another p, then move it to the beginning
			// of that p
			const parent = e.parent("p");
			if (parent.length && parent.children().length === 1) {
				let nextP = parent;
				while ((nextP = nextP.next()).length) {
					if (nextP.css("float") !== "none" || !nextP.is(":visible")) {
						// if floating or not visible, keep searching
						continue;
					}
					
					if (nextP.is("p")) {
						// prepend newline and id to p, remove the original one
						nextP.prepend("\n");
						parent.children().prependTo(nextP);
						parent.remove();
					}
					
					// break if not floating - no candidate found
					break;
				}
			}
		}
	});

	// ensure boxes do not show up twice
	mw.util.addCSS(`.senseid::before, .etymid::before { content: none; display: none; }`);
});

// AGPrefs/logged-out user preferences cannot load peer gadgets, so just in case:
mw.loader.load("ext.gadget.ShowIDs-pagestyles");

// </nowiki>