User:TiagoLubiana/navigation-scholia.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.
// Adapted from https://www.wikidata.org/wiki/User:Lagewi/navigation-scholia.js

function getItem () {
	if (location.pathname.startsWith('/wiki/Q')) {
		return location.pathname.slice(6);
	}
}

function createLinkItem (id, label, url, description) {
	const li = document.createElement('li');
	li.setAttribute('id', id);
	const a = document.createElement('a');
	a.setAttribute('href', url);
	a.setAttribute('title', description);
	a.innerText = label;
	li.append(a);
	return li;
}

function customLinks (list) {
	const item = getItem();
	
	if (item) {
		list.append(createLinkItem(
			't-custom-subclasses',
			'List subclasses',
			'https://query.wikidata.org/embed.html#SELECT%2aWHERE%7B%3Fo%20wdt%3AP279*%20wd%3A' +
			item + '.%3Fo%20rdfs%3Alabel%20%3Fl.FILTER%28LANG%28%3Fl%29%3D%27en%27%29%7D',
			'List subclasses of an item'
		));
		list.append(createLinkItem(
			't-custom-instances',
			'List instances',
			'https://query.wikidata.org/embed.html#SELECT%20DISTINCT%0A%3Fp%20%3FidLabel%0A(GROUP_CONCAT(%20DISTINCT%20%3FtopicLabel%3B%20SEPARATOR%20%3D%20%22%2C%20%22)%20as%20%3Ftopics)%0AWHERE%20%7B%0A%20%20%0A%20%20VALUES%20%3Ftopics_of_interest%20%7B%20wd%3A' + item +'%7D%20.%0A%20%20%0A%20%20%3Ftopic%20wdt%3AP279%3F%20%3Ftopics_of_interest%20.%20%20%23%200%20or%201%20P279%0A%20%20%0A%20%20%3Fp%20wdt%3AP2302%20wd%3AQ21503250%20.%0A%20%20%3Fp%20p%3AP2302%20%3Fst%20.%0A%20%20%3Fst%20ps%3AP2302%20wd%3AQ21503250%20.%0A%20%20%3Fst%20pq%3AP2308%20%3Ftopic%20.%0A%20%20%3Fp%20rdfs%3Alabel%20%3FidLabel%20.%0A%20%20FILTER%20(Lang(%3FidLabel)%20%3D%20%22en%22)%0A%20%20%0A%20%20%3Ftopic%20rdfs%3Alabel%20%3FtopicLabel%20.%0A%20%20FILTER%20(Lang(%3FtopicLabel)%20%3D%20%22en%22)%0A%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20skos%3AaltLabel%20%3FidAltLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FidAltLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20wdt%3AP2378%20%3Fissuer%20.%0A%20%20%20%20%3Fissuer%20rdfs%3Alabel%20%3FissuerLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FissuerLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20wdt%3AP1629%20%3Fidq%20.%0A%20%20%20%20%3Fp%20rdfs%3Alabel%20%3FidqLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FidqLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%7D%0AGROUP%20BY%20%3Fp%20%3FidLabel%0AORDER%20BY%20%3Fp',
			'List instances of an item'
		));
		list.append(createLinkItem(
			't-custom-scholia',
			'Scholia page',
			'https://scholia.toolforge.org/' + item,
			'Go to the Scholia page of this item'
		));
		list.append(createLinkItem(
			't-custom-author-disambiguator',
			'Author Disambiguator page',
			'https://author-disambiguator.toolforge.org/work_item_oauth.php?id=' + item + '&batch_id=&match=1&author_list_id=&doit=Get+author+links+for+work',
			'Go to the AuthorDisambiguator article page of this item'
		));
		list.append(createLinkItem(
			't-custom-property-for-type',
			'Properties used for this type',
			'https://query.wikidata.org/embed.html#SELECT%20DISTINCT%0A%3Fp%20%3FidLabel%0A(GROUP_CONCAT(%20DISTINCT%20%3FtopicLabel%3B%20SEPARATOR%20%3D%20%22%2C%20%22)%20as%20%3Ftopics)%0AWHERE%20%7B%0A%20%20%0A%20%20VALUES%20%3Ftopics_of_interest%20%7Bwd%3A' + item + '%7D%20.%0A%20%20%0A%20%20%3Ftopic%20wdt%3AP279%3F%20%3Ftopics_of_interest%20.%20%20%23%200%20or%201%20P279%0A%20%20%0A%20%20%3Fp%20wdt%3AP2302%20wd%3AQ21503250%20.%0A%20%20%3Fp%20p%3AP2302%20%3Fst%20.%0A%20%20%3Fst%20ps%3AP2302%20wd%3AQ21503250%20.%0A%20%20%3Fst%20pq%3AP2308%20%3Ftopic%20.%0A%20%20%3Fp%20rdfs%3Alabel%20%3FidLabel%20.%0A%20%20FILTER%20(Lang(%3FidLabel)%20%3D%20%22en%22)%0A%20%20%0A%20%20%3Ftopic%20rdfs%3Alabel%20%3FtopicLabel%20.%0A%20%20FILTER%20(Lang(%3FtopicLabel)%20%3D%20%22en%22)%0A%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20skos%3AaltLabel%20%3FidAltLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FidAltLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20wdt%3AP2378%20%3Fissuer%20.%0A%20%20%20%20%3Fissuer%20rdfs%3Alabel%20%3FissuerLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FissuerLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fp%20wdt%3AP1629%20%3Fidq%20.%0A%20%20%20%20%3Fp%20rdfs%3Alabel%20%3FidqLabel%20.%0A%20%20%20%20FILTER%20(Lang(%3FidqLabel)%20%3D%20%22en%22)%0A%20%20%7D%0A%7D%0AGROUP%20BY%20%3Fp%20%3FidLabel%0AORDER%20BY%20%3Fp',
			'List properties used for this type'
		));
	}
}

function customPortal () {
	const sidePanel = document.getElementById('mw-panel');
	if(!sidePanel) return;
	const nav = document.createElement('nav');
	nav.setAttribute('id', 'p-custom');
	nav.setAttribute('class', 'mw-portlet mw-portlet-tb vector-menu vector-menu-portal portal');
	nav.setAttribute('aria-labelledby', 'p-custom-label');
	nav.setAttribute('role', 'navigation');
	const h3 = document.createElement('h3');
	h3.setAttribute('id', 'p-custom-label');
	h3.innerHTML = '<span>Custom tools<span>';
	nav.append(h3);
	const div = document.createElement('div');
	div.setAttribute('class', 'vector-menu-content');
	const ul = document.createElement('ul');
	ul.setAttribute('class', 'vector-menu-content-list');
	div.append(ul);
	nav.append(div);
	customLinks(ul);
	sidePanel.append(nav);
}

customPortal();