MediaWiki:Gadget-AnchorLinks.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 small link before property labels, statement values,
 * sense IDs and form IDs on entity pages to provide a clickable/copiable link
 * to that section of the page.
 * 
 * To use it, add the following line to your common.js:
 * mw.loader.load("//www.wikidata.org/w/index.php?title=User:Nikki/AnchorLinks.js&action=raw&ctype=text/javascript");
 * 
 * @license CC0-1.0
*/

(function () {
	"use strict";

	var translations = {
		"en": {
			"userjs-anchorlink-title-form": "Jump to this form",
			"userjs-anchorlink-title-property": "Jump to this property",
			"userjs-anchorlink-title-sense": "Jump to this sense",
			"userjs-anchorlink-title-statement": "Jump to this statement",
		},
		"qqq": {
			"userjs-anchorlink-title-form": "link title when used next to form IDs on lexeme pages",
			"userjs-anchorlink-title-property": "link title when used next to the property label for a group of statements",
			"userjs-anchorlink-title-sense": "link title when used next to sense IDs on lexeme pages",
			"userjs-anchorlink-title-statement": "link title when used next to an individual statement",
		},
		"de": {
			"userjs-anchorlink-title-form": "Zu dieser Form springen",
			"userjs-anchorlink-title-property": "Zu dieser Eigenschaft springen",
			"userjs-anchorlink-title-sense": "Zu dieser Bedeutung springen",
			"userjs-anchorlink-title-statement": "Zu dieser Aussage springen",
		}
	};

	function create_link(id, type) {
		var linktitle = $.i18n("userjs-anchorlink-title-" + type);
		return "<a class='anchorlink' href='#" + id + "' title='" + linktitle + "'><span>›</span></a>";
	}

	function add_links() {
		// Add anchor links for properties
		$(".wikibase-statementgroupview").each(function () {
			var id = this.id;
			$(this).find(".wikibase-statementgroupview-property-label > a[href^='/wiki/Property:']").before(create_link(id, "property"));
		});

		// Add anchor links for properties in diffs
		// (but only if the diff page is displaying an entity)
		if (document.querySelector(".wikibase-entityview")) {
			$(".diff-lineno a[href^='/wiki/Property:']").each(function () {
				var id = this.href.replace(/.*:/, "");
				$(this).before(create_link(id, "property"));
			});
		}

		// Add anchor links for statements
		$(".wikibase-statementview").each(function () {
			var id = this.id;
			$(this).find(".wikibase-statementview-mainsnak .wikibase-snakview-value").before(create_link(id, "statement"));
		});

		// Add anchor links for senses
		$(".wikibase-lexeme-sense").each(function () {
			var id = this.id;
			$(this).find(".wikibase-lexeme-sense-id").before(create_link(id, "sense"));
		});

		// Add anchor links for forms
		$(".wikibase-lexeme-form").each(function () {
			var id = this.id;
			$(this).find(".wikibase-lexeme-form-id").before(create_link(id, "form"));
		});
	}

	$.i18n().load(translations);
	mw.hook("wikibase.entityPage.entityView.rendered").add(add_links);

})();