首先,你需要一個新的文件添加到您的mediawiki安裝。把它叫做googleSuggest.php。你需要,因爲有網絡瀏覽器的安全性(你可以感謝的瀏覽器開發商)
下面的代碼添加到它的跨域問題這個文件:
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$url="http://suggestqueries.google.com/complete/search?qu=".$q;
$text = file_get_contents($url); //Get content from Google suggest
$text=str_replace("window.google.ac.h([\"$q\",[[","",$text); //Remove unwanted portion
$arr_items=explode("],[",$text); //Split and put it in arrary
foreach($arr_items as $items)
{ $arr_item=explode(",",$items);
$key=$arr_item[0]; //Get the keyword, the arrary will have other details such as no.of resutls also.
$key=trim($key,"\""); //Use to remove quotes
if (strpos(strtolower($key), $q) !== false) {
echo "$key\n";
}
}
?>
然後,你將需要從jQuery.com 下載的jQuery然後你需要得到這個插件:http://docs.jquery.com/UI/Autocomplete
然後你將需要編輯頭部分。添加以下行。
<script type="text/javascript" src="PATHTOJQUERY.JS"></script>
<script type='text/javascript' src='PATHTOjquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="PATHTOjquery.autocomplete.css" />
<script type="text/javascript">
var keywords=['qualitypoint','qpt','quality','one','two'];
$().ready(function() {
$("#q").autocomplete("googleSuggest.php", {
width: 260,
selectFirst: false
});
$("#q").result(function(event, data, formatted) {
if (data)
$(this).parent().next().find("input").val(data[1]);
});
});</script>
然後,在你想要的網頁搜索:
<form method="get" action="http://google.com/search" autocomplete="off" >
<p>
<input type="text" id="q" />
<input type="submit" value="Google Search" />
</p>
</form>