Baidu Multiuser

百度马甲切换

当前为 2014-05-17 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name	Baidu Multiuser
// @namespace	http://gera2ld.blog.163.com/
// @author	Gerald <[email protected]>
// @icon	http://s.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
// @version	1.4.3
// @description	百度马甲切换
// @homepage	http://geraldl.ml/UserJS/BaiduMultiuser
// @match	*://*.baidu.com/*
// @include	*.baidu.com/*
// @exclude http://developer.baidu.com/*
// @exclude http://web.im.baidu.com/*
// @require	https://greasyfork.org/scripts/144/code.user.js
// @grant	GM_getValue
// @grant	GM_setValue
// @grant	GM_openInTab
// @grant	GM_registerMenuCommand
// ==/UserScript==

function getValue(k,d){
	var o=GM_getValue(k,'');
	if(o&&typeof o=='string') o=JSON.parse(o);
	return o||d;
}
function setValue(k,v){GM_setValue(k,JSON.stringify(v));}
function switchUser(s,p){
	var d=new Date();if(s) d.setTime(16094e8); else s='';
	document.cookie='BDUSS='+s+';domain=baidu.com;path=/;expires='+d.toGMTString();
	if(typeof p=='function') p();
	else if(typeof p=='string') location.replace(p);
	else location.reload();
}
function initLoc(){
		gu.right=gu._right=gu.parentNode.offsetWidth-gu.offsetWidth-gu.offsetLeft;
		gu.top=gu._top=gu.offsetTop;
}
function saveUsers(){setValue('ge_users',users);}
function saveLoc(){setValue('ge_users_loc',{right:gu.right,top:gu.top});}
function userManage(e,p,o){
	e.preventDefault();o=e.target;e=o.parentNode;p=e.parentNode;
	if(o.tagName=='A') {
		if(e==p.firstChild) switchUser();
		else if(e==p.lastChild) GM_openInTab('http://wappass.baidu.com/?login&u=http://www.baidu.com');	// 方便处理
		else switchUser(users[o.innerText||o.textContent]);
	} else if(o.tagName=='SPAN') {
		o=o.previousSibling;delete users[o.innerText||o.textContent];
		setTimeout(saveUsers,0);p.removeChild(e);
	}
}
function locate(l){
	if(l) {
		gu.right=l&&!isNaN(l.right)?l.right:100;
		gu.top=l&&!isNaN(l.top)?l.top:100;
	}
	gu.style.right=gu.right+'px';
	gu.style.top=gu.top+'px';
}
function mousemove(e){
	e.preventDefault();e.stopPropagation();
	var l={right:gu._right+gu.x-e.pageX,top:gu._top+e.pageY-gu.y};
	locate(l);
}
function pinUpdate(){
	symbol.innerHTML=gu.pin?'●':'○';
	symbol.setAttribute('title',gu.pin?'固定在页面上':'固定在屏幕上');
	gu.style.position=gu.pin?'absolute':'';
}
function pin(){
	initLoc();
	if(gu.pin)	// fixed => absolute
		gu.top+=window.pageYOffset;
	else	// absolute => fixed
		gu.top-=window.pageYOffset;
	pinUpdate();
	locate();
	saveLoc();
}
function buildMenu(){
	utils.addStyle('\
#ge_u{display:block;padding:10px;text-align:left;}\
#ge_u .ge_h{display:none;}\
#ge_u{z-index:10006;font:normal normal 400 12px/18px 宋体;position:fixed;}\
#ge_u>span{background:white;color:blue;border-radius:3px;border:1px solid #c0c0c0;padding:2px;cursor:pointer;}\
#ge_u>div{position:relative;margin-top:3px;}\
#ge_u>div>*{position:absolute;}\
.ge_u{background:white;border:1px solid silver;box-shadow:5px 5px 7px #333;}\
.ge_u{width:120px;max-height:400px;overflow-x:hidden;overflow-y:auto;}\
.ge_u>li{position:relative;display:block;padding:2px 20px 4px 6px;}\
.ge_u>li:hover{background:lightgray;}\
.ge_u a{display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}\
.ge_u span{position:absolute;top:0;right:0;color:white;background:#77f;border-radius:3px;margin:2px;cursor:pointer;padding:2px;}\
.ge_u span:hover{background:red;}\
');
	gu=document.createElement('div');gu.id='ge_u';
	gu.innerHTML='<span>马甲<span></span></span><div><ul class="ge_u ge_h"></ul></div>';
	gu.style.display=getValue('float','');
	ul=gu.querySelector('ul');ul.onclick=userManage;
	symbol=gu.firstChild.lastChild;
	gu.pin=!!getValue('ge_pin');pinUpdate();
	symbol.onclick=function(){setValue('ge_pin',gu.pin=!gu.pin);pin();};
	gu.onmouseover=function(e){
		if(this.contains(e.relatedTarget)) return;
		ul.classList.remove('ge_h');
		if(gu.offsetLeft+gu.firstChild.offsetLeft+ul.offsetWidth<=document.body.offsetWidth) ul.style.pixelLeft=0;
		else ul.style.pixelLeft=document.body.offsetWidth-gu.offsetLeft-gu.firstChild.offsetLeft-ul.offsetWidth;
	};gu.onmouseout=function(e){if(!this.contains(e.relatedTarget)) ul.classList.add('ge_h');};
	var d=getValue('ge_users_loc',{});if(typeof d=='string') d=JSON.parse(d);
	document.body.appendChild(gu);locate(d);gu.moving=false;
	gu.firstChild.onmousedown=function(e){
		e.preventDefault();e.stopPropagation();
		if(e.target!=gu.firstChild||gu.moving) return;gu.moving=true;
		initLoc();
		gu.x=e.pageX;
		gu.y=e.pageY;
		document.addEventListener('mousemove',mousemove,false);
	};
	gu.onmouseup=function(e){
		if(!gu.moving) return;gu.moving=false;
		e.preventDefault();e.stopPropagation();
		document.removeEventListener('mousemove',mousemove,false);
		saveLoc();
	};
	initMenu();
}
function initMenu(){
	d=[];
	if(users) {
		d.push('<li><a href=#>未登录状态</a></li>');
		for(var i in users) {
			if(!i) {delete users[i];continue;}
			d.push('<li><a href=#>'+i.replace(/&/g,'&amp;').replace(/</g,'&lt;')+'</a><span>删</span></li>');
		}
	} else
		d.push('<li title="未检测到Cookie,请任意添加一个马甲或自行去除对应Cookie的HttpOnly属性。" style="color:gray">未检测到Cookie</li>');
	d.push('<li><a href=#>添加马甲</a></li>');
	ul.innerHTML=d.join('');
}
function showUsers(){
	utils.popup.show({
		html:ul.innerHTML,
		className:'ge_u',
		init:function(d){ul.style.display='none';d.onclick=userManage;},
		dispose:function(d){ul.style.display='';d.onclick=null;initMenu();},
	});
}
function showOptions(){
	utils.popup.show({
		html:'\
<h3>设置 - 百度马甲切换脚本</h3>\
<label><input type=checkbox id=gu_showfloat>显示悬浮图标</label><br>\
<label>切换马甲快捷键:'+utils.getLink('hotkey',{title:'帮助',html:'(?)'})+'<input id=gu_shortcut></label><br>\
<fieldset><legend>马甲数据 <button id=gu_import>导入</button> <button id=gu_export>导出</button> \
<a title="复制数据到以下文本框然后点击导入即可导入数据。\n点击导出后复制数据文本即可用于导入。">(?)</a></legend>\
<textarea id=gu_data></textarea></fieldset>\
',
		className:'ge_opt',
		init:function(d){
			var t=d.querySelector('#gu_showfloat');
			t.checked=getValue('float')!='none';
			t.onchange=function(){
				gu.style.display=this.checked?'':'none';
				setValue('float',gu.style.display);
			};
			t=d.querySelector('#gu_shortcut');t.value=shortcut;
			t.onchange=function(){
				if(shortcut) utils.shortcut(shortcut);
				shortcut=this.value;bindShortcut();
				setValue('shortcut',shortcut);
			};
			t=d.querySelector('#gu_data');t.onclick=function(){this.select();};
			d.querySelector('#gu_import').onclick=function(o){
				try{o=JSON.parse(t.value);}catch(e){o=null;}
				if(o&&typeof o=='object') {
					for(var i in o) users[i]=o[i];
					saveUsers();initMenu();alert('导入成功!');
				} else alert('导入失败!');
			};
			d.querySelector('#gu_export').onclick=function(){t.value=JSON.stringify(users);};
		}
	});
}
function bindShortcut(){if(shortcut) utils.shortcut(shortcut,showUsers);}
function init(d,m){
	users=getValue('ge_users',{});
	shortcut=getValue('shortcut','s-m');
	if((d=PageData)&&d.user&&d.user.is_login&&d.user.name) d=d.user.name;	// 贴吧
	else if(d=unsafeWindow.s_session) d=d.username;	// 百度首页
	if(d) {
		m=document.cookie.match(/BDUSS=(.*?)(;|$)/);
		if(m) {users[d]=m[1];saveUsers();}
		else users=null;
	}
	GM_registerMenuCommand('百度马甲设置',showOptions);
	buildMenu();bindShortcut();
}
var menu,gu,ul,symbol,options,users,shortcut;
if(window.top===window&&document.head) init();