Tieba Utilities

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

目前為 2014-12-28 提交的版本,檢視 最新版本

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