User:Bargioni/IDs target blank.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.
// add terget="_blank" to any external identifiers

function add_target() {
	$('.wb-external-id.external').attr('target','_blank');
}

$( function($) {
	// check if we're viewing an item
	let qid = mw.config.get( 'wbEntityId' );
	if ( !qid ) {
		return;
	}

	var retries = 0;
	var max_retries = 20;

	function manage_identifiers(){
		var ids = $('.wikibase-statementgrouplistview>div').get(2); // the block containing identifiers
		var div_ids = $(ids).find('>div'); // the divs of each identifier
		if (div_ids.length > 0) {
			// now we have at least one identifier
			// console.log('Found '+div_ids.length+' identifiers'); // test
			add_target();
		}
		else {
			// console.log('waiting for identifiers... try again'); // test
			if (retries++ < max_retries) setTimeout(manage_identifiers, 100);
		}
	}
	
	if ($('#identifiers').length) {
		// we have identifiers, but we have to wait for them
		setTimeout(manage_identifiers, 100);
	}
});