Tieba Utilities

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

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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址