Tieba Utilities

百度贴吧依赖脚本(寂寞的原子的公共库)

Versión del día 28/12/2014. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name	Tieba Utilities
// @namespace	http://gera2ld.blog.163.com/
// @author	Gerald <[email protected]>
// @icon	http://ww2.sinaimg.cn/small/a56031a1gw1emwlbe1c8gj2097097wfa.jpg
// @version	0.8.4
// @description	百度贴吧依赖脚本(寂寞的原子的公共库)
// @homepageURL	http://geraldl.net/userjs/TiebaUtils
// @match	*://*.baidu.com/*
// @include	*.baidu.com/*
// @run-at	document-start
// @grant	none
// ==/UserScript==

if(typeof unsafeWindow=='undefined') unsafeWindow=window;
var shortcuts={},$=function(){
	$=unsafeWindow.$;
	return $.apply(this,arguments);
},links={
	hotkey:'http://geraldl.net/userjs/TiebaUtils#hotkey',
	advanced:'http://geraldl.net/userjs/TiebaAdvanced#advanced',
},utils={
	wait: (function(){
		var objs=[],checking=false;
		function check(){
			var i,j,o,n,p;
			for(i=0;i<objs.length;i++) {
				o=objs[i];p=Object.getOwnPropertyNames(o.callbacks);
				for(j=0;j<p.length;j++) {
					n=p[j];
					if(n in o.obj) {
						p.splice(j,1);j--;
						c=o.callbacks[n];
						delete o.callbacks[n];
						c.forEach(function(f){f(o.obj[n]);});
					}
				}
				if(!p.length) {
					objs.splice(i,1);i--;
				}
			}
			if(objs.length) {
				setTimeout(check,500);
			} else {
				checking=false;
			}
		}
		return function(obj,name,callback) {
			var i,o=null,c;
			for(i=0;i<objs.length;i++)
				if(objs[i].obj===obj) {o=objs[i];break;}
			if(!o) objs.push(o={obj:obj,callbacks:{}});
			c=o.callbacks[name];
			if(!c) c=o.callbacks[name]=[];
			c.push(callback);
			if(!checking) {
				checking=true;
				check();
			}
		};
	})(),
	find: function(selector,ancestor,callback){
		var o=$(selector,ancestor),mo;
		if(o.length) callback(o);
		else if(window.MutationObserver) {
			mo=new MutationObserver(function(changes){
				$.each(changes,function(i,e){
					$.each(e.addedNodes,function(i,o){
						o=$(o).find(selector);
						if(o.length) {
							mo.disconnect();
							callback(o);
						}
					});
				});
			});
			mo.observe(ancestor,{
				childList:true,
				subtree:true,
			});
		} else $(window).load(function(){
			callback($(selector,ancestor));
		});
	},
	hook: function(o,n,a) {
		var f;
		if(o&&(f=o[n])) {
			if(!f.hooked) {
				o[n]=function() {
					var t=this,a=arguments,f=a.callee,r=undefined,_r,i,stop=false;
					f.hookStop=function(){stop=true;};
					for(i=0;i<f.hook_before.length;i++){
						r=f.hook_before[i].apply(t,[f,a]);
						if(stop) return r;
					}
					r=f.hook_func.apply(t,a);
					for(i=0;i<f.hook_after.length;i++){
						_r=f.hook_after[i].apply(t,[f,r,a]);
						if(_r!==undefined) r=_r;
						if(stop) return r;
					}
					return r;
				};
				o[n].hook_func=f;
				f=o[n];
				f.hooked=true;
				f.hook_after=[];
				f.hook_before=[];
			}
			o=f.hook_after;
			if(n=a.after) {
				if(n.concat) f.hook_after=o.concat(n);
				else o.push(n);
			}
			o=f.hook_before;
			if(n=a.before) {
				if(n.concat) f.hook_before=o.concat(n);
				else o.push(n);
			}
		}
	},
	addStyle: function(css,id) {
		var s=document.getElementById(id)||document.createElement('style');
		if(id) s.id=id;if(css) s.innerHTML=css;
		document.head.appendChild(s);
		return $?$(s):s;
	},
	getObj: function(k,d) {
		var r=localStorage.getItem('ge_'+k),u=undefined,v=u;
		if(r) try{v=JSON.parse(r);}catch(e){}
		if(v==u&&d!=u) v=utils.setObj(k,d);
		return v;
	},
	setObj: function(k,v) {localStorage.setItem('ge_'+k,JSON.stringify(v));return v;},
	addButton: function(t,o,m,a) {	// Base Function
		if(!a) a={};
		if(m) {
			var i,k=a.keys;
			if(!k||!k.length) k=['mousedown','mouseup'];
			for(i=0;i<k.length;i++) if(o[k[i]]) {o[k[i]].call(o,m);break;}
		}
		t=$(t);
		if(a.after) o.insertAfter(t.children(a.after));
		else if(a.before) o.insertBefore(t.children(a.before));
		else o.appendTo(t);
		return o;
	},
	addTButton: function(o,m) {	// Add Toolbar Button
		var tb=$('div.edui-btn-toolbar').first();
		if(m) o.click(m);
		return o.appendTo($('<div class="edui-btn edui-btn-bold" unselectable="on" onmousedown="return false">').prependTo(tb));
	},
	addSButton: function(v) {	// Add Big Button
		var b=$('<span class=poster_submit>').insertBefore('.poster_draft_status');
		b.label=$('<em>').html(v).appendTo(b).wrap('<a href="#" class="ui_btn ui_btn_m"><span>');
		return b;
	},
	addPopup: function(E,b,i) {	// Obsoleted
		var p=utils.addRPopup(b,i);
		p.holder=E;
		return p;
	},
	addRPopup: function(b,i,c) { // Add Popup Menu
		var p=$('<div class=ge_panel_p title="">').prependTo(b).click(function(e){
			e.stopPropagation();
			if(['A','BUTTON'].indexOf(e.target.tagName)>=0) e.preventDefault();
		}).hide();
		p.caret=$('<i class=ge_caret>').appendTo(p).html('<i>');
		p.panel=$('<div class=ge_panel>').appendTo(p);
		p.onclose=function(){
			p.hide();$(document).unbind('click',p.onclose);if(c) c(p);
		};
		p.onopen=function(){
			p.show();$(document).click(p.onclose);if(i) i(p);
			var l=Math.min(-50,$(document.body).innerWidth()-b.offset().left-p.panel.outerWidth());
			p.css({'margin-left':l});
			p.caret.css({left:b.width()/2-l-10});
		};
		p.ontoggle=function(e){
			e.preventDefault();
			if(p.is(':visible')) p.onclose(); else setTimeout(p.onopen,0);
		};
		b.click(p.ontoggle);
		return p;
	},
	bindProp: function(obj,prop,key,def,func,evt) {
		obj.prop(prop,utils.getObj(key,def));
		if(!evt) evt=['change'];
		evt.forEach(function(i){obj.bind(i,function(e){utils.setObj(key,this[prop]);if(func) obj.each(function(i,o){func.call(o,e);});});});
		return obj;
	},
	shortcut:function(key,func){
		key=key.toLowerCase();
		var d=[];
		if(key.slice(-2)=='--') {d.push('-');key=key.slice(0,-2).split('-');}
		else key=key.split('-');
		if(key.indexOf('m')>=0||key.indexOf('meta')>=0) d.unshift('m');
		if(key.indexOf('s')>=0||key.indexOf('shift')>=0) d.unshift('s');
		if(key.indexOf('a')>=0||key.indexOf('alt')>=0) d.unshift('a');
		if(key.indexOf('c')>=0||key.indexOf('ctrl')>=0) d.unshift('c');
		key=key.join('-');
		if(func) shortcuts[key]=func; else delete shortcuts[key];
	},
	list: function(lkey,ikey,dnew,def) {	// def===true: not null
		var t={};t.last=0;
		t.load=function(i,nosave){
			if(i==undefined) i=ikey?utils.getObj(ikey,0):0;
			if(i<0||!t.length) i=0; else if(i>=t.length) i=t.length-1;
			if(ikey&&!nosave) utils.setObj(ikey,i);
			t.cur=t.list[t.last=i];
			return t;
		};
		t.push=function(d){if(!d) d=dnew();t.list.push(d);t.save();return t.length-1;};
		t.pop=function(i){var o=t.list.splice(i,1)[0];t.save();t.load(i);return o;}
		t.save=function(){if(lkey) utils.setObj(lkey,t.list);if(ikey) utils.setObj(ikey,t.last);};
		t.list=lkey?utils.getObj(lkey,[]):[];
		t.__defineGetter__('length',function(){return t.list.length;});
		if(!t.length&&def) {if(def.concat) {t.list=def.concat();t.save();} else t.push();}
		return t;
	},
	getLink:function(t,o){
		o=o||{};
		var l=links[t];
		if(l) o.href=l;
		if(!o['target']) o.target='_blank';
		t=o.html||'';delete t.html;l=['<a'];
		for(i in o) l.push(i+'="'+o[i]+'"');
		return l.join(' ')+'>'+t+'</a>';
	},
	popup:{	// Popup Window
		dialog:null,
		className:'ge_popup',
		show:function(o){
			var t=this,d=t.dialog;
			t.hide();t.obj=o;
			d.className=o.className||'';
			d.classList.add(t.className);
			d.innerHTML=o.html;
			if(o.init) o.init(d);
			d.style.display='block';
			d.style.top=(innerHeight-d.offsetHeight)/2+'px';
			d.style.left=(innerWidth-d.offsetWidth)/2+'px';
			document.addEventListener('click',t._hide=t.hide.bind(this),false);
		},
		hide:function(){
			var t=this,o=t.obj,d=t.dialog;
			if(o) {
				if(o.dispose) o.dispose(d);
				d.innerHTML='';d.className=t.className;
				d.style.display='none';t.obj=null;
				document.removeEventListener('click',t._hide,false);
			}
		},
	},
};
document.addEventListener('keydown',function(e){
	if(e.target==document.body){
		var k=[],f;
		if(e.ctrlKey) k.push('c');
		if(e.altKey) k.push('a');
		if(e.shiftKey) k.push('s');
		if(e.metaKey) k.push('m');
		k.push(String.fromCharCode(e.keyCode));
		k=k.join('-').toLowerCase();
		if(f=shortcuts[k]) {e.preventDefault();f();}
	}
},false);
window.addEventListener('DOMContentLoaded',function(){
	var d=document.createElement('div');document.body.appendChild(d);
	d.addEventListener('click',function(e){e.stopPropagation();},false);
	utils.popup.dialog=d;
	utils.addStyle('\
.ge_x{clear:both;}\
.ge_popup{display:none;z-index:10006;font:normal normal 400 12px/18px 宋体;position:fixed;background:white;border:1px solid silver;box-shadow:5px 5px 7px #333;text-align:left;}\
.ge_mask{background:rgba(0,0,0,0.6);position:fixed;top:0;bottom:0;left:0;right:0;z-index:1001;display:none;}\
.ge_panel_p{position:relative;z-index:1006;}\
.ge_panel{position:absolute;background:#eee;border:1px solid black;padding:10px;border-radius:5px;bottom:0;white-space:nowrap;}\
.ge_caret,.ge_caret>i{position:absolute;border:solid;border-color:black transparent;border-width:8px 8px 0;z-index:889;}\
.ge_caret>i{border-top-color:#eee;top:-9px;left:-8px;}\
.ge_sbtn{background:#77f;color:white;border-radius:3px;border:1px solid;border:none;margin:2px;cursor:pointer;text-align:center;}\
span.ge_sbtn{padding:2px 3px;}\
.ge_disabled{background:gray;cursor:default;}\
.ge_rsep{margin-right:10px;}\
.ge_opt{padding:20px;border-radius:5px;}\
.ge_opt fieldset{border:1px solid silver;border-radius:5px;padding:5px;}\
.ge_opt textarea{min-height:100px;width:100%;}\
','ge_css');
},false);
var gkey='__ge_firefox',ff=unsafeWindow[gkey];
if(!ff) ff=unsafeWindow[gkey]={};
ff['utils']=utils;