User:So9q/DepictsThisSense.js

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 a link to WCQS under P5137 statements on senses

Author: Dennis Priskorn 2021

Pseudo code:
First find the ID of the sense
find the P5137 statement and extract the qid
Then show the iframe
*/
$("div.wikibase-lexeme-sense").each(function(){
	var senseId = $(this).attr("id");
	//console.log("DepictsThisSense.js:"+senseId);
	// find S1-P5137 aka item for this sense
	var senseIdP5137 = `#${senseId}-P5137`;
	// pick the first
	var p5137s = $(senseIdP5137).find(".wikibase-statementlistview");
	//console.log("Found p5137s:");
	//console.log(p5137s);
	if ($(p5137s).length === 1) {
		var firstValue = $(p5137s).first()
								  .find("div.wikibase-snakview-value")
								  .first();
		//console.log("Found value:");
		//console.log(firstValue);
		var href = firstValue.find("a").first().attr("href");
		//console.log("Found href:"+href);
		if (href) {
			//console.log("Found href:"+href);
			qid = href.replace("/wiki/", "");
			// show iframe from WCQS
			$(senseIdP5137).append(
				$("<a>").text("View files in Wikimedia Commons that depicts this sense")
						.css("grid-column", "span 2")
						.attr("href", 
				'https://wcqs-beta.wmflabs.org/embed.html#%23defaultView%3AImageGrid%0Aselect%20%3Ffile%20%3Fimage%20where%20%7B%0A%20%20%3Ffile%20wdt%3AP180%20wd%3A' + qid + '.%0A%20%20%3Ffile%20schema%3Aurl%20%3Fimage.%0A%7D')
				);
		}
		
	}
});