User:Nikki/Ranker.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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* This script adds a link to the Ranker tool next to every property on an entity page.

   To use it, add the following line to your common.js:
   mw.loader.load("//www.wikidata.org/w/index.php?title=User:Nikki/Ranker.js&action=raw&ctype=text/javascript");

   License: CC0
*/
// jshint esversion: 6

(function () {
	"use strict";

	mw.util.addCSS(`
		.ranker {
			float: right;
			font-size: small;
			margin-inline-end: 1em;
		}
		html[dir="rtl"] .ranker {
			/* Chromium doesn't support float: inline-end */
			float: left;
		}
		.ranker a {
			color: #999;
			display: block;
			line-height: 1;
		}
		/* By default, the property label div is only as wide as the text in it.
		   Since the icon is being right-aligned, make the div a constant width. */
		.wikibase-statementgroupview-property-label {
			width: 100%;
		}
	`);

	function linkToRanker (e) {
		$(".wikibase-statementgroupview").each(function () {
			var propid = this.id;
			var eid = e.id;
			if (propid.match(/-/)) { // sense or form ID
				var s = propid.split(/-/);
				propid = s[1];
				eid = eid + "-" + s[0];
			}
				
			var url = "https://ranker.toolforge.org/edit/www.wikidata.org/" + eid + "/" + propid;
			$(this).find(".wikibase-statementgroupview-property-label")
				.prepend(`<div class="ranker"><a href="${url}">▤</a></div>`);
		});
	}

	mw.hook("wikibase.entityPage.entityLoaded").add(linkToRanker);
})();