home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / fil50CE460251CCEB561279D710FF0C811D < prev    next >
Text File  |  2010-07-12  |  13KB  |  380 lines

  1. if (!YaUtils) {
  2.   if (typeof(Cc) === "undefined")
  3.     var Cc = Components.classes;
  4.   if (typeof(Ci) === "undefined")
  5.     var Ci = Components.interfaces;
  6.   
  7.   var YaUtils = {
  8.     CHROME_CONTENT: "chrome://yasearch/content/",
  9.     
  10.     get nsUtils() {
  11.       delete this.nsUtils;
  12.       return this.nsUtils = Cc["@yandex.ru/yasearch;1"].getService(Ci.nsIYaSearch)
  13.                                                        .wrappedJSObject
  14.                                                        .utils;
  15.     },
  16.     
  17.     get nsDOMUtils() {
  18.       delete this.nsDOMUtils;
  19.       return this.nsDOMUtils = Cc["@yandex.ru/yasearch;1"].getService(Ci.nsIYaSearch)
  20.                                                           .wrappedJSObject
  21.                                                           .DOMUtils;
  22.     },
  23.     
  24.     convertStringToUnicode: function(aString, aCharset) {
  25.       var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
  26.       converter.charset = aCharset || "windows-1251";
  27.       return converter.ConvertToUnicode(aString);
  28.     },
  29.     
  30.     evaluateXPath: function(aNode, aExpr) {
  31.       return this.nsDOMUtils.evaluateXPath(aNode, aExpr);
  32.     },
  33.     
  34.     RegExp: {
  35.       _escapeRe: /([.*+?^=!:${}()|[\]\/\\])/g,
  36.       
  37.       escape: function(aString) {
  38.         return String(aString).replace(this._escapeRe, '\\$1');
  39.       },
  40.       
  41.       createFromString: function(aString, aFlags) {
  42.         return new RegExp(this.escape(aString), aFlags);
  43.       }
  44.     },
  45.     
  46.     searchWordsRe: {
  47.       _phrasesRe: {},
  48.       
  49.       hostRe: new RegExp("(?:^|\.)(yandex|google|rambler|aport|mail|gogo|nigma|yahoo|msn|live|bing)\.(?:com|ru|ua|kz|by)$", "i"),
  50.       
  51.       getPhraseRe: function(aHost) {
  52.         if (!this._phrasesRe[aHost]) {
  53.           var q = "q";
  54.           
  55.           switch (aHost) {
  56.             case "yandex":  q = "text";             break;
  57.             case "rambler": q = "(?:query|words)";  break;
  58.             case "aport":   q = "r";                break;
  59.             case "yahoo":   q = "p";                break;
  60.           }
  61.           
  62.           this._phrasesRe[aHost] = new RegExp("[\?&](?:" + q + ")=([^#&\?]*)");
  63.         }
  64.         
  65.         return this._phrasesRe[aHost];
  66.       }
  67.     },
  68.     
  69.     getSearchTextFromLocation: function(aLocation, aWebProgress) {
  70.       var locationHost, res = false;
  71.       try {
  72.         locationHost = aLocation.host;
  73.       } catch(e) {}
  74.       
  75.       if (locationHost && locationHost.match(this.searchWordsRe.hostRe) &&
  76.           aLocation.path.match(this.searchWordsRe.getPhraseRe(RegExp.$1))) {
  77.         var t = (RegExp.$1).replace(/\+/g, " ");
  78.         try {
  79.           res = decodeURIComponent(t);
  80.           if (/^\!?link=".*"$/.test(res))
  81.             res = false;
  82.         } catch(e) {
  83.           try {
  84.             t = unescape(t);
  85.             if (/[╨░-╤æ]/i.test(t)) {
  86.               res = t;
  87.             } else {
  88.               var charset = (aWebProgress && aWebProgress.document.characterSet.toLowerCase() == "koi8-r") ?
  89.                                 "koi8-r" : null;
  90.               
  91.               t = this.convertStringToUnicode(t, charset);
  92.               res = /[╨░-╤æ]/i.test(t) ? t : false;
  93.             }
  94.           } catch(ex) {}
  95.         }
  96.         
  97.         if (res && res[0] == "#" && /maps\.yandex/.test(locationHost))
  98.           res = false;
  99.       }
  100.       
  101.       return res;
  102.     },
  103.     
  104.     getSearchTextFromWebProgress: function(aWebProgress) {
  105.       var searchTextReferring = this.getSearchTextFromLocation(aWebProgress.referringURI, aWebProgress);
  106.       if (!searchTextReferring)
  107.         return false;
  108.       
  109.       if (this.getSearchTextFromLocation(aWebProgress.currentURI, aWebProgress))
  110.         return false;
  111.       
  112.       try {
  113.         var currentHost = aWebProgress.currentURI.host;
  114.         var referringHost = aWebProgress.referringURI.host;
  115.         
  116.         var yandexHostRegExp = /(^|\.)yandex\.(ru|ua|kz|by|com)$/;
  117.         
  118.         if (currentHost === referringHost ||
  119.             (yandexHostRegExp.test(currentHost) && yandexHostRegExp.test(referringHost)))
  120.           return false;
  121.       } catch(e) {}
  122.       
  123.       return searchTextReferring;
  124.     },
  125.     
  126.     get KeyCorrector() {
  127.       delete this.KeyCorrector;
  128.       
  129.       var nsKeyCorrector = Cc["@yandex.ru/yasearch;1"].getService(Ci.nsIYaSearch)
  130.                                                       .wrappedJSObject.KeyCorrector;
  131.       
  132.       this.KeyCorrector = function(aTextbox) {
  133.         this.__textbox = aTextbox;
  134.         
  135.         this.__allowedTooltipText = aTextbox.getAttribute("yaAllowedTooltiptext") || "";
  136.         this.__allowedTooltip = null;
  137.         this.__allowedTooltipTimer = null;
  138.         
  139.         this.__lastKey = null;
  140.         this.init();
  141.       };
  142.       
  143.       this.KeyCorrector.prototype = {
  144.         _regexpNames: ["Value", "ValueFinal", "Chars"],
  145.         
  146.         _keyConvTable: nsKeyCorrector.keyConvTable,
  147.         
  148.         _charConvTable: nsKeyCorrector.charConvTable,
  149.         
  150.         _setListeners: function(aAdd) {
  151.           var fn = aAdd ? "addEventListener" : "removeEventListener";
  152.           
  153.           var events = [ ["keypress", true], ["keydown", false] ];
  154.           
  155.           if (this.__allowedCharsRE)
  156.             events.push(["input", false]);
  157.           
  158.           if (this.__allowedTooltipText)
  159.             events.push(["blur", false]);
  160.           
  161.           events.forEach(function(ev) {
  162.             this.__textbox[fn](ev[0], this, ev[1], true);
  163.           }, this);
  164.           
  165.           this.__textbox.ownerDocument.defaultView[fn]("unload", this, false, true);
  166.         },
  167.         
  168.         init: function() {
  169.           this._regexpNames.forEach(function(aReName) {
  170.             var attrName = "yaAllowed" + aReName;
  171.             var attrValue = this.__textbox.getAttribute(attrName);
  172.             this["__allowed" + aReName + "RE"] = attrValue ? new RegExp(attrValue) : null;
  173.           }, this);
  174.           
  175.           this._setListeners(true);
  176.           
  177.           var me = this;
  178.           this.__textbox.yaValueIsNotValid = function(aShowTooltip) {
  179.             if (me.__allowedValueFinalRE && !me.__allowedValueFinalRE.test(this.value)) {
  180.               if (aShowTooltip === true)
  181.                 me.showAllowedTooltip();
  182.               
  183.               return true;
  184.             }
  185.             
  186.             return false;
  187.           }
  188.         },
  189.         
  190.         destroy: function() {
  191.           this.__textbox.yaValueIsNotValid = null;
  192.           
  193.           this._setListeners(false);
  194.           
  195.           this._regexpNames.forEach(function(aReName) {
  196.             this["__allowed" + aReName + "RE"] = null;
  197.           }, this);
  198.           
  199.           this.__allowedTooltipText = null;
  200.           
  201.           if (this.__allowedTooltip) {
  202.             this.hideAllowedTooltip();
  203.             this.__allowedTooltip.parentNode.removeChild(this.__allowedTooltip);
  204.             this.__allowedTooltip = null;
  205.           }
  206.         },
  207.         
  208.         get allowedTooltip() {
  209.           if (this.__allowedTooltip === null) {
  210.             this.__allowedTooltip = false;
  211.             
  212.             if (this.__allowedTooltipText) {
  213.               var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  214.               
  215.               var doc = this.__textbox.ownerDocument;
  216.               
  217.               var tt = doc.createElementNS(XULNS, "tooltip");
  218.               tt.setAttribute("orient", "vertical");
  219.               
  220.               var description = doc.createElementNS(XULNS, "description");
  221.               description.textContent = this.__allowedTooltipText;
  222.               
  223.               tt.appendChild(description);
  224.               
  225.               this.__allowedTooltip = tt;
  226.               this.__textbox.parentNode.appendChild(tt);
  227.             }
  228.           }
  229.           
  230.           return this.__allowedTooltip;
  231.         },
  232.         
  233.         set allowedTooltip() {
  234.           if (this.__allowedTooltip) {
  235.             this.hideAllowedTooltip();
  236.             this.__allowedTooltip.parentNode.removeChild(this.__allowedTooltip);
  237.           }
  238.           
  239.           this.__allowedTooltip = null;
  240.         },
  241.         
  242.         hideAllowedTooltip: function() {
  243.           var tt = this.allowedTooltip;
  244.           if (!tt || !("openPopup" in tt))
  245.             return;
  246.           
  247.           if (this.__allowedTooltipTimer) {
  248.             this.__allowedTooltipTimer.cancel();
  249.             this.__allowedTooltipTimer = null;
  250.           }
  251.           
  252.           if ("hidePopup" in tt)
  253.             tt.hidePopup();
  254.         },
  255.         
  256.         showAllowedTooltip: function() {
  257.           var tt = this.allowedTooltip;
  258.           if (!tt || !("openPopup" in tt))
  259.             return;
  260.           
  261.           this.hideAllowedTooltip();
  262.           
  263.           tt.openPopup(this.__textbox, "overlap", 0, 7, false, null);
  264.           
  265.           this.__allowedTooltipTimer = new YaUtils.nsUtils.G_Timer(
  266.             function() {
  267.               if (tt)
  268.                 tt.hidePopup();
  269.             }, 10000
  270.           );
  271.         },
  272.         
  273.         _fireInsertCommand: function(aData) {
  274.           try {
  275.             var command = "cmd_insertText";
  276.             var controller = document.commandDispatcher.getControllerForCommand(command);
  277.             if (controller && controller.isCommandEnabled(command)) {
  278.               controller = controller.QueryInterface(Ci.nsICommandController);
  279.               var params = Cc["@mozilla.org/embedcomp/command-params;1"].createInstance(Ci.nsICommandParams);
  280.               params.setStringValue("state_data", aData);
  281.               controller.doCommandWithParams(command, params);
  282.               return true;
  283.             }
  284.           } catch(e) {}
  285.  
  286.           return false;
  287.         },
  288.         
  289.         handleEvent: function(aEvent) {
  290.           if (aEvent.isTrusted === true && !(aEvent.ctrlKey || aEvent.altKey || aEvent.metaKey)) {
  291.             switch (aEvent.type) {
  292.               case "input":
  293.                 var _re = this.__allowedCharsRE;
  294.                 if (_re) {
  295.                   this.hideAllowedTooltip();
  296.                   
  297.                   var tbvalueCurrent = this.__textbox.value;
  298.                   
  299.                   if (tbvalueCurrent) {
  300.                     function convert(match) {
  301.                       return _re.test(match) ? match : "";
  302.                     }
  303.                     var tbvalueFiltered = tbvalueCurrent.replace(/./g, convert);
  304.                     
  305.                     if (tbvalueFiltered !== tbvalueCurrent) {
  306.                       this.__textbox.value = tbvalueFiltered;
  307.                       this.showAllowedTooltip();
  308.                     }
  309.                   }
  310.                 }
  311.                 break;
  312.               
  313.               case "keydown":
  314.                 this.__lastKey = aEvent.keyCode;
  315.                 break;
  316.               
  317.               case "keypress":
  318.                 var res = false;
  319.                 var cancelEvent = false;
  320.                 
  321.                 if (aEvent.charCode) {
  322.                   var shiftKey = aEvent.shiftKey;
  323.                   var strFromCode = String.fromCharCode(aEvent.charCode);
  324.                   
  325.                   if (this._keyConvTable) {
  326.                     if ((shiftKey && strFromCode.toUpperCase() != strFromCode) ||
  327.                         (!shiftKey && strFromCode.toLowerCase() != strFromCode))
  328.                     {
  329.                       shiftKey = !shiftKey;
  330.                     }
  331.                     
  332.                     var chCode = shiftKey ?
  333.                                      this._keyConvTable.withShift[this.__lastKey] :
  334.                                      this._keyConvTable.withoutShift[this.__lastKey];
  335.                     res = (chCode && chCode != aEvent.charCode) ? String.fromCharCode(chCode) : false;
  336.                   } else {
  337.                     res = (shiftKey ? this._charConvTable.withShift[strFromCode.toLowerCase()]||false : false) ||
  338.                           this._charConvTable.withoutShift[strFromCode]||false;
  339.                   }
  340.                   
  341.                   if (this.__allowedCharsRE) {
  342.                     var str = res || strFromCode;
  343.                     if (str && !this.__allowedCharsRE.test(str)) {
  344.                       res = false;
  345.                       cancelEvent = true;
  346.                       this.showAllowedTooltip();
  347.                     }
  348.                   }
  349.                 }
  350.                 
  351.                 if (res && this._fireInsertCommand(res))
  352.                   cancelEvent = true;
  353.                 
  354.                 if (cancelEvent) {
  355.                   aEvent.preventDefault();
  356.                   aEvent.stopPropagation();
  357.                 }
  358.                 break;
  359.               
  360.               case "blur":
  361.                 this.hideAllowedTooltip();
  362.                 break;
  363.               
  364.               case "unload":
  365.                 this.destroy();
  366.                 break;
  367.               
  368.               default:
  369.                 break;
  370.             }
  371.           }
  372.         }
  373.       };
  374.       
  375.       return this.KeyCorrector;
  376.     }
  377.   };
  378.   
  379.   Components.utils.import("resource://yasearch/JSON.jsm", YaUtils);
  380. }