User:Bawolff/sandbox/wiktLookupDevelopment.js

/*****************

If you wish to use this elsewhere, please do not copy it, but instead hotlink it. This is still undergoing active development, and you will miss out if you just statically copy it. To hotlink, copy only the following line:

mw.loader.load('http://en.wikinews.org/w/index.php?title=MediaWiki:Gadget-dictionaryLookupHover.js&action=raw&ctype=text/javascript');

See http://en.wikinews.org/wiki/WN:WiktLookup#Wiktionary_lookup_gadget_.28Hover_box_variety.29 for details on this script, and how to use it on your own site.

Note this script can take a number of parameters. Parameters are stored in the wiktLookup object. Generally its a good idea to set parameters before the onload event is fired (some parameters it matters, others it doesn't). An example of parameters would be using the following js code (don't feel like you have to set all of them. they all have defaults. this is just an example):

if (!window.wiktLookup) wiktLookup = {}; wiktLookup.count = 1; //number of defn to return. wiktLookup.showWord = bold; // to show what word we're looking up. one of bold, none, link wiktLookup.noRedir = false;// true to stop magic (not mediawiki)-redirection. unrecomended wiktLookup.audio = true;// true, false, autoplay. NOT IMPLEMENTED wiktLookup.width = 500; // width of box. make sure number not string. in units of pixels. wiktLookup.height = 400; //- height of box wiktLookup.key = 'w';// - ctrl+shift+key makes box popup (should be lowercase. default is 'l') wiktLookup.reverseShift = false; // - if set, requires shift+double click to lookup a word. wiktLookup.mode = context; // - context, bottom. default to context. to display as tooltip, or at bottom wiktLookup.definitionBegin = 'Definition ('; // - for i18n. in english it is 'Definition ('. used in mode='bottom' wiktLookup.definitionEnd = '):'; // - for i18n. in English it is '):' used in bottom mode wiktLookup.hideText = 'hide'; //- for i18n. in English it is 'hide'. used in bottom mode.



/*** This is the development version of the gadget js.***/ /* Stolen from fr wikinews, and than subsequently heavily modified by Bawolff. This script is based on the french one created by en:wiktionary:User:Bequw and en:wiktionary:User:Conrad.Irwin. The bottom mode is heavily based on work done by fr:wikt:user:Darkdadaah.

Special thanks for wikinews:User:Amgine, fr:Wikinews:User:Otourly, fr:wikinews:user:Sniff, fr:Wikinews:User:JackPotte, en:wikibooks:user:TestPilot, and many others for support and assistance.

The current version of this is maintained by Bawolff. please don't hesitate to contact me (bawolff - http://en.wikinews.org/wiki/user_talk:Bawolff ) if you have any issues, comments, bugs, need help, or whatever.

/*global wgUserLanguage, wgContentLanguage, window, hookEvent, addOnloadHook, XSLTProcessor*/ /*jslint browser: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, newcap: true, immed: true, maxerr: 80 */

/*members TEXT_NODE, addEventListener, appendChild, attachEvent, body, border, charAt, clientWidth, clientX, clientY, cloneContents, cloneRange, collapse, createElement, createRange, data, detach, display, documentElement, en, endContainer, endOffset, event, expand, findWord, firstChild, focus, fr, fra, frc, fre, getElementById, getInnerWidth, getRangeAt, getScrollX, getScrollY, getSelection, height, id, innerWidth, left, length, lookupWord, match, nl, nodeType, ondblclick, open, pageXOffset, pageYOffset, position, preferLang, preventDefault, rangeOffset, rangeParent, rangeToWord, readyState, replace, returnValue, scrollLeft, scrollTop, selection, setEnd, setStart, setup, shiftKey, split, src, startContainer, startOffset, stopPropagation, stripAfterDash, style, substring, text, toString, top, useNewWindow, validLangs, wgUserLanguage, width, wiktDomain, wiktLookup, zIndex, audio, count, toUpperCase, audio, noRedir

//set wiktLookup.useNewWindow to true to force new window. if (!window.wiktLookup) var wiktLookup = {}; wiktLookup.key = wiktLookup.key ? wiktLookup.key : 'l'; /* Options: count - numb results to return showWord - bold, none, link noRedir - true to stop redirection audio - true, false, autoplay width - width of box height - height of box key - ctrl+shift+key makes box popup (should be lowercase) reverseShift - if set, requires shift+double click to lookup a word. mode - context, bottom. default to context definitionBegin - for i18n. in english it is 'Definition (' definitionEnd - for i18n. in English it is '):' hideText - for i18n. in English it is 'hide';

wiktLookup.validLangs = {en: "en", fr: "fr", frc: "fr", fra: "fr", fre: "fr", nl: "nl", es: 'es', it: 'it', ja: 'ja'}; wiktLookup.wiktDomain = 'en'; //default wiktLookup.stripAfterDash = /-.*$/; if (wiktLookup.validLangs[wgUserLanguage]) { wiktLookup.wiktDomain = wiktLookup.validLangs[wgUserLanguage]; } else if (wiktLookup.validLangs[wgUserLanguage.replace(wiktLookup.stripAfterDash)]) { wiktLookup.wiktDomain = wiktLookup.validLangs[wgUserLanguage.replace(wiktLookup.stripAfterDash)]; } else if (wiktLookup.validLangs[wgContentLanguage]) { wiktLookup.wiktDomain = wiktLookup.validLangs[wgContentLanguage]; } else if (wiktLookup.validLangs[wgContentLanguage.replace(wiktLookup.stripAfterDash)]) { wiktLookup.wiktDomain = wiktLookup.validLangs[wgContentLanguage.replace(wiktLookup.stripAfterDash)]; }

if (!wiktLookup.preferLang) wiktLookup.preferLang = (window.wgContentLanguage ? wgContentLanguage : wiktLookup.wiktDomain );

wiktLookup.getOptions = function { var opt = ''; if (wiktLookup.count) opt += '&count=' + wiktLookup.count; if (wiktLookup.showWord) opt += '&showWord=' + wiktLookup.showWord; if (wiktLookup.audio) opt += '&audio=' + wiktLookup.audio; if (wiktLookup.noRedir) opt += '&rd=500';

return opt; }

wiktLookup.rangeToWord = function (selection) { //includes apostraphes //fixme: if at begin of container. var finalText = selection.toString; try { var expandWord = /['\-]/ //list of chars that are not actual word boundries. 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 && expandWord.test(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; }         else if (rangeBefore.startOffset === 0) { //at the end of the range. this might add periods, which are stripped later. finalText = rangeBefore.toString; rangeBefore.detach; range.detach; return finalText; }       }      }    }    rangeBefore.detach; //done with it. var rangeAfter = range.cloneRange; rangeAfter.setEnd(rangeAfter.endContainer, rangeAfter.endOffset + 1); var textNode = rangeAfter.cloneContents.firstChild; //double var... if (textNode.nodeType === 3 && expandWord.test(textNode.data.charAt(textNode.data.length - 1))) { 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; } else if (rangeAfter.endOffset - rangeAfter.endContainer.data.length === 0) { // this assumes this is a text node... this might add periods, which are stripped later. finalText = rangeAfter.toString; rangeAfter.detach; range.detach; return finalText; }     }    }    rangeAfter.detach; //no match, and done with this range. } catch (e) { if (window.range) {range.detach;} return finalText; } return finalText; }

wiktLookup.findWord = function (e) { if (!e) {e = window.event;} if (e.shiftKey && e.type === 'dblclick' && !wiktLookup.reverseShift) { return true; //don't do anything if shift is pressed down. for compat with other things. } if (!e.shiftKey && e.type === 'dblclick' && wiktLookup.reverseShift) { return true; //if reverse shift is on, require shift key for double click. } var clientX, clientY, coordsAbs, langOverride; if (e.clientX) {//this implies has Y as well clientX = e.clientX; clientY = e.clientY; } // stolen from http://www.codetoad.com/javascript_get_selected_text.asp var text; if (window.getSelection) { text = wiktLookup.rangeToWord(window.getSelection); if (!clientX) { try { //mostly for from keyboard requests var parent = window.getSelection.anchorNode.parentNode; clientX = parent.offsetLeft + 7*window.getSelection.anchorOffset; //this is very rough clientY = parent.offsetTop + parent.offsetHeight; //at bottom of elm coordsAbs = true; while (parent = parent.offsetParent) { clientX += parent.offsetLeft; clientY += parent.offsetTop; }    } catch (e) {} }   try { //figure out what language the current node is. var cur = window.getSelection.anchorNode.parentNode; for (var i = 0; i < 5; i++) { if (cur.lang) { langOverride = cur.lang.replace(wiktLookup.stripAfterDash); break; } else { if (cur.parentNode) {cur = cur.parentNode;} else {break;} }     }    }    catch (e) { /*ignore error*/ } } else if (document.getSelection) { text = wiktLookup.rangeToWord(document.getSelection); }

if (!clientX) { clientX = 400; clientY = 200; //good as numbers as any... }

if (text && text.length < 32) { //If we have the text. we be done. return wiktLookup.lookupWord(text, clientX, clientY, coordsAbs, langOverride); }

// seems to work somewhat on moz. Hopefully moz will be picked up above. So this shouldn't be neccesary. 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) { wiktLookup.lookupWord(wordlist1[wordlist1.length - 1]+wordlist2[0], clientX, clientY, coordsAbs, langOverride); }   e.preventDefault; e.stopPropagation; } //IE specific stuff. Try this as a last resort. else { try { if (document.readyState !== "complete") return false; //IE var my_range = document.selection.createRange; my_range.collapse; my_range.expand("word");

wiktLookup.lookupWord(my_range.text, clientX, clientY, coordsAbs, langOverride);; e.returnValue = false; return false; } catch (err) { /*ignore*/ } } } wiktLookup.getScrollX = function  { //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; } return 0; //default } wiktLookup.getScrollY = function { //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; } return 0; } wiktLookup.getInnerWidth = function { //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; } return 0; } wiktLookup.lookupWord = function(word, x, y, absCoords,langOveride) { if (!word.match(/(?:^[\wa-zA-Zа-яА-Яà-žÀ-ŽΑ-ῥԱ-ևぁ-ヶ促-杁ㄱ-하ⁿßſ]|[\wa-zA-Zа-яА-Яà-žÀ-ŽΑ-ῥԱ-ևぁ-ヶ促-杁ㄱ-하ⁿßſ]\s?$)/)) { //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 } var s = word2 ? word2[0] : word //if regex failed, fall back to word if (!s) {return false;} // test if null or empty. s = encodeURIComponent(s);

if (!wiktLookup.supported) { //This includes IE, which doesn't recognize the mime type mediawiki uses. // in future this could put the window where user clicked. var newwin = window.open('http://' + wiktLookup.wiktDomain + '.wiktionary.org/wiki/' + s + '?uselang=' + wiktLookup.preferLang,'temp','height=450,width=800,location,menubar,toolbar,status,resizable,scrollbars'); if (newwin) { newwin.focus; }     return true; }

var frame = document.getElementById('dict-popup-frame'); frame.src = 'about:blank'; //don't display old results. frame.style.display = 'block'; frame.src = 'http://' + wiktLookup.wiktDomain + '.wiktionary.org/w/api.php?action=parse&redirects&prop=text&format=xml&xslt=MediaWiki:extractFirst.xsl&page=' + s + '&lang=' + (langOveride ? langOveride : wiktLookup.preferLang) + wiktLookup.getOptions; var left, top; if (!absCoords) { var left = x + wiktLookup.getScrollX; if (left + (wiktLookup.width ? wiktLookup.width : 420) > wiktLookup.getInnerWidth && left - (wiktLookup.width ? wiktLookup.width : 420) > 0) { left -= (wiktLookup.width ? wiktLookup.width  : 420); }     top = (y + wiktLookup.getScrollY + 10); }   else { top = y + 10; left = x;   } frame.style.top = top +'px'; frame.style.left= left +'px'; document.getElementById('dict-popup-container').style.display = 'block'; }

wiktLookup.setup = function { if (document.addEventListener) { document.addEventListener("dblclick", wiktLookup.findWord, true); //capture } else if (document.attachEvent) { document.attachEvent("ondblclick", wiktLookup.findWord); } else { document.ondblclick = wiktLookup.findWord; }

hookEvent('keypress', function(event) {  if (!event) event = window.event;  if (!(event.ctrlKey && event.shiftKey)) return true;  var key = (event.charCode ? event.charCode : event.keyCode);

if (key && (String.fromCharCode(key) === wiktLookup.key || String.fromCharCode(key) === wiktLookup.key.toUpperCase)) { wiktLookup.findWord(event); if (event.preventDefault) event.preventDefault; return false; } return true; }); } wiktLookup.supported = function { if (navigator && navigator.userAgent && navigator.userAgent.indexOf('Gecko') !== -1 && navigator.productSub  && navigator.productSub <= 20090823 && navigator.productSub > 2000000) {  //old moz has broken support for html entities.  //FIXME: figure out exact date when gecko was fixed.  return false; }

if (wiktLookup.useNewWindow) return false; if (window.XSLTProcessor) return true; //note: IE doesn't have this. return false; }

$(wiktLookup.setup);

$(function {  if (!wiktLookup.definitionBegin) wiktLookup.definitionBegin = 'Definition ('; if (!wiktLookup.definitionEnd) wiktLookup.definitionEnd = '):'; if (!wiktLookup.hideText) wiktLookup.hideText = 'hide';

var div = document.createElement('div'); div.id = 'dict-popup-container'; var frame = document.createElement('iframe'); frame.style.border = 'none'; if (wiktLookup.mode !== 'bottom') frame.style.position = 'absolute'; frame.style.display = 'none'; frame.style.zIndex = '102'; if (wiktLookup.mode === 'bottom') { frame.style.width = '98%'; } else { frame.style.width = (wiktLookup.width ? wiktLookup.width + 'px' : '420px'); //default 300 if unset } frame.style.height = (wiktLookup.height ? wiktLookup.height + 'px' : '180px'); //default 150 if unset. frame.id = 'dict-popup-frame'; frame.src = 'about:blank';

if (wiktLookup.mode === 'bottom') { div.style.padding = '0.3em'; div.style.display = 'none'; div.style.position = 'fixed'; div.style.margin = '0'; div.style.bottom = '0'; div.style.zIndex = '50'; div.style.background = 'url(http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/840px-Wiktionary-logo.svg.png) white no-repeat 30%'; div.style.borderTop = 'solid thin black'; div.style.width = '100%';

div.appendChild(document.createTextNode(wiktLookup.definitionBegin)); var link = document.createElement('a'); link.href = '#'; link.onclick = wiktLookup.hide; link.appendChild(document.createTextNode(wiktLookup.hideText)); div.appendChild(link); div.appendChild(document.createTextNode(wiktLookup.definitionEnd)); } div.appendChild(frame); document.body.appendChild(div);

});

wiktLookup.hide = function { try { var frame = document.getElementById('dict-popup-frame'); if (frame.src !== 'about:blank') frame.src = 'about:blank'; //transparent frame.style.display = 'none'; document.getElementById('dict-popup-container').style.display = 'none'; } catch(e) {/*ignore*/ } } hookEvent('click', wiktLookup.hide);