User:Schloe06/common.js

From mediawiki.org

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.
messages = {
    'en': {
        'quickchanges-title': 'Hello there!',
        'quickchanges-greeting': 'Welcome, $1!',
        'quickchanges-intro': 'The following pages have been recently modified:',
        'quickchanges-link': 'Find related content',
        'quickchanges-tooltip': 'Get recommendations of images and references on the fly'
    }
};

mw.messages.set(messages['en']);
var lang = mw.config.get('wgUserLanguage');
if (lang && lang != 'en' && lang in messages) {
    mw.messages.set(messages[lang]);
}

// Import the jQuery dialog plugin before starting the rest of this script
mw.loader.using(['jquery.ui'], function() {
	// insert required elements
	var iframe = $('<iframe src="https://rawgit.com/schloett/wiki-recommender-tmp/master/visualization-widgets/SearchResultListVis/index.html" style="border:none;position:absolute;width:100%;height:100%;top:0;left:0;" />');
    var sidebar = $('<div style="position:fixed;z-index:9999;width:25%;height:100%;top:0;right:0;background-color:white;border-left:1px solid silver;"></div>').hide();
    $(sidebar).append(iframe);
    $('body').append(sidebar);
    
		$('#wpTextbox1').click(function(){
			$(sidebar).hide();
		});

	var autoQuery = function(e) {
		if (e.keyCode == 13) {
			console.log('trigger');
			$(iframe).get(0).contentWindow.postMessage({event:'eexcess.queryTriggered', data:'msg'}, '*');
            var text = this.value;
            // extract paragraph
            var cursorPosition = this.selectionStart;
            var delimiter = "==";
            var start = 0;
            var pos = this.value.indexOf(delimiter);

            var lastDelimiters = [-1, -1];
            var lastDelimitersIndex = 0;

            while (pos != -1 && pos < cursorPosition) {
                start = pos + delimiter.length;
                lastDelimiters[lastDelimitersIndex++] = pos;
                if (lastDelimitersIndex > 1)
                    lastDelimitersIndex = 0;
                pos = this.value.indexOf(delimiter, start);
            }
            var end = this.value.indexOf(delimiter, cursorPosition) - 1;
            if (end < 0) {
                end = text.length;
            }
            
            var documentTitle = window.location.href;
            var titleKey = 'title=';
            var titleStart = documentTitle.indexOf(titleKey) + titleKey.length;
            var titleEnd = documentTitle.indexOf('&', titleStart);
            documentTitle = documentTitle.substring(titleStart, titleEnd).replace('_', ' ');
            
            var paragraphTitle = text.substring(lastDelimiters[0] + delimiter.length, lastDelimiters[1]).trim();
            
            text = text.substring(start, end);
            
            console.log('docTitle: ' + documentTitle);
            console.log('parTitle: ' + paragraphTitle);
            console.log('text: ' +text);
			$(iframe).get(0).contentWindow.postMessage({event:'eexcess.queryTriggered', data:{query:documentTitle + ' OR ' + paragraphTitle}}, '*');

		    $.ajax({
        		url: 'https://commons.wikimedia.org/w/api.php?',
		        data: {
	            	action: "query",
	            	origin: 'https://en.wikipedia.org',
    	        	generator: "search",
        	    	gsrnamespace: "6",
            		gsrsearch: '"' + documentTitle + '" OR "' + paragraphTitle + '"',
            		gsrlimit: "20",
            		gsroffset: "20",
            		prop: "imageinfo",
            		iiprop: "url|extmetadata",
            		format: "json",
            		//fetching only thumbs to ensure faster loading
            		iiurlwidth: "120"
        		},

        		xhrFields: {withCredentials: true},
        		success: function (response) {
        			console.log(response);
        			$(sidebar).fadeIn();
        			var msg ={
        				event:'eexcess.newResults',
        				dataWiki:{data:response},
        				dataEEXCESS:{data:{totalResults:0}}
        			};
        			$(iframe).get(0).contentWindow.postMessage(msg, '*');
            	}
    		});
            
            /*
            var paragraphs = {
            	language:'en',
            	paragraphs:[{
            		id:1,
            		content:text,
            		headline:''
            	}]
            };
            console.log(paragraphs);
            var xhr = $.ajax({
                url: 'https://eexcess.joanneum.at/eexcess-privacy-proxy-issuer-1.0-SNAPSHOT/issuer/recognizeEntity',
                data: JSON.stringify(paragraphs),
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                timeout:10000
            });
            
            xhr.done(function(response) {
            	console.log(response);
            });
            
            xhr.fail(function(jqXHR, textStatus, errorThrown) {
            	console.log(jqXHR);
            	console.log(textStatus);
            	console.log(errorThrown);
            })*/
            
		}
	};

	$(document).ready( function() {
		$('#wpTextbox1').bind('keyup',autoQuery);

		// Add a link to the toolbox
		var link = mw.util.addPortletLink(
			'p-tb',
			'#',
			mw.message('quickchanges-link').plain(),
			't-prettylinkwidget',
			mw.message('quickchanges-tooltip').plain(),
			'/',
			'#t-whatlinkshere'
		);

		// Create a jQuery object for this link so that we get
		// to use jQuery awesomeness like .click() for binding functions to events
		// and methods like e.preventDefault();
		$(link).click( function( e ) {
			// Avoid the browser going to '#'
			e.preventDefault();

			// Initiate quickRC!
			//quickRC();
			console.log('test');
			$('#wpTextbox1').bind('keyup',autoQuery);
		});
		


	});

});