From 516cebccf9714f209cf797a8e1ce01d4f7c65c20 Mon Sep 17 00:00:00 2001 From: abiu <abiu@appsnt.com> Date: Mon, 27 Apr 2020 15:09:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=AE=BF=E9=97=AEgoogle=E6=97=B6=E5=80=99,=E7=94=A8bi?= =?UTF-8?q?ng=20=E4=BB=A3=E6=9B=BFgoogle=20=E6=9D=A5=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/search.js | 6 +++++- test/controllers/search.test.js | 8 ++++++++ views/layout.html | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/controllers/search.js b/controllers/search.js index d41c378208..bf1bd01ef0 100644 --- a/controllers/search.js +++ b/controllers/search.js @@ -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); + } }; diff --git a/test/controllers/search.test.js b/test/controllers/search.test.js index dcfdf22e46..2836c59540 100644 --- a/test/controllers/search.test.js +++ b/test/controllers/search.test.js @@ -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); + }); + }); }); diff --git a/views/layout.html b/views/layout.html index 7dfbcef1d1..353c1aca82 100644 --- a/views/layout.html +++ b/views/layout.html @@ -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> @@ -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>