Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当客户端无法访问google时候,用bing 代替google 来搜索 #1106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
exports.index = function (req, res, next) {
var q = req.query.q;
q = encodeURIComponent(q);
res.redirect('https://www.google.com.hk/#hl=zh-CN&q=site:cnodejs.org+' + q);
if(req.query.notGG){
res.redirect('https://cn.bing.com/search?q=site:cnodejs.org+' + q)
}else{
res.redirect('https://www.google.com.hk/#hl=zh-CN&q=site:cnodejs.org+' + q);
}
};
8 changes: 8 additions & 0 deletions test/controllers/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ describe('test/controllers/search.test.js', function () {
done(err);
});
});
it('should redirect to bing search when google is not reacheable', function (done) {
request.get('/search').query({q: 'node 中文',notGG:'1'})
.expect(302)
.end(function (err, res) {
res.headers['location'].should.equal('https://cn.bing.com/search?q=site:cnodejs.org+node%20%E4%B8%AD%E6%96%87');
done(err);
});
});
});
26 changes: 26 additions & 0 deletions views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@

<form id='search_form' class='navbar-search' action="/search">
<input type='text' id='q' name='q' class='search-query span3' value=''/>
<input type='hidden' id='notGG' name='notGG' value='' />
</form>
<ul class='nav pull-right'>
<li><a href='/'>首页</a></li>
Expand Down Expand Up @@ -146,8 +147,33 @@
ga('create', '<%-config.google_tracker_id%>', 'auto');
ga('send', 'pageview');
</script>

<% } %>

<script>
function checkSiteReachable(url,callback) {
var timer = setTimeout(function(){
callback(false);
},2000)

var img = document.createElement("img");
img.onload = function() {
clearTimeout(timer);
callback(true);
}
img.onerror = function() {
clearTimeout(timer);
callback(false);
}
img.src = url+"/favicon.ico";
}
checkSiteReachable('https://www.google.com',function(reachable){
if(!reachable){
document.querySelector('#notGG').value = '1';
}
})
</script>

<% if (config.cnzz_tracker_id) { %>
<div style="display:none;">
<script src="//s95.cnzz.com/z_stat.php?id=<%- config.cnzz_tracker_id %>&web_id=<%- config.cnzz_tracker_id %>" language="JavaScript"></script>
Expand Down