User:Nikki/ShowIDs.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 adds entity IDs in brackets after links.
 * 
 * To use it, add the following line to your common.js:
 * mw.loader.load("//www.wikidata.org/w/index.php?title=User:Nikki/ShowIDs.js&action=raw&ctype=text/javascript");
 * 
 * @license CC0-1.0
*/

(function () {
	"use strict";

	function showIDs () {
		mw.util.addCSS(".idlink { unicode-bidi: isolate; }");

		// :not(.anchorlink) to prevent it from adding IDs before the anchors
		// for senses and forms that AnchorLinks.js inserts
		// and for linked characters in lemmas from LexemeLinkCharacters.js
		$(".wikibase-entityview-main a:not(.anchorlink, .linkedcharacter)").each(function () {
			var m = this.href.match(/\/wiki\/(?:.*:)?([PQL][0-9]+(#[SF][0-9]+)?)$/);
			if (m && m[1]) {
				if (!$(this).parent().find(".idlink")[0])
					$(this).after(" <small class=\"idlink\" dir=\"auto\">("+m[1].replace("#", "-")+")</small>");
			}
		});
	}

	mw.hook('wikibase.entityPage.entityView.rendered').add(showIDs);
	mw.hook('wikibase.statement.saved').add(showIDs);
	mw.hook("userjs-showids").add(showIDs);
})();