ginebra/chrome/js/SearchSuggest.js
changeset 0 1450b09d0cfd
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 //Constructor
       
     2 function SearchSuggest(){
       
     3 }
       
     4 
       
     5 var searchArray = new Array(6);
       
     6 var arrayLimit = 6;
       
     7 var usercallback;
       
     8 
       
     9 SearchSuggest.prototype.loadYahoo = function(query){
       
    10     var yUrl = "http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?";
       
    11     var yAppId = "appid=rv9RNS3V34GEz5NvGPBClnQaRVtN1tO57Di0OxX24nfHx1iqNA_QfahxpBoJJw--";
       
    12     var yOutput = "&output=json";
       
    13     var yResults = "&results = 5";
       
    14     var yCallBack = "&callback=SearchSuggest.prototype.processYahoo&query=";
       
    15     var yahooSuggest = yUrl + yAppId + yOutput + yCallBack + query;
       
    16     
       
    17     var headID = document.getElementsByTagName("head")[0];         
       
    18     var newScript = document.createElement('script');
       
    19     newScript.type = 'text/javascript';
       
    20     newScript.src = yahooSuggest;
       
    21     headID.appendChild(newScript);
       
    22 }
       
    23 
       
    24 SearchSuggest.prototype.processYahoo = function(resultObject){
       
    25     searchArray = [];
       
    26     if(resultObject.ResultSet && resultObject.ResultSet.Result){
       
    27         var n = arrayLimit < resultObject.ResultSet.Result.length ? arrayLimit : resultObject.ResultSet.Result.length;
       
    28         for(var i=0; i<n; i++) {
       
    29             searchArray[i] = resultObject.ResultSet.Result[i];
       
    30         }
       
    31     }
       
    32     usercallback(searchArray);
       
    33 }
       
    34 
       
    35 SearchSuggest.prototype.loadGoogle = function(query){
       
    36     var gUrl = "http://suggestqueries.google.com/complete/search?hl=en&json=t&jsonp=";
       
    37     var gCallBack = "SearchSuggest.prototype.processGoogle&q=";
       
    38     var googleSuggest = gUrl + gCallBack + query;
       
    39       
       
    40     var headID = document.getElementsByTagName("head")[0];         
       
    41     var newScript = document.createElement('script');
       
    42     newScript.type = 'text/javascript';
       
    43     newScript.src = googleSuggest;
       
    44     headID.appendChild(newScript);
       
    45 }
       
    46 
       
    47 SearchSuggest.prototype.processGoogle = function(resultObject){
       
    48     searchArray = [];
       
    49     if(resultObject){
       
    50         var n = arrayLimit < resultObject[1].length ? arrayLimit : resultObject[1].length;
       
    51         searchArray[0] = resultObject[0];
       
    52         for(var i=1; i<n; i++) {
       
    53             searchArray[i] = resultObject[1][i];
       
    54         }
       
    55     }
       
    56     usercallback(searchArray);
       
    57 }
       
    58 
       
    59 SearchSuggest.prototype.searchSuggest = function(queryString, getSearchUrl){
       
    60     //load yahoo suggest database. this has a limit of 5k hits/day per ip address.
       
    61     //this.loadYahoo(queryString);
       
    62     //load google suggest database
       
    63     this.loadGoogle(queryString);
       
    64     usercallback = getSearchUrl;
       
    65 }