User:Nikki/SignWriting.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// This script isn't designed for Wikidata, so it breaks horribly when setting the UI to ase.
//mw.loader.load("https://incubator.wikimedia.org/w/index.php?title=MediaWiki:Gadget-Signwriting.js&action=raw&ctype=text/javascript");
// This one converts the text without changing the interface layout instead.
mw.loader.load("https://en.wikipedia.org/w/index.php?title=User:Slevinski/signwriting_viewer.js&action=raw&ctype=text/javascript");

// Prevent text breaking onto a new line after a sign is included
// Also remove left margin so that signs aren't oddly aligned
mw.util.addCSS(".signwritingtext { display: inline-block; margin-left: 0 !important }");

mw.hook('wikibase.entityPage.entityView.rendered').add(function () {
	// If the script has already loaded by the time the page is done rendering, run the function again to fix lexeme forms and text in statements
	if (typeof signwriting_styled !== "undefined")
		signwriting_styled();

	// Apply to labels appearing when clicking "All entered languages"
	$(".wikibase-entitytermsforlanguagelistview-more a").click(function () { signwriting_styled() });
});

// Fix text in statements after saving a statement
mw.hook('wikibase.statement.saved').add(function () {
	signwriting_styled();
});

// Support inline searches
// ul elements are not added to the DOM until they're first needed, but are not removed once added
// so here MOs are used to watch for top-level elements being added
// and also to watch for changes in each ul
function addObserverToList(list) {
	var observer = new MutationObserver(function () {
		signwriting_styled();
	});
	observer.observe(list, { childList: true });
}
var observer = new MutationObserver(function (mutationList) {
	mutationList.forEach(function(mutation) {
		if (mutation.addedNodes) {
			Array.prototype.forEach.call(mutation.addedNodes, function(node) {
				if (node.nodeName === "UL" && node.classList && node.classList.contains("ui-ooMenu")) {
					addObserverToList(node);
				}
			});
		}
	});
});
observer.observe(document.body, { childList: true });