User:Lagewi/references.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.
(function () {
	if (!mw.config.exists('wbEntityId')	|| mw.config.get('wgCanonicalNamespace') !== '') {
		return;
	}
	
	const labels = {};
	const references = [];
	$('.wikibase-referenceview').each(function (i, reference) {
		const $statements = $(reference).find('.wikibase-snakview');
		const $statedAs = $statements.has('.wikibase-snakview-property [title="Property:P248"]');
		const $referenceUrl = $statements.has('.wikibase-snakview-property [title="Property:P854"]');
		if ($statedAs.length) {
			const id = 'https://www.wikidata.org/entity/'   $statedAs.find('.wikibase-snakview-value a[title]').attr('title');
			const label = $statedAs.find('.wikibase-snakview-value a[title]').text();
			if (!labels[id]) {
				labels[id] = label;
				references.push(id);
			}
		} else if ($referenceUrl.length) {
			const url = $referenceUrl.find('.wikibase-snakview-value a').attr('href');
			if (!labels[url]) {
				labels[url] = url;
				references.push(url);
			}
		}
	});

	const $toc = $('#toc');
	const $container = $(document.createElement('div'));
	$container.css({
		float: 'left',
    	'margin-bottom': '1em'
	});
	$toc.after($container);
	
	const $summary = $(document.createElement('div'));
	$summary.text('Other resources');
	$container.append($summary);
	
	const $subject = $(document.createElement('div'));
	const $ul = $(document.createElement('ul'));
	for (let i = 0; i < references.length; i  ) {
		const $li = $(document.createElement('li'));
		const $a = $(document.createElement('a'));
		$a.attr('href', references[i]);
		$a.text(labels[references[i]]);
		$li.append($a);
		$ul.append($li);
	}
	$subject.append($ul);
	$subject.hide();
	$container.append($subject);
	
	$summary.toggler({ $subject, visible: false });
})();