User:Schloe06/common.js

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.dialog'], function {	// insert required elements	var iframe = $('');   var sidebar = $(' ').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); });

});

});