User:Bargioni/MnM ext2.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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/** add remove and N/A to Mix-n-match gadget
 * Another good idea of user Epìdosis...
 * ... waiting for Magnus Manske's MnM gadget to include it
 * 
 * Version 2, using MutationObserver https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
 */

/*jshint esversion: 6 */

function MnM_extension_addinterface () {
	// console.log('waiting for mixnmatch_gadget...', mw.MnM_extension_retries); // test
	if ( $('#mnm_entries').length === 0 ) {
		// MnM gadget not yet loaded, wait 0.1s and try again
		mw.MnM_extension_retries++;
		if (mw.MnM_extension_retries > mw.MnM_extension_max_retries) {
			console.log('MnM_extension: end of tries, something was not loaded');
			return; // stop trying
		}
		setTimeout(function(){
			MnM_extension_addinterface();
		}, 100);
		return;
	}

	// console.log('mixnmatch_gadget table successfully defined');
	const targetNode = document.getElementById('mnm_entries');
	// Options for the observer (which mutations to observe)
	const config = { attributes: true, childList: true, subtree: true };
	// Callback function to execute when mutations are observed
	const callback = function(mutationsList, observer) {
		// console.log(mutationsList); // test
		for (let mutation of mutationsList) {
			if (mutation.type === 'childList') { // required?
				// console.log('a mutation',mutation.addedNodes); // test
				for (let node of mutation.addedNodes) {
					// console.log('node',node); // node is <tr><td><i>none found</i></td></tr> or <tr id="mnm_row_entry_...">
					if ($(node).prop("tagName") != 'TR') continue;
					if (!$(node).attr('id')) continue;
					var entry = node;
					var mnm_entry_id = $(entry).attr('id').replace(/mnm_row_entry_/,'');
					var cell = $(entry).find('td').get(0);
					if ($(entry).text().indexOf('✓') == -1) {
						$(cell).prepend('<span class="remover" \
							title="remove the automatic match of this entry from MnM" \
							data-mnm_entry_id="'+mnm_entry_id+'">&#8854;</span> '); // ⊖
					}
					else {
						$(cell).css('text-align','right');
					}
					$('.remover').css({'cursor':'pointer', 'font-size':'16pt', 'color':'#0645ad'});
					$('.remover').on('click',function(){
						var mixnmatch_gadget_api = 'https://mix-n-match.toolforge.org/api.php';
						$(this).css('background-color','gray');
						var entry = $(this).attr('data-mnm_entry_id');
						var tusc_user = mw.config.get('wgUserName');
						var query = 'remove_q';
						var params = `?query=${query}&tusc_user=${tusc_user}&entry=${entry}&callback=?`; // JSONP
						// object mixnmatch_gadget is not yet available: getJSON fails - Stefano Bargioni 2022-09-09 TODO
						// memo: https://mix-n-match.toolforge.org/api.php?callback=jQuery361023131491591654707_1662726804172&query=match_q&tusc_user=Bargioni&entry=105131158&q=118097&_=1662726804176
						$.getJSON(mixnmatch_gadget_api + params, function(R){
							// console.log(1,R,entry);
							$('.remover[data-mnm_entry_id='+entry+']')
								.css('background-color','')
								.off('click')
								.removeClass('remover')
								.addClass('not_available')
								.attr('title','set this entry as N/A on MnM')
								.html('&otimes;') // ⊗
								.on('click',function(){
									$(this).css('background-color','gray');
									var entry = $(this).attr('data-mnm_entry_id');
									var tusc_user = mw.config.get('wgUserName');
									var query = 'match_q';
									var params = `?query=${query}&tusc_user=${tusc_user}&entry=${entry}&q=0&callback=?`; // JSONP
									$.getJSON(mixnmatch_gadget_api + params, function(R){
										console.log('N/A',R,entry);
										$('.not_available[data-mnm_entry_id='+entry+']')
											.css('background-color','')
											.off('click')
											.css({'cursor':'', 'color':'gray'});
									});
								});
						})
						.fail(function(R){
							console.log('ko',R,entry);
						});
					});
				}
			}
		}
	};
	
	// Create an observer instance linked to the callback function
	const observer = new MutationObserver(callback);
	if(targetNode) {
		observer.observe(targetNode, config);
	}
}

$( function($) {

	// extension disabled - 2022.09.14
	console.log('MnM_ext2 disabled: Magnus Manke\'s gadget now includes more functionalities');
	return;
	
    /**
     * Check if we're viewing an item
     */
    var qid = mw.config.get( 'wbEntityId' );
    if ( !qid ) {
		return;
    }
    else {
		qid = qid.substring(1);
    }

	mw.MnM_extension_retries = 0;
	mw.MnM_extension_max_retries = 20;
	MnM_extension_addinterface ();
} );