Last active
October 13, 2015 21:19
-
-
Save firedfox/4257541 to your computer and use it in GitHub Desktop.
search engine matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------- | |
// 从这里开始是web端同步来的数据 | |
// 后端应该也需要从web端同步过来 | |
//----------------------------- | |
// 站点配置 | |
var c = { | |
// 搜索引擎配置 | |
se: [ | |
[1,'baidu.com','word|wd|w',1,'news,tieba,zhidao,mp3,image,video,hi,baike,wenku,opendata,jingyan'], | |
[2,'google.com','q',0,'tbm=isch,tbm=vid,tbm=nws|source=newssearch,tbm=blg,tbm=frm'], | |
[4,'sogou.com','query',1,'news,mp3,pic,v,gouwu,zhishi,blogsearch'], | |
[6,'search.yahoo.com','p',1,'news,images,video'], | |
[7,'yahoo.cn','q',1,'news,image,music'], | |
[8,'soso.com','w',1,'image,video,music,sobar,wenwen,news,baike'], | |
[11,'youdao.com','q',1,'image,news,,mp3,video'], | |
[13,'bing.com','q',2,'images,videos,news'], | |
[14,'so.com','q',1,'video,news'], | |
[15,'jike.com','q',1,'news,image,video'], | |
[16,'qihoo.com','kw',0,''], | |
[17,'etao.com','q',1,'s,8'], | |
[18,'soku.com','keyword',2,'a'] | |
] | |
}; | |
//----------------------------- | |
// 从这里开始是js的逻辑 | |
//----------------------------- | |
// so.com和360.cn共用同一个搜索引擎id | |
// 后端无法支持,只能在这里写死 | |
// 另外360视频搜索的域名是so.v.360.cn,太2了,只能单独判断 | |
c.se.push([14,'so.360.cn','q',1,',news']); | |
c.se.push([14,'v.360.cn','q',1,'so']); | |
/** | |
* 获取二级搜索引擎id | |
* @param {Array} seInfo 某个搜索引擎的信息 | |
* @return {number} 二级搜索引擎id | |
* | |
* 匹配类型参数说明 | |
* 0:位置不固定,正则匹配,例如www.google.com/search?tbm=isch | |
* 可以用|分隔多个匹配项,例如谷歌新闻 | |
* 1:sub.domain.com形式,例如news.baidu.com | |
* 2:domain.com/sub形式,例如www.bing.com/images | |
* 符合1或2的尽量用1或2,都不符合的才用0,这样会更准确,减少误判 | |
*/ | |
var getSSE = function(seInfo) { | |
var sse = 0; | |
var prefix = (seInfo[3] == 2 ? seInfo[1] + '\\/' : ''); | |
var postfix = (seInfo[3] == 1 ? '\\.' + seInfo[1] : ''); | |
var sub = seInfo[4].split(','); | |
for (var i = 0, l = sub.length; i < l; i++) { | |
if (sub[i] !== '' && new RegExp(prefix + sub[i] + postfix).test(document.referrer)) { | |
sse = i + 1; | |
break; | |
} | |
} | |
return sse; | |
}; | |
// 判断是否来源于搜索引擎 | |
for (var i = 0, l = c.se.length; i < l; i++) { | |
if (new RegExp('(^|\\.)' + c.se[i][1].replace(/\./g, '\\.')).test(baidu.url.getHostname(document.referrer))) { // 匹配域名 | |
var sw = baidu.url.getQueryValue(document.referrer, c.se[i][2]) || ''; | |
// 匹配搜索词 | |
// google.com so.com/360.cn etao.com都是例外,可能会没有搜索词,但也要判断成搜索引擎 | |
if (!sw && c.se[i][0] != 2 && c.se[i][0] != 14 && c.se[i][0] != 17) { | |
continue; | |
} | |
// cpro虽然位于baidu.com大域下,但并不是搜索产品,不应该显示搜索词 | |
if (c.se[i][0] == 1 && document.referrer.indexOf('cpro.baidu.com') > -1) { | |
sw = ''; | |
} | |
this.tags['se'] = c.se[i][0]; // 搜索引擎id | |
this.tags['sse'] = getSSE(c.se[i]); // 二级搜索引擎id | |
this.tags['sw'] = sw; // 关键词 | |
return 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment