MediaWiki:Gadget-dictionaryLookup.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

//Stolen from fr wikinews.
$(function () {
var goInline=false;
 
var mozilla=false;
if (document.all == null && document.getElementById != null && document.layers == null) {
        mozilla = true;
}
function FindWord(e) {
        if (!e) {e = window.event;}
	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 range = document.createRange();
		range.selectNode(e.rangeParent);
		var my_rangestr = range.toString();
		range.detach();
 
 
		// 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.preventDefault(); 
		e.stopPropagation();
	}
}
 
 
function LookupWord(word) {
                var word2 = word.match(/\b(?:[\w-]|(?!\.\s)\.)*\b/); //strip quotation marks
                s = word2 ? word2[0] : word //if regex failed, fall back to word
		var newurl = "//en.wiktionary.org/wiki/" + encodeURIComponent(s);
 
		if (goInline)location.href = newurl;
		else{
			var newwin = window.open(newurl,'temp','height=450,width=800,location,menubar,toolbar,status,resizable,scrollbars');
			if (newwin)     newwin.focus();		
		}
}
 
 
if (document.addEventListener) {
        document.addEventListener("dblclick", FindWord, true);
} else if (document.all) {
        document.ondblclick = FindWord;
}

});