因为最近一阵微搜索半死不活,就连它自己官网上的站内搜索也挂了,而swiftype对中文汉字的识别又太差,所以想找一个靠谱的站内搜索,但是找来找去没找到,于是只好用自带的谷歌,但是谷歌在国内又是“你懂的”的状态。于是就有了这篇教程:修改hexo站内搜索为Google谷歌镜像搜索。

如果以前安装过微搜索,那么先对《为Hexo添加微搜索》其中的步骤进行逆操作:删除tinysou.ejs文件,删除相关文件中的<%- partial('tinysou') %>以及tinysou: true这几行代码。然后编辑D:\hexo\themes\你的主题目录\layout\_partial目录下面的header.ejs文件,把<div id="search-form-wrap">标签恢复成如下样式:

1
2
3
<div id="search-form-wrap">
<%- search_form({button: '&#xF002;'}) %>
</div>

然后我们先去github的hexo-theme-landscape,在This repository中搜索关键字google sarch,但是没发现什么有价值的信息,于是转到大本营hexo搜搜看,结果很令人兴奋啊,于是知道了关键文件是search_form.js,马上打开everything,搜索search_form.js,得知在目录D:\hexo\node_modules\hexo\lib\plugins\helper下,这个路径也和网上搜索到的lib/plugins/helper/search_form.js路径类似,于是这个文件就是我们要修改的。

那我们使用Notepad++打开D:\hexo\node_modules\hexo\lib\plugins\helper\search_form.js文件,在第12行return标签后面,把google.com替换成你自己找的谷歌google镜像域名,我替换成igogo.me,然后我想把占位符从英文的“search”改成中文的“搜索”,于是修改第13行的placeholder="'placeholder="搜索"',这样就大功告成了。

修改后的代码如下:

1
2
3
4
5
return '<form action="//igogo.me/search" method="get" accept-charset="UTF-8" class="' + className + '">' +
'<input type="search" name="q" results="0" class="' + className + '-input"' + (text ? ' placeholder="搜索"' + text + '"' : '') + '>' +
(button ? '<button type="submit" class="' + className + '-submit">' + (typeof button === 'string' ? button : text) + '</button>' : '') +
'<input type="hidden" name="sitesearch" value="' + config.url + '">' +
'</form>';

最后只要hexo clean && hexo g && hexo d重新部署上传就OK了。

然后每隔一段时间搜索最新的google镜像站,把域名替换下(去掉或不去掉前面的www都可以,比如//www.guge.link/search),保证搜索服务可用。