Tieba Utilities

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

目前為 2014-05-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Tieba Utilities
  3. // @namespace http://gera2ld.blog.163.com/
  4. // @author Gerald <gera2ld@163.com>
  5. // @icon http://s.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
  6. // @version 0.7
  7. // @description 百度贴吧依赖脚本(寂寞的原子的公共库)
  8. // @homepage http://userscripts.org/scripts/show/400102
  9. // @match *://*.baidu.com/*
  10. // @include *.baidu.com/*
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. function initShortcuts(){
  15. shortcuts={};
  16. document.addEventListener('keydown',function(e){
  17. if(e.target==document.body){
  18. var k=[],f;
  19. if(e.ctrlKey) k.push('c');
  20. if(e.altKey) k.push('a');
  21. if(e.shiftKey) k.push('s');
  22. if(e.metaKey) k.push('m');
  23. k.push(String.fromCharCode(e.keyCode));
  24. k=k.join('-').toLowerCase();
  25. if(f=shortcuts[k]) {e.preventDefault();f();}
  26. }
  27. },false);
  28. }
  29. var _wait={},_wait_obj=[],shortcuts=null,$=function(){
  30. $=unsafeWindow.$;
  31. return $.apply(this,arguments);
  32. },links={
  33. hotkey:'http://geraldl.tk/UserJS/TiebaUtils#hotkey',
  34. advanced:'http://geraldl.tk/UserJS/TiebaAdvanced#advanced',
  35. },utils={
  36. wait: function(o,n,callback,t) {
  37. function check(){
  38. if(o[n]) {
  39. delete _wait[i];
  40. c.forEach(function(i){i(o[n]);});
  41. } else setTimeout(check,t);
  42. }
  43. var i=_wait_obj.indexOf(o);
  44. if(i<0) {
  45. i=_wait_obj.length;
  46. _wait_obj.push(o);
  47. }
  48. i+='.'+n;
  49. var c=_wait[i];
  50. if(c) t=0;
  51. else {
  52. if(!t) t=500;
  53. c=_wait[i]=[];
  54. }
  55. c.push(callback);
  56. if(t) check();
  57. },
  58. hook: function(o,n,a) {
  59. var f;
  60. if(o&&(f=o[n])) {
  61. if(!f.hooked) {
  62. o[n]=function() {
  63. var t=this,a=arguments,f=a.callee,r=undefined,_r,i,stop=false;
  64. f.hookStop=function(){stop=true;};
  65. for(i=0;i<f.hook_before.length;i++){
  66. r=f.hook_before[i].apply(t,[f,a]);
  67. if(stop) return r;
  68. }
  69. r=f.hook_func.apply(t,a);
  70. for(i=0;i<f.hook_after.length;i++){
  71. _r=f.hook_after[i].apply(t,[f,r,a]);
  72. if(_r!==undefined) r=_r;
  73. if(stop) return r;
  74. }
  75. return r;
  76. };
  77. o[n].hook_func=f;
  78. f=o[n];
  79. f.hooked=true;
  80. f.hook_after=[];
  81. f.hook_before=[];
  82. }
  83. o=f.hook_after;
  84. if(n=a.after) {
  85. if(n.concat) f.hook_after=o.concat(n);
  86. else o.push(n);
  87. }
  88. o=f.hook_before;
  89. if(n=a.before) {
  90. if(n.concat) f.hook_before=o.concat(n);
  91. else o.push(n);
  92. }
  93. }
  94. },
  95. addStyle: function(css,id) {
  96. var s=document.getElementById(id)||document.createElement('style');
  97. if(id) s.id=id;if(css) s.innerHTML=css;
  98. document.head.appendChild(s);
  99. return $?$(s):s;
  100. },
  101. getObj: function(k,d) {
  102. var r=localStorage.getItem('ge_'+k),u=undefined,v=u;
  103. if(r) try{v=JSON.parse(r);}catch(e){}
  104. if(v==u&&d!=u) v=utils.setObj(k,d);
  105. return v;
  106. },
  107. setObj: function(k,v) {localStorage.setItem('ge_'+k,JSON.stringify(v));return v;},
  108. addButton: function(t,o,m,a) { // Base Function
  109. if(!a) a={};
  110. if(m) {
  111. var i,k=a.keys;
  112. if(!k||!k.length) k=['mousedown','mouseup'];
  113. for(i=0;i<k.length;i++) if(o[k[i]]) {o[k[i]].call(o,m);break;}
  114. }
  115. t=$(t);
  116. if(a.after) o.insertAfter(t.children(a.after));
  117. else if(a.before) o.insertBefore(t.children(a.before));
  118. else o.appendTo(t);
  119. return o;
  120. },
  121. addTButton: function(o,m) { // Add Toolbar Button
  122. var tb=$('div.edui-btn-toolbar').first();
  123. if(m) o.click(m);
  124. return o.appendTo($('<div class="edui-btn edui-btn-bold" unselectable="on" onmousedown="return false">').prependTo(tb));
  125. },
  126. addSButton: function(v) { // Add Big Button
  127. var b=$('<a href="#" class="ui_btn ui_btn_m poster_submit">').insertBefore('.poster_draft_status').html('<span><em>'+v+'</em></span>');
  128. return b;
  129. },
  130. addPopup: function(E,b,i) { // Add Popup Menu
  131. var p=$('<div class=ge_panel_p title="">').appendTo(E).click(function(e){
  132. e.stopPropagation();
  133. if(['A','BUTTON'].indexOf(e.target.tagName)>=0) e.preventDefault();
  134. }).hide(),bfilter='.ui_btn_m,.edui-btn';
  135. p.holder=E;
  136. p.caret=$('<i class=ge_caret>').appendTo(p).html('<i></i>');
  137. p.panel=$('<div class=ge_panel>').appendTo(p);
  138. p.onclose=function(){p.hide();$(document).unbind('click',p.onclose);};
  139. p.onopen=function(){
  140. p.show();$(document).click(p.onclose);if(i) i(p);
  141. var o1=E.offset(),o2=p.button.offset(),op=p.offsetParent(),
  142. l=Math.min(o2.left-o1.left-50,$(document.body).innerWidth()-o1.left-p.panel.outerWidth()-20);
  143. p.css({'margin-left':l,bottom:op.height()-o2.top+op.offset().top+parseInt(op.css('padding-top'))});
  144. p.caret.css({left:o2.left-o1.left-l});
  145. };
  146. p.ontoggle=function(e){
  147. e.preventDefault();p.button=$(e.target).parents(bfilter).addBack(bfilter);
  148. if(p.is(':visible')) p.onclose(); else setTimeout(p.onopen,0);
  149. };
  150. if(b) b.click(p.ontoggle);
  151. return p;
  152. },
  153. bindProp: function(obj,prop,key,def,func,evt) {
  154. obj.prop(prop,utils.getObj(key,def));
  155. if(!evt) evt=['change'];
  156. 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);});});});
  157. return obj;
  158. },
  159. shortcut:function(key,func){
  160. if(!shortcuts) initShortcuts();
  161. key=key.toLowerCase();
  162. var d=[];
  163. if(key.slice(-2)=='--') {d.push('-');key=key.slice(0,-2).split('-');}
  164. else key=key.split('-');
  165. if(key.indexOf('m')>=0||key.indexOf('meta')>=0) d.unshift('m');
  166. if(key.indexOf('s')>=0||key.indexOf('shift')>=0) d.unshift('s');
  167. if(key.indexOf('a')>=0||key.indexOf('alt')>=0) d.unshift('a');
  168. if(key.indexOf('c')>=0||key.indexOf('ctrl')>=0) d.unshift('c');
  169. key=key.join('-');
  170. if(func) shortcuts[key]=func; else delete shortcuts[key];
  171. },
  172. list: function(lkey,ikey,dnew,def) { // def===true: not null
  173. var t={};t.last=0;
  174. t.load=function(i,nosave){
  175. if(i==undefined) i=ikey?utils.getObj(ikey,0):0;
  176. if(i<0||!t.length) i=0; else if(i>=t.length) i=t.length-1;
  177. if(ikey&&!nosave) utils.setObj(ikey,i);
  178. t.cur=t.list[t.last=i];
  179. return t;
  180. };
  181. t.push=function(d){if(!d) d=dnew();t.list.push(d);t.save();return t.length-1;};
  182. t.pop=function(i){var o=t.list.splice(i,1)[0];t.save();t.load(i);return o;}
  183. t.save=function(){if(lkey) utils.setObj(lkey,t.list);if(ikey) utils.setObj(ikey,t.last);};
  184. t.list=lkey?utils.getObj(lkey,[]):[];
  185. t.__defineGetter__('length',function(){return t.list.length;});
  186. if(!t.length&&def) {if(def.concat) {t.list=def.concat();t.save();} else t.push();}
  187. return t;
  188. },
  189. getLink:function(t,o){
  190. o=o||{};
  191. var l=links[t];
  192. if(l) o.href=l;
  193. if(!o['target']) o.target='_blank';
  194. t=o.html||'';delete t.html;l=['<a'];
  195. for(i in o) l.push(i+'="'+o[i]+'"');
  196. return l.join(' ')+'>'+t+'</a>';
  197. },
  198. popup:{ // Popup Window
  199. dialog:null,
  200. show:function(o){
  201. var t=this,d=t.dialog;
  202. t.hide();t.obj=o;
  203. d.className=o.className||'';
  204. d.innerHTML=o.html;
  205. if(o.init) o.init(d);
  206. d.style.display='block';
  207. d.style.top=(innerHeight-d.offsetHeight)/2+'px';
  208. d.style.left=(innerWidth-d.offsetWidth)/2+'px';
  209. document.addEventListener('click',t._hide=t.hide.bind(this),false);
  210. },
  211. hide:function(){
  212. var t=this,o=t.obj,d=t.dialog;
  213. if(o) {
  214. if(o.dispose) o.dispose(d);
  215. d.style.display='none';t.obj=null;
  216. document.removeEventListener('click',t._hide,false);
  217. }
  218. },
  219. },
  220. };
  221. window.addEventListener('DOMContentLoaded',function(){
  222. var d=document.createElement('div');
  223. d.id='ge_popup';document.body.appendChild(d);
  224. d.addEventListener('click',function(e){e.stopPropagation();},false);
  225. utils.popup.dialog=d;
  226. utils.addStyle('\
  227. .ge_x{clear:both;}\
  228. #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;}\
  229. .ge_mask{background:rgba(0,0,0,0.6);position:fixed;top:0;bottom:0;left:0;right:0;z-index:999;display:none;}\
  230. .ge_panel_p{position:relative;z-index:888;height:1px;}\
  231. .ge_panel{position:absolute;background:#eee;border:1px solid black;padding:10px;border-radius:5px;z-index:888;bottom:0;}\
  232. .ge_caret,.ge_caret>i{position:absolute;border:solid;border-color:black transparent;border-width:8px 8px 0;z-index:889;}\
  233. .ge_caret>i{border-top-color:#eee;top:-9px;left:-8px;}\
  234. .ge_sbtn{background:#77f;color:white;border-radius:3px;border:1px solid;border:none;margin:2px;cursor:pointer;text-align:center;}\
  235. span.ge_sbtn{padding:2px 3px;}\
  236. .ge_disabled{background:gray;cursor:default;}\
  237. .ge_rsep{margin-right:10px;}\
  238. .ge_opt{padding:20px;border-radius:5px;}\
  239. .ge_opt fieldset{border:1px solid silver;border-radius:5px;padding:5px;}\
  240. .ge_opt textarea{min-height:100px;width:100%;}\
  241. ','ge_css');
  242. },false);
  243. var ff=null,key='__ge_firefox'; // For Greasemonkey or Scriptish
  244. //if(/Firefox\//.test(navigator.userAgent)){
  245. ff=unsafeWindow[key];
  246. if(!ff) ff=unsafeWindow[key]={};
  247. //}
  248. (ff||window)['utils']=utils;