MediaWiki:Lookup.js

// // copied from fr.wikinews $(function {	var goInline = false;	var validLangs = {		en: "en",		fr: "fr",		frc: "fr",		fra: "fr",		fre: "fr",		nl: "nl"	};	var stripAfterDash = /-.*$/;	var userLanguage = mw.config.get("wgUserLanguage");	var contentLanguage = mw.config.get("wgContentLanguage");	var wiktDomain = validLangs[userLanguage]		|| validLangs[userLanguage.replace(stripAfterDash)]		|| validLangs[contentLanguage]		|| validLangs[contentLanguage.replace(stripAfterDash)]		|| "en"; // default	var mozilla = false;	if (document.all == null && document.getElementById != null && document.layers == null) {		mozilla = true;	}

function rangeToWord(selection) { //includes apostraphes //fixme: if at begin of container. var finalText = selection.toString; try { var range = selection.getRangeAt(0); var rangeBefore = range.cloneRange; if (rangeBefore.startOffset !== 0) { rangeBefore.setStart(rangeBefore.startContainer, rangeBefore.startOffset - 1); var textNode = rangeBefore.cloneContents.firstChild; if (textNode.nodeType === 3 && textNode.data.charAt(0) === "'") { for (var i = 0; i < 30 /*to stop run-away*/ ; i++) { rangeBefore.setStart(rangeBefore.startContainer, rangeBefore.startOffset - 1); textNode = rangeBefore.cloneContents.firstChild; if ((textNode.nodeType !== 3) || (textNode.nodeType === 3 && textNode.data.match(/^\s/))) { rangeBefore.setStart(rangeBefore.startContainer, rangeBefore.startOffset + 1); finalText = rangeBefore.toString; rangeBefore.detach; range.detach; return finalText; }					}				}			}			var rangeAfter = range.cloneRange; rangeAfter.setEnd(rangeAfter.endContainer, rangeAfter.endOffset + 1); var textNode = rangeAfter.cloneContents.firstChild; //double var... if (textNode.nodeType === 3 && textNode.data.charAt(0) === "'") { for (var i = 0; i < 30 /*to stop run-away*/ ; i++) { rangeAfter.setEnd(rangeAfter.endContainer, rangeAfter.endOffset + 1); textNode = rangeAfter.cloneContents.firstChild; if ((textNode.nodeType !== 3) || (textNode.nodeType === 3 && textNode.data.match(/\s$/))) { rangeAfter.setEnd(rangeAfter.endContainer, rangeAfter.endOffset - 1); finalText = rangeAfter.toString; rangeAfter.detach; range.detach return finalText; }				}			}		} catch (e) { if (range) { range.detach; }			return finalText; }		return finalText; }

function FindWord(e) { if (!e) { e = window.event; }		if (e.shiftKey) { return true; //don't do anything if shift is pressed down. for compat with other things. }		// stolen from http://www.codetoad.com/javascript_get_selected_text.asp var text; if (window.getSelection) { text = rangeToWord(window.getSelection); } else if (document.getSelection) { text = rangeToWord(document.getSelection); }		/***		 else if (document.selection && document.selection.createRange ) { text = document.selection.createRange.text; } ***/		if (text && text.length < 32) { return LookupWord(text, e.clientX, e.clientY); }		if (!mozilla && window.event && document && document.body) { if (document.readyState != "complete") return false; //IE var my_range = document.selection.createRange; my_range.collapse; my_range.expand("word");

LookupWord(my_range.text);

e.returnValue = false; return false; } else if (e.rangeParent && e.rangeParent.nodeType == document.TEXT_NODE) { //mozilla part var rangeOffset = e.rangeOffset; var my_rangestr = e.rangeParent.data; //the event is dynamic!

// which word the rangeOffset is in			var wordlist1 = my_rangestr.substring(0, rangeOffset).split(/\s+/); var wordlist2 = my_rangestr.substring(rangeOffset, my_rangestr.length).split(/\s+/);

if (my_rangestr.length > 0) { LookupWord(wordlist1[wordlist1.length - 1] + wordlist2[0], e.clientX, e.clientY); }			e.preventDefault; e.stopPropagation; }

}

function getScrollX { var scroll = 0; //default //standard if (window.pageXOffset !== undefined) { return window.pageXOffset; }		//IE 6 in standards mode if (document.documentElement && document.documentElement.scrollLeft !== undefined) { return document.documentElement.scrollLeft; }		//other IE		if (document.body && document.body.scrollLeft !== undefined) { return document.body.scrollLeft; }	}

function getScrollY { var scroll = 0; //default //standard if (window.pageYOffset !== undefined) { return window.pageYOffset; }		//IE 6 in standards mode if (document.documentElement && document.documentElement.scrollTop !== undefined) { return document.documentElement.scrollTop; }		//other IE		if (document.body && document.body.scrollTop !== undefined) { return document.body.scrollTop; }	}

function getInnerWidth { var scroll = 1024; //default //standard if (window.innerWidth !== undefined) { return window.innerWidth; }		//IE 6 in standards mode if (document.documentElement && document.documentElement.clientWidth !== undefined) { return document.documentElement.clientWidth; }		//other IE		if (document.body && document.body.clientWidth !== undefined) { return document.body.clientWidth; }	}

function LookupWord(word, x, y) { if (!word.match(/(?:^[àâçéèêëîïôûùüÿÀÂÇÉÈÊËÎÏÔÛÙÜŸ]|[àâçéèêëîïôûùüÿÀÂÇÉÈÊËÎÏÔÛÙÜŸ]$)/)) { //this regex doesn't work if word starts or ends with accents. //is this regex even needed? var word2 = word.match(/\b(?:[\S]|(?!\.\s)\.)*\b/); //strip quotation marks }		s = word2 ? word2[0] : word //if regex failed, fall back to word if (!s) { return false; } // test if null or empty. s = encodeURIComponent(s);

var frame = $('#dict-popup-frame'); frame.show; var src = '/w/api.php?action=parse&prop=text&format=xml&xslt=MediaWiki:extractFirst.xsl&page=' + s + '&lang=' + mw.config.get('wgContentLanguage'); $.ajax({			url: src, 			success: function(r) {				frame.html(r.firstElementChild.firstElementChild.nextElementSibling.firstElementChild.textContent);			}		}); var left = x + getScrollX; if (left + 420 > getInnerWidth && left - 420 > 0) { left -= 420 }		frame.css("top", (y + getScrollY + 10) + 'px'); frame.css("left", left + 'px'); }

if (document.addEventListener) { document.addEventListener("dblclick", FindWord, true); } else if (document.all) { document.ondblclick = FindWord; }

}); $(function { var frame = document.createElement('div'); frame.style.border = 'none'; frame.style.position = 'absolute'; frame.style.display = 'none'; frame.style.zIndex = '102'; frame.style.width = '420px'; //default 300 frame.style.height = '180px'; //default 150 frame.id = 'dict-popup-frame'; document.body.appendChild(frame); //frame.src ='http://en.wiktionary.org/w/api.php?action=parse&prop=text&format=xml&xslt=MediaWiki:extractFirst.xsl&page=' + s;

});

$(document).on('click', function {	try {		document.getElementById('dict-popup-frame').style.display = 'none';	} catch (e) {} });