Tieba Utilities

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

Per 13-07-2014. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name	Tieba Utilities
// @namespace	http://gera2ld.blog.163.com/
// @author	Gerald <[email protected]>
// @icon	http://s.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
// @version	0.8.1
// @description	百度贴吧依赖脚本(寂寞的原子的公共库)
// @homepageURL	http://geraldl.ml/userjs/TiebaUtils
// @match	*://*.baidu.com/*
// @include	*.baidu.com/*
// @run-at document-start
// @grant none
// ==/UserScript==

var _wait={},_wait_obj=[],shortcuts={},$=function(){
	$=unsafeWindow.$;
	return $.apply(this,arguments);
},links={
	hotkey:'http://geraldl.ml/userjs/TiebaUtils#hotkey',
	advanced:'http://geraldl.ml/userjs/TiebaAdvanced#advanced',
},utils={
	wait: function(o,n,callback,t) {
		function check(){
			if(o[n]) {
				delete _wait[i];
				c.forEach(function(i){i(o[n]);});
			} else setTimeout(check,t);
		}
		var i=_wait_obj.indexOf(o);
		if(i<0) {
			i=_wait_obj.length;
			_wait_obj.push(o);
		}
		i+='.'+n;
		var c=_wait[i];
		if(c) t=0;
		else {
			if(!t) t=500;
			c=_wait[i]=[];
		}
		c.push(callback);
		if(t) check();
	},
	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) { // 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);};
		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,
		show:function(o){
			var t=this,d=t.dialog;
			t.hide();t.obj=o;
			d.className=o.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.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');
	d.id='ge_popup';document.body.appendChild(d);
	d.addEventListener('click',function(e){e.stopPropagation();},false);
	utils.popup.dialog=d;
	utils.addStyle('\
.ge_x{clear:both;}\
#ge_popup{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:888;}\
.ge_panel{position:absolute;background:#eee;border:1px solid black;padding:10px;border-radius:5px;z-index:888;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||window)['utils']=utils;