Skip to content

Instantly share code, notes, and snippets.

@Teraflopst
Created March 23, 2019 19:39
Show Gist options
  • Save Teraflopst/a61b75ea6e3b6309938f2bd76451fc0a to your computer and use it in GitHub Desktop.
Save Teraflopst/a61b75ea6e3b6309938f2bd76451fc0a to your computer and use it in GitHub Desktop.
切换搜索引擎搜索关键字 Switch Search Engine
// 2019-03-24
// Switch Search Engine
var o_baidu = {
url_id: ".baidu.",
url_q: "wd=",
url_s: "https://www.baidu.com/s?wd=",
};
var o_google = {
url_id: ".google.",
url_q: "q=",
url_s: "https://www.google.com/search?q=",
};
var o_bing = {
url_id: ".bing.",
url_q: "q=",
url_s: "https://www.bing.com/search?q=",
};
var o_baidu_2 = {
url_id: ".baidu.",
url_q: "word=",
url_s: "https://www.baidu.com/s?wd=",
};
var se = [o_baidu, o_google, o_bing, o_baidu_2];
// Main Function
function SwitchSE(q) {
for (var i=0; i<se.length; i++)
{
if ((q.indexOf(se[i].url_id) != -1)&&(q.indexOf(se[i].url_q) != -1)) {
var kw = "", url = "";
var regstr = "(" + se[i].url_q + ")" + "(\S*)";
var reg =new RegExp(regstr);
var str = q;
var res = str.substring(str.match(reg).index, str.length);
var idx = res.indexOf("&");
if (idx != -1) {
kw = res.substring(res.indexOf("=")+1,idx);
} else {
kw = res.substring(res.indexOf("=")+1,res.length);
}
if ((se[i].url_id == ".baidu.") || (se[i].url_id == ".bing.")) {
url = o_google.url_s + kw;
} else {
url = o_baidu.url_s + kw;
}
return url;
}
}
}
// Test demo
var arg1 = 'https://www.google.com/search?newwindow=1&q=hello'
var arg2 = 'https://www.google.com/search?q=hello';
var arg3 = 'https://www.google.com/search?q=hello&oq=world&sourceid=chrome';
var arg4 = 'https://www.google.co.jp/search?q=hello';
var arg5 = 'https://www.baidu.com/s?ie=utf-8&wd=hello';
var arg6 = 'https://www.baidu.com/s?wd=hello+world'
var arg7 = 'https://www.baidu.com/s?word=hello';
var arg8 = 'https://www.bing.com/search?q=hello&go=Search&qs=ds&form=QBRE';
var arg9 = 'https://www.baidu.com/s?ie=utf-8&word=hello+world&tn=baiduhome_pg';
// Console Output
console.log(SwitchSE(arg1));
console.log(SwitchSE(arg2));
console.log(SwitchSE(arg3));
console.log(SwitchSE(arg4));
console.log(SwitchSE(arg5));
console.log(SwitchSE(arg6));
console.log(SwitchSE(arg7));
console.log(SwitchSE(arg8));
console.log(SwitchSE(arg9));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment