研究了一下Google Ajax Search API, 给博客做了个样品
效果比较简陋,没有美工的细胞:(
很多代码都是直接从Google的sample里抄的,我做的工作只是添加了简单的PopUp机制.
代码相当简单,就不用注释了.
全部代码:
CSS:
<style type="text/css">
#searcher { width: 200px; }
#searchresultdialog.show { display:block; background-color:white; width:400px; border:solid 1px #eee; position:absolute; padding:0; }
#searchresultdialog.hide { display:none; }
#searchresult .header { font-size:larger;}
#hideresult, #moreresults { text-align:right; margin-top:0; padding:5px 20px; background:#eee; }
#searchresult li { border-bottom:solid 1px #eee; }
</style>
JavaScript:
<!--注意:下面的这个key应该是每个人/网站/虚拟目录都不同的, 需要从Google申请. 申请过程完全免费, 应该是百分百成功率. 申请地址: http://code.google.com/apis/ajaxsearch/signup.html -->
<script src="http://www.google.com/jsapi?key=ABQIAAAAnbDm87eis9d6TnBZLuDj_BT3fZxwUXmDsFtw544tTyLwi1xKvRQWzjl_UJiFlgsvxapvgIGau2FakA" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
google.load("search", "1");
function PopupGoogleSearch()
{
var searchControl = new google.search.SearchControl();
this.searcher= new google.search.WebSearch();
searchControl.addSearcher(this.searcher);
this.searcher.setResultSetSize(google.search.Search.SMALL_RESULTSET);
this.searcher.setSiteRestriction("deerchao.cnblogs.com");
this.searcher.setNoHtmlGeneration();
this.searcher.setSearchCompleteCallback(this, PopupGoogleSearch.prototype.searchComplete, [this.searcher]);
this.searchForm = new google.search.SearchForm(true, document.getElementById("searchform"));
this.searchForm.setOnSubmitCallback(this, PopupGoogleSearch.prototype.onSubmit);
this.searchForm.setOnClearCallback(this, PopupGoogleSearch.prototype.onClear);
this.resultPanel = document.getElementById("searchresult");
this.moreResults = document.getElementById("moreresults");
}
PopupGoogleSearch.prototype.searchComplete = function (searcher)
{
this.onClear();
if (searcher.results && searcher.results.length > 0)
{
var ul = document.createElement("ul");
for(var i=0; i<searcher.results.length; i++)
{
var result = searcher.results [i];
var li = document.createElement("li");
var a = document.createElement("a");
a.innerHTML = "<p class='header'><a href ='" + result.url + "'>" +result.title +"</a></p>";
li.appendChild(a);
var p = document.createElement("div");
p.innerHTML = result.content;
li.appendChild(p);
ul.appendChild(li);
}
this.resultPanel.appendChild(ul);
this.showPanel();
}
};
PopupGoogleSearch.prototype.onSubmit = function(form)
{
if(form.input.value)
{
this.searcher.execute(form.input.value);
this.moreResults.innerHTML = "<a href='http://www.google.com/search?q=" + form.input.value +"'>更多结果</a>";
}
return false;
};
PopupGoogleSearch.prototype.onClear = function ()
{
this.resultPanel.innerHTML = "";
this.hidePanel();
};
PopupGoogleSearch.prototype.showPanel = function()
{
document.getElementById("searchresultdialog").className = "show";
}
PopupGoogleSearch.prototype.hidePanel = function()
{
document.getElementById("searchresultdialog").className = "hide";
}
function createDiv(html, class)
{
var el = document.createElement("div");
if (html) {
el.innerHTML = html;
}
if (class) { el.className = class; }
return el;
}
function OnLoad()
{
new PopupGoogleSearch();
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>
HTML:
<form id="searcher">
<div id="searchform">Loading</div>
</form>
<div id="searchresultdialog" class="hide">
<p id="hideresult"><a href="#" onclick="document.getElementById('searchresultdialog').className = 'hide';">关闭</a></p>
<div id="searchresult"></div>
<p id="moreresults"></p>
</div>
很多代码都是直接从Google的sample里抄的,我做的工作只是添加了简单的PopUp机制.
代码相当简单,就不用注释了.
全部代码:
CSS:
<style type="text/css">
#searcher { width: 200px; }
#searchresultdialog.show { display:block; background-color:white; width:400px; border:solid 1px #eee; position:absolute; padding:0; }
#searchresultdialog.hide { display:none; }
#searchresult .header { font-size:larger;}
#hideresult, #moreresults { text-align:right; margin-top:0; padding:5px 20px; background:#eee; }
#searchresult li { border-bottom:solid 1px #eee; }
</style>
JavaScript:
<!--注意:下面的这个key应该是每个人/网站/虚拟目录都不同的, 需要从Google申请. 申请过程完全免费, 应该是百分百成功率. 申请地址: http://code.google.com/apis/ajaxsearch/signup.html -->
<script src="http://www.google.com/jsapi?key=ABQIAAAAnbDm87eis9d6TnBZLuDj_BT3fZxwUXmDsFtw544tTyLwi1xKvRQWzjl_UJiFlgsvxapvgIGau2FakA" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
google.load("search", "1");
function PopupGoogleSearch()
{
var searchControl = new google.search.SearchControl();
this.searcher= new google.search.WebSearch();
searchControl.addSearcher(this.searcher);
this.searcher.setResultSetSize(google.search.Search.SMALL_RESULTSET);
this.searcher.setSiteRestriction("deerchao.cnblogs.com");
this.searcher.setNoHtmlGeneration();
this.searcher.setSearchCompleteCallback(this, PopupGoogleSearch.prototype.searchComplete, [this.searcher]);
this.searchForm = new google.search.SearchForm(true, document.getElementById("searchform"));
this.searchForm.setOnSubmitCallback(this, PopupGoogleSearch.prototype.onSubmit);
this.searchForm.setOnClearCallback(this, PopupGoogleSearch.prototype.onClear);
this.resultPanel = document.getElementById("searchresult");
this.moreResults = document.getElementById("moreresults");
}
PopupGoogleSearch.prototype.searchComplete = function (searcher)
{
this.onClear();
if (searcher.results && searcher.results.length > 0)
{
var ul = document.createElement("ul");
for(var i=0; i<searcher.results.length; i++)
{
var result = searcher.results [i];
var li = document.createElement("li");
var a = document.createElement("a");
a.innerHTML = "<p class='header'><a href ='" + result.url + "'>" +result.title +"</a></p>";
li.appendChild(a);
var p = document.createElement("div");
p.innerHTML = result.content;
li.appendChild(p);
ul.appendChild(li);
}
this.resultPanel.appendChild(ul);
this.showPanel();
}
};
PopupGoogleSearch.prototype.onSubmit = function(form)
{
if(form.input.value)
{
this.searcher.execute(form.input.value);
this.moreResults.innerHTML = "<a href='http://www.google.com/search?q=" + form.input.value +"'>更多结果</a>";
}
return false;
};
PopupGoogleSearch.prototype.onClear = function ()
{
this.resultPanel.innerHTML = "";
this.hidePanel();
};
PopupGoogleSearch.prototype.showPanel = function()
{
document.getElementById("searchresultdialog").className = "show";
}
PopupGoogleSearch.prototype.hidePanel = function()
{
document.getElementById("searchresultdialog").className = "hide";
}
function createDiv(html, class)
{
var el = document.createElement("div");
if (html) {
el.innerHTML = html;
}
if (class) { el.className = class; }
return el;
}
function OnLoad()
{
new PopupGoogleSearch();
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>
HTML:
<form id="searcher">
<div id="searchform">Loading</div>
</form>
<div id="searchresultdialog" class="hide">
<p id="hideresult"><a href="#" onclick="document.getElementById('searchresultdialog').className = 'hide';">关闭</a></p>
<div id="searchresult"></div>
<p id="moreresults"></p>
</div>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~