[JavaScript] 纯文本查看 复制代码 <script>
var whiteList = {
bjh: 'passport.baidu.com'
};
function getUrlParam(key, loc) {
loc = loc || window.location;
var search = loc.search;
var skey = key.replace(/[A-Z]/g, function ($0) {
return '_' + $0.toLowerCase();
});
var re = new RegExp('[?|&](' + key + '|' + skey + ')=(.*?)(&|#|$)', 'i');
var r = search.match(re);
return (r && decodeURIComponent(r[2])) || null;
}
function request(option) {
var r;
if (window.XMLHttpRequest) {
r = new XMLHttpRequest();
} else {
r = new ActiveXObject('Microsoft.XMLHTTP');
}
r.onreadystatechange = function () {
if (r.readyState === 4) {
if (r.status === 200) {
typeof option.success === 'function' && option.success(r.responseText);
} else {
typeof option.fail === 'function' && option.fail(r.responseText);
}
} else {}
};
r.open(option.method || 'GET', option.url);
for (var p in option.headers) {
if (option.headers[p]) {
r.setRequestHeader(p, option.headers[p]);
}
}
r.send();
}
function jsonp(option) {
var time = new Date().getTime();
var params = [];
for (var p in option.data) {
params.push(p + '=' + option.data[p]);
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = option.url + '?callback=json_callback_' + time + '&' + params.join('&');
document.getElementsByTagName('head')[0].appendChild(script);
window['json_callback_' + time] = function (res) {
clean();
typeof option.success === 'function' && option.success(res);
};
script.onerror = function () {
clean();
typeof option.fail === 'function' && option.fail();
};
function clean() {
delete window['json_callback_' + time];
document.getElementsByTagName('head')[0].removeChild(script);
}
}
function getCookie(cname) {
var name = cname + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca.trim();
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
document.onload = (function () {
// 此处大坑: url输入baijiahao.baidu.com的时候(点了登录之后) 后端会打到这个页面 前端获取stoken并给后端, 同时pass将stoken写在cookie中, stoken没获取到我们认为其是异常用户, 所以登出
// bjh登录是passport.baidu.com域名, theme(包括但不限于beijing)登录是wappass.baidu.com域名, 获取stoken的域名需一直, 所以有下面的判断, theme在cookie中设置
var hostName = 'passport.baidu.com';
var theme = getCookie('theme').trim();
if (false) {
hostName = whiteList[theme] || 'wappass.baidu.com';
}
jsonp({
url: 'https://' + hostName + '/v3/login/api/auth/',
data: {
return_type: 2, //eslint-disable-line
tpl: 'bjh'
},
success: function (res) {
if (+res.errno === 0 && res.stoken) {
request({
url: '/pcui/home/dealtoken',
headers: {
bjh: res.stoken
},
success: function () {
var linkUrl = getUrlParam('u');
var elem = document.createElement('a');
elem.href = linkUrl;
var linkOrigin = elem.protocol + '//' + elem.host;
if (!/^(http|https):\/\/.+\.baidu\.com$/.test(linkOrigin)) {
linkUrl = 'https://baijiahao.baidu.com';
}
window.location.href = linkUrl;
}
});
} else {
window.location.href = 'https://passport.baidu.com/?logout&tpl=bjh&u=' + window.location.origin;
}
}
});
})();
</script>
cookies:PASSID=fzHSUh;UBI=fi_PncwhpxZ%7ETaJc0-OQtOnAK2RjHGRhtK2hII2PdvimpytiR9ZafDVj7MgLJBVcA4oz2qOLFdjx1iAKpYqEeHAE1px3Ffv7cLQzamWkKDa6BwkXi9z7gB0qKngC5tHd9VaqrLKCDDbGeb3FkrxXSRoOcYUZw__;STOKEN=eebf1e69497c9579bd389424d7aed6566107f98541233805bfa1e8cddf086a20;NEWUSER=1;BDUSS=WVMLTdLdFZXZ05mTzBUWG9Qck44T0F4a1NIQzY3UDNHWlpIR3gta1lEQXczc1ZjQVFBQUFBJCQAAAAAAAAAAAEAAACTxN7y27rZvL~YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADBRnlwwUZ5cR;PTOKEN=61cf1c7b6db63a7356879c8f7bc59a24;BAIDUID=21038C09119FA40AD8E92B659CCC9F5C:FG=1
这个cookies 登录百家好像登录不上 什么问题
|