மீடியாவிக்கி:Gadget-Wiktionarylookup.js
if (!window.disableWiktLookup) {
 /*****************
 
 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('//en.wikinews.org/w/index.php?title=MediaWiki:Gadget-dictionaryLookupHover.js&amp;action=raw&amp;ctype=text/javascript');
 
 [[Image:Wikinews-breaking-news.svg|x15px|top-left|]]
 See //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.
 
 *****************/
 
 
 /*** This is the stable version of the gadget 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('//en.wikinews.org/w/index.php?title=MediaWiki:Gadget-dictionaryLookupHover.js&amp;action=raw&amp;ctype=text/javascript');
 
 
 See //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.
 wiktLookup.exit = true; //have a hide link.
 wiktLookup.disableByDefault = true; //enable only in &lt;span class=&quot;wiktLookup-enable&quot;&gt;blah&lt;/span&gt; sections.
 
 *****************/
 
 
 /*** This is the stable 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 - //en.wikinews.org/wiki/user_talk:Bawolff ) if you have any issues, comments, bugs, need help, or whatever.
 */
 
 /*global wgUserLanguage, wgContentLanguage, window, 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';
 exit - X in corner to hide.
 disableByDefault - only enable in specially marked sections.
 */
 
 wiktLookup.validLangs = {ta: &quot;ta&quot;, en: &quot;en&quot;, fr: &quot;fr&quot;, frc: &quot;fr&quot;, fra: &quot;fr&quot;, fre: &quot;fr&quot;, nl: &quot;nl&quot;, es: 'es', it: 'it', ja: 'ja', pt: 'pt', ru: 'ru'};
 wiktLookup.wiktDomain = 'ta'; //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[mw.config.get('wgContentLanguage')]) {
   wiktLookup.wiktDomain = wiktLookup.validLangs[mw.config.get('wgContentLanguage')];
 } else if (wiktLookup.validLangs[mw.config.get('wgContentLanguage').replace(wiktLookup.stripAfterDash)]) {
   wiktLookup.wiktDomain = wiktLookup.validLangs[mw.config.get('wgContentLanguage').replace(wiktLookup.stripAfterDash)];
 }
 
 if (!wiktLookup.preferLang) wiktLookup.preferLang = (window.wgContentLanguage ? mw.config.get('wgContentLanguage') : wiktLookup.wiktDomain );
 
 if (wiktLookup.disableByDefault === undefined) wiktLookup.disableByDefault = false;
 
 wiktLookup.getOptions = function () {
  var opt = '';
  if (wiktLookup.count)
   opt += '&amp;count=' + wiktLookup.count;
  if (wiktLookup.showWord)
   opt += '&amp;showWord=' + wiktLookup.showWord;
  if (wiktLookup.audio)
   opt += '&amp;audio=' + wiktLookup.audio;
  if (wiktLookup.noRedir)
   opt += '&amp;rd=500';
  if (wiktLookup.exit)
   opt += '&amp;exit=true';
 
  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 &amp;&amp; expandWord.test(textNode.data[0])) {
         for (var i = 0; i &lt; 30 /*to stop run-away*/; i++) {
           rangeBefore.setStart(rangeBefore.startContainer, rangeBefore.startOffset - 1);
           textNode = rangeBefore.cloneContents().firstChild;
           if ((textNode.nodeType !== 3) || (textNode.nodeType === 3 &amp;&amp; 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 &amp;&amp; expandWord.test(textNode.data.charAt(textNode.data.length - 1))) {
       for (var i = 0; i &lt; 30 /*to stop run-away*/; i++) {
         rangeAfter.setEnd(rangeAfter.endContainer, rangeAfter.endOffset + 1);
         textNode = rangeAfter.cloneContents().firstChild;
         if ((textNode.nodeType !== 3) || (textNode.nodeType === 3 &amp;&amp; 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 &amp;&amp; e.type === 'dblclick' &amp;&amp; !wiktLookup.reverseShift) {
     return true; //don't do anything if shift is pressed down. for compat with other things.
   }
   if (!e.shiftKey &amp;&amp; e.type === 'dblclick' &amp;&amp; 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.
           //and determine if we're in an enabled section
           // aka &lt;span class=&quot;wiktLookup-enable&quot;&gt;blah&lt;/span&gt;
           //wiktLookup.disableByDefault
       var enabled = !wiktLookup.disableByDefault;
       var foundAnEnableSection = false;
       var foundLangSection = false;
       var cur = window.getSelection().anchorNode.parentNode;
       for (var i = 0; i &lt; 9; i++) {
         if (!foundAnEnableSection &amp;&amp; cur.className.indexOf('wiktLookup-disable') !== -1) {
           return; //die
         }
         if (!foundAnEnableSection &amp;&amp; cur.className.indexOf('wiktLookup-enable') !== -1) {
           enabled = true;
           foundAnEnableSection = true;
           if (foundLangSection) break;
         }
         if (!foundLangSection &amp;&amp; cur.lang) {
           langOverride = cur.lang.replace(wiktLookup.stripAfterDash);
           if (foundAnEnableSection) break;
         } else {
           if (cur.parentNode) {cur = cur.parentNode;}
           else {break;}
         }
       }
       if (!enabled) {
         return; //die
       }
     }
     catch (e) { 
       /*ignore error*/
       if (!enabled) {
         return; //die
       } 
     }
   }
   else if (document.getSelection) {
     text = wiktLookup.rangeToWord(document.getSelection());
   }
 
   if (!clientX) {
    clientX = 400;
    clientY = 200;
    //good as numbers as any...
   }
 
   if (text &amp;&amp; text.length &lt; 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 &amp;&amp; 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 &gt; 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 !== &quot;complete&quot;) return false;
       //IE
       var my_range = document.selection.createRange();
       my_range.collapse();
       my_range.expand(&quot;word&quot;);
 
       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 &amp;&amp; document.documentElement.scrollLeft !== undefined) { return document.documentElement.scrollLeft; }
   //other IE
   if (document.body &amp;&amp; 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 &amp;&amp; document.documentElement.scrollTop !== undefined) { return document.documentElement.scrollTop; }
   //other IE
   if (document.body &amp;&amp; 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 &amp;&amp; document.documentElement.clientWidth !== undefined) { return document.documentElement.clientWidth; }
   //other IE
   if (document.body &amp;&amp; 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&amp;redirects&amp;prop=text&amp;format=xml&amp;xslt=MediaWiki:extractFirst.xsl&amp;page=' + s + '&amp;lang=' + (langOveride ? langOveride : wiktLookup.preferLang) + wiktLookup.getOptions();
     var left, top;
     if (!absCoords) {
       var left = x + wiktLookup.getScrollX();
       if (left + (wiktLookup.width ? wiktLookup.width  : 420) &gt; wiktLookup.getInnerWidth() &amp;&amp; left - (wiktLookup.width ? wiktLookup.width  : 420) &gt; 0) {
         left -=  (wiktLookup.width ? wiktLookup.width  : 420);
       }
       top = (y + wiktLookup.getScrollY() + 10);
     }
     else {
       top = y + 10;
       left = (x + (wiktLookup.width ? wiktLookup.width  : 420) &gt; wiktLookup.getInnerWidth() ? wiktLookup.getInnerWidth() -  (wiktLookup.width ? wiktLookup.width  : 420) : 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(&quot;dblclick&quot;, wiktLookup.findWord, true); //capture
   } else if (document.attachEvent) {
     document.attachEvent(&quot;ondblclick&quot;, wiktLookup.findWord);
   }
   else {
     document.ondblclick = wiktLookup.findWord;
   }
 }
 wiktLookup.supported = function () {
  if (navigator &amp;&amp; navigator.userAgent &amp;&amp; navigator.userAgent.indexOf('Gecko') !== -1 &amp;&amp; navigator.productSub  &amp;&amp; navigator.productSub &lt;= 20090823 &amp;&amp; navigator.productSub &gt; 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 || document.selectNodes ) return true; //note: second part for IE
  
  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(//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 !== 'javascript:%22%20%22') frame.src = 'javascript:%22%20%22'; //transparent
     //not using about:blank due to bug with font re-sizing on firefox.
     frame.style.display = 'none';
     document.getElementById('dict-popup-container').style.display = 'none';
   } catch(e) {/*ignore*/ }
 }