User:Bargioni/WikiBridge.js - Wikidata

PHOTO EMBED

Fri Jun 25 2021 15:28:31 GMT+0000 (UTC)

Saved by @admariner

/**

*	WikiBridge - a brilliant idea of user:Gentile64, written by user:Bargioni

*	Add a tabbed section to Wikidata items showing some paragraphs from Wikipedias 

*	chosen by the Babel languages of the logged in user

* 

*   Version2 : I18n

*/



if( typeof(WikiBridge_conf) == "undefined" ) {

    importScript( 'User:Bargioni/WikiBridge_conf.js' );

}



function WikiBridge_source(UL,T) {

	var url = 'https://'+UL+'.wikipedia.org/wiki/'+T;

	return '<span class="wikipedia_logo"> \

	<img src="https://upload.wikimedia.org/wikipedia/commons/7/77/Wikipedia_svg_logo.svg" width="24px" \

	</span>\

	<a target="_blank" href="' + url + '">' + url + '</a>'; 

}



function absolute_links(UL) {

	var L = $('a[rel="mw:WikiLink"]').add('figure a').not('.WikiBridge_link').each(function(i,e) {

		var href = 'https://'+UL+'.wikipedia.org/wiki/'+$(e).attr('href');

		$(e).attr('href',href);

		// $(e).removeClass('mw-redirect');

		$(e).addClass('WikiBridge_link');

		$(e).attr('target','_blank');

	});

	return L.length; // unuseful :-)

}



function WikiBridge_builder() {

	var H = []; // gather the html code of the tabs

	H.push('<div id="WikiBridge_tabs">');

	H.push('<div id="WikiBridge_close"><span id="WikiBridge_name">WikiBridge</span> <a href="#" title="Close the WikiBridge box" onclick="$(&quot;#WikiBridge_tabs&quot;).remove();return false">×</a></div>');

	H.push('<ul>');

	

	if(!wb || !wb.getUserLanguages || !mw.uls || !mw.uls.getFrequentLanguageList) return; // A few cases of this? Did a dependency not yet loaded?

	var user_langs = wb.getUserLanguages();

	

	// create tabs

	for (var i = 0; i < user_langs.length; i++) {

		var user_lang = user_langs[i];

		H.push('<li><a href="#Wiki_Bridge_'+user_lang+'"><span id="Wiki_Bridge_lang_'+user_lang+'">'+user_lang+'</span></a></li>');

	}

	

	// create info tab

	H.push('<li><a href="#Wiki_Bridge_info"><span>&#9432;</span></a></li>'); // &#9432; info icon

	H.push('</ul>');

	// add a div for each tab

	for (i = 0; i < user_langs.length; i++) {

		user_lang = user_langs[i];

		H.push('<div id="Wiki_Bridge_'+user_lang+'"> \

		<span class="wkp_text" id="wkp_text_'+user_lang+'">Qui testo da Wikipedia '+user_lang+'</span></div>');

	}

	

	// add a div for the info tab

	var U = WikiBridge_conf[user_langs[0]];

	var info = U ? U.info : 'missing info';

	if(U && U.en && U.en.info) info = U.en.info;

	H.push('<div id="Wiki_Bridge_info">\

	<img src="https://upload.wikimedia.org/wikipedia/commons/9/96/Idea.svg" width="24px"/> \

	' + info + ' \

	Version 0.3 - Nov 30, 2020.</div>');

	H.push('</div>');

	

	// append the WikiBridge

	$('.wikibase-entitytermsview-entitytermsforlanguagelistview').append(H.join(''));

	

	// activate the WikiBridge

	$('#WikiBridge_tabs').tabs();

	// style some components of the WikiBridge

	$('#WikiBridge_close').css({'float':'right', 'font-size':'12pt', 'position':'relative', 'left':'-10px', 'top':'6px'});

	$('#WikiBridge_name').css({'font-size':'11.2pt'});

	$('#WikiBridge_close').css({'color':'rgb(39, 121, 170)'});

	$('#WikiBridge_tabs').css('font-size','1em');

	

	// get sentences from Wikipedias to fill in the div of each tab

	for (i = 0; i < user_langs.length; i++) {

		user_lang = user_langs[i];

		var wkp_link = $('div[data-wb-sitelinks-group="wikipedia"] .wikibase-sitelinkview-page[lang="'+user_lang+'"] a').attr('href');

		if (wkp_link && wkp_link.indexOf('Category') == -1) {

			var msg = WikiBridge_conf[user_lang] ? WikiBridge_conf[user_lang].loading : WikiBridge_conf.en.loading

			$('#wkp_text_' + user_lang).text(msg);

			var parts = wkp_link.split(/\//);

			var title = parts[parts.length - 1];

			var url = 'https://'+user_lang+'.wikipedia.org/api/rest_v1/page/html/'+title;

			$.get( url, function (R) {

				var U = this.url;

				var parts = U.split(/\//);

				var T = decodeURI(parts[parts.length - 1]);

				var UL = (parts[2].split(/\./))[0];

				var H = $.parseHTML(R);

				var myText = H[15].innerHTML || H[16].innerHTML || '???';

				myText += '<p class="WikiBridge_source">' + WikiBridge_source(UL,T) + '</p>';

				$('#wkp_text_'+UL).html(myText);

				$('.wkp_text table').remove();

				$('.wkp_text .mw-reflink-text').remove();

				$('.wkp_text div').remove();

				$('.WikiBridge_source').css({'background-color':'white'});

				absolute_links(UL);

			})

			.fail(function() {

				var U = this.url;

				var parts = U.split(/\//);

				var UL = (parts[2].split(/\./))[0];

				// $('#wkp_text_'+UL).text('An error occurred while retrieving the Wikipedia page '+this.url);

				var msg = WikiBridge_conf[U] ? WikiBridge_conf[U].error_loading : WikiBridge_conf.en.error_loading;

				$('#wkp_text_'+UL).text(msg + this.url);

				$('#Wiki_Bridge_lang_'+UL).css('color','red');

			});

		}

		else {

			var qm = 'https://upload.wikimedia.org/wikipedia/commons/4/4b/Question_Mark_1.svg';

			qm = '<img src="'+qm+'" width="24px"/>';

			var msg = WikiBridge_conf[user_lang] ? WikiBridge_conf[user_lang].create_new : WikiBridge_conf.en.create_new;

			$('#wkp_text_'+user_lang).html(qm + msg);

			$('#Wiki_Bridge_lang_'+user_lang).css('color','red');

		}

	}

}



function WikiBridge_waitForConfig(func){

    if(typeof WikiBridge_conf !== "undefined"){

		func();

    }

    else{

		setTimeout(WikiBridge_waitForConfig, 100, func);

    }

}



$( function($) {

    WikiBridge_waitForConfig(function() {

		// preference loaded, let's go on

		WikiBridge_builder();

    });

});
content_copyCOPY

https://www.wikidata.org/wiki/User:Bargioni/WikiBridge.js