$.fn.googleSuggest = function(opts){ opts = $.extend({service: 'web', secure: false}, opts); var services = { youtube: { client: 'youtube', ds: 'yt' }, books: { client: 'books', ds: 'bo' }, products: { client: 'products-cc', ds: 'sh' }, news: { client: 'news-cc', ds: 'n' }, images: { client: 'img', ds: 'i' }, web: { client: 'psy', ds: '' }, recipes: { client: 'psy', ds: 'r' } }, service = services[opts.service]; opts.minLength = 1; opts.open = function(){ var a_width = $("#q").outerWidth()+$("#search-btn").outerWidth()-2; $(".ui-widget-content").css({"width":a_width+"px"}); $(".ui-menu-item").css({"width":a_width-16+"px"}); } opts.select = function(event, ui){ $("#q").val(ui.item.value); $("#search-form").submit(); } opts.source = function(request, response){ $.ajax({ url: 'https://clients1.google.com/complete/search', dataType: 'jsonp', data: { q: request.term, nolabels: 't', client: service.client, ds: service.ds, hl: '' }, success: function(data) { response($.map(data[1], function(item){ return { value: $("").html(item[0]).text() }; }).slice(0, 4)); } }); }; return this.each(function(){ $(this).autocomplete(opts); }); }