Tieba Utilities

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

当前为 2014-07-13 提交的版本,查看 最新版本

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