Tieba Utilities

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

目前为 2015-02-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Tieba Utilities
  3. // @namespace http://gera2ld.blog.163.com/
  4. // @author Gerald <gera2ld@163.com>
  5. // @icon http://cn.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
  6. // @version 0.8.7
  7. // @description 百度贴吧依赖脚本(寂寞的原子的公共库)
  8. // @homepageURL http://geraldl.net/userjs/TiebaUtils
  9. // @match *://*.baidu.com/*
  10. // @include *.baidu.com/*
  11. // @run-at document-start
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // ==/UserScript==
  15.  
  16. function initUtils(event_id){
  17. // communicate with content page
  18. var post=function(data){
  19. var e=document.createEvent("MutationEvent");
  20. e.initMutationEvent(event_id,false,false,null,null,null,JSON.stringify(data),e.ADDITION);
  21. document.dispatchEvent(e);
  22. };
  23.  
  24. // popup menu for editor
  25. function Popup(b,funcs){
  26. this.funcs=funcs;
  27. this.container=null;
  28. this._onopen=this.onopen.bind(this);
  29. this._onclose=this.onclose.bind(this);
  30. this._ontoggle=this.ontoggle.bind(this);
  31. this.button=$(b).click(this._ontoggle);
  32. this.call('init').stopPropagation();
  33. }
  34. Popup.prototype={
  35. stopPropagation:function(){
  36. this.container.click(function(e){
  37. e.stopPropagation();
  38. });
  39. return this;
  40. },
  41. call:function(func){
  42. var f=this.funcs[func];
  43. if(f) f(this);
  44. return this;
  45. },
  46. getfunc:function(func){
  47. var t=this,f=t.funcs[func];
  48. if(f) return function(){f(t);};
  49. },
  50. onopen:function(){
  51. this.container.show();
  52. $(document).click(this._onclose);
  53. this.call('open');
  54. this.call('locate');
  55. },
  56. onclose:function(){
  57. this.container.hide();
  58. $(document).unbind('click',this._onclose);
  59. this.call('close');
  60. },
  61. ontoggle:function(e){
  62. e.preventDefault();
  63. if(this.container.is(':visible')) this.onclose();
  64. else setTimeout(this._onopen,0);
  65. },
  66. };
  67.  
  68. var shortcuts={},links={
  69. hotkey:'http://geraldl.net/userjs/TiebaUtils#hotkey',
  70. advanced:'http://geraldl.net/userjs/TiebaAdvanced#advanced',
  71. },utils={
  72. index:function(objs,obj,key){
  73. for(var i=0;i<objs.length;i++)
  74. if(objs[i][key]===obj) return i;
  75. return -1;
  76. },
  77. wait: (function(){
  78. var objs=[];
  79. function init(obj,name,callbacks){
  80. var value=undefined;
  81. Object.defineProperty(obj,name,{
  82. get:function(){return value;},
  83. set:function(val){
  84. value=val;
  85. if(callbacks) {
  86. callbacks.forEach(function(f){f(val);});
  87. callbacks=null;
  88. }
  89. },
  90. configurable:true,
  91. });
  92. }
  93. return function(obj,name,callback) {
  94. if(obj[name]!==undefined) callback(obj[name]);
  95. else {
  96. var i=utils.index(objs,obj,'obj'),o=objs[i],c;
  97. if(!o) objs.push(o={obj:obj,callbacks:{}});
  98. c=o.callbacks[name];
  99. if(!c) c=o.callbacks[name]=[];
  100. c.push(callback);
  101. init(obj,name,c);
  102. }
  103. };
  104. })(),
  105. /* window cannot be observed, use utils.wait instead
  106. observe: (function(){
  107. var objs=[];
  108. function init(obj,o){
  109. // Opera Presto is not supported any more
  110. function observer(changes){
  111. changes.forEach(function(c){
  112. var d=o.data[c.name];
  113. if(d) {
  114. console.log('Observed: ',obj,c.name);
  115. d.forEach(function(f){f(obj[c.name]);});
  116. delete o.data[c.name];
  117. if(!--o.length) {
  118. o=objs.indexOf(o);
  119. if(o>=0) objs.splice(o,1);
  120. Object.unobserve(obj,observer,['add']);
  121. }
  122. }
  123. });
  124. }
  125. Object.observe(obj,observer,['add']);
  126. }
  127. return function(obj,name,callback) {
  128. if(name in obj) callback(obj[name]);
  129. else {
  130. var i=utils.index(objs,obj,'obj'),o=objs[i],d;
  131. if(!o) {
  132. objs.push(o={obj:obj,data:{},length:0});
  133. init(obj,o);
  134. }
  135. d=o.data[name];
  136. if(!d) {
  137. d=o.data[name]=[];
  138. o.length++;
  139. }
  140. d.push(callback);
  141. }
  142. };
  143. })(),*/
  144. find: function(selector,ancestor,callback){
  145. var o=$(selector,ancestor),mo;
  146. if(o.length) callback(o);
  147. else if(window.MutationObserver) {
  148. mo=new MutationObserver(function(changes){
  149. $.each(changes,function(i,e){
  150. $.each(e.addedNodes,function(i,o){
  151. o=$(o).find(selector);
  152. if(o.length) {
  153. mo.disconnect();
  154. callback(o);
  155. }
  156. });
  157. });
  158. });
  159. mo.observe(ancestor,{
  160. childList:true,
  161. subtree:true,
  162. });
  163. } else $(window).load(function(){
  164. callback($(selector,ancestor));
  165. });
  166. },
  167. hook: function(o,n,a) {
  168. var f;
  169. if(o&&(f=o[n])) {
  170. if(!f.hooked) {
  171. o[n]=function() {
  172. var t=this,a=arguments,f=a.callee,r=undefined,_r,i,stop=false;
  173. f.hookStop=function(){stop=true;};
  174. for(i=0;i<f.hook_before.length;i++){
  175. r=f.hook_before[i].apply(t,[f,a]);
  176. if(stop) return r;
  177. }
  178. r=f.hook_func.apply(t,a);
  179. for(i=0;i<f.hook_after.length;i++){
  180. _r=f.hook_after[i].apply(t,[f,r,a]);
  181. if(_r!==undefined) r=_r;
  182. if(stop) return r;
  183. }
  184. return r;
  185. };
  186. o[n].hook_func=f;
  187. f=o[n];
  188. f.hooked=true;
  189. f.hook_after=[];
  190. f.hook_before=[];
  191. }
  192. o=f.hook_after;
  193. if(n=a.after) {
  194. if(n.concat) f.hook_after=o.concat(n);
  195. else o.push(n);
  196. }
  197. o=f.hook_before;
  198. if(n=a.before) {
  199. if(n.concat) f.hook_before=o.concat(n);
  200. else o.push(n);
  201. }
  202. }
  203. },
  204. addStyle: function(css,id) {
  205. var s=document.getElementById(id)||document.createElement('style');
  206. if(id) s.id=id;if(css) s.innerHTML=css;
  207. document.head.appendChild(s);
  208. return $?$(s):s;
  209. },
  210. getObj: function(k,d) {
  211. var r=localStorage.getItem('ge_'+k),u=undefined,v=u;
  212. if(r) try{v=JSON.parse(r);}catch(e){}
  213. if(v==u&&d!=u) v=utils.setObj(k,d);
  214. return v;
  215. },
  216. setObj: function(k,v) {
  217. var j=JSON.stringify(v);
  218. localStorage.setItem('ge_'+k,j);
  219. post({cmd:'setvalue',data:{key:k,val:j}});
  220. return v;
  221. },
  222. addButton: function(t,o,m,a) { // Base Function
  223. if(!a) a={};
  224. if(m) {
  225. var i,k=a.keys;
  226. if(!k||!k.length) k=['mousedown','mouseup'];
  227. for(i=0;i<k.length;i++) if(o[k[i]]) {o[k[i]].call(o,m);break;}
  228. }
  229. t=$(t);
  230. if(a.after) o.insertAfter(t.children(a.after));
  231. else if(a.before) o.insertBefore(t.children(a.before));
  232. else o.appendTo(t);
  233. return o;
  234. },
  235. addTButton: function(o,m) { // Add Toolbar Button
  236. var tb=$('div.edui-btn-toolbar').first();
  237. if(m) o.click(m);
  238. return o.appendTo($('<div class="edui-btn edui-btn-bold" unselectable="on" onmousedown="return false">').prependTo(tb));
  239. },
  240. addSButton: function(v) { // Add Big Button
  241. var b=$('<span class=poster_submit>').insertBefore('.poster_draft_status');
  242. b.label=$('<em>').html(v).appendTo(b).wrap('<a href="#" class="ui_btn ui_btn_m"><span>');
  243. return b;
  244. },
  245. addRPopup: function(b,o,c) { // Add popup for submit-like buttons
  246. return new Popup(b,{
  247. init:function(p){
  248. p.container=$('<div class=ge_panel_p title="">').prependTo(p.button).hide();
  249. p.caret=$('<i class=ge_caret>').appendTo(p.container).html('<i>');
  250. p.panel=$('<div class=ge_panel>').appendTo(p.container);
  251. },
  252. open:function(p){
  253. if(o) o(p);
  254. var l=Math.min(-50,$(document.body).innerWidth()-p.button.offset().left-p.panel.outerWidth());
  255. p.container.css({marginLeft:l});
  256. p.caret.css({left:p.button.width()/2-l-10});
  257. },
  258. close:c,
  259. });
  260. },
  261. addTPopup: function(b,o,c) { // Add popup for toolbar buttons
  262. return new Popup(b,{
  263. init:function(p){
  264. p.container=$('<div class="edui-dropdown-menu edui-popup" title="" style="display:block;top:44px;position:absolute;">').appendTo('.edui-dialog-container').hide();
  265. p.caret=$('<div class="edui-popup-caret up" style="top:-8px;position:absolute;">').appendTo(p.container);
  266. p.panel=$('<div class="edui-popup-body">').appendTo(p.container);
  267. },
  268. open:function(p){
  269. if(o) o(p);
  270. p.container.css({right:Math.max(-50,p.button.offset().left+p.button.width()-$(document.body).innerWidth())});
  271. p.caret.css({right:p.panel.outerWidth()+p.panel.offset().left-p.button.parent().offset().left-p.button.width()});
  272. },
  273. close:c,
  274. });
  275. },
  276. addLPopup: function(b,o,c) { // Add popup for Lzl buttons
  277. return new Popup(b,{
  278. init:function(p){
  279. p.container=$('<div class="lzl_edui_dialog_container">').hide();
  280. p.caret=$('<div class="inde_edui_popup_caret">').appendTo(p.container);
  281. p.panel=$('<div class="inde_edui_dropdown_menu">').appendTo(p.container);
  282. },
  283. reinit:function(p){
  284. p.stopPropagation().container.insertAfter(p.button);
  285. },
  286. open:function(p){
  287. if(o) o(p);
  288. p.container.css({left:'auto',right:0});
  289. p.caret.css({right:p.panel.outerWidth()+p.panel.offset().left-p.button.offset().left-p.button.width()});
  290. },
  291. close:c,
  292. });
  293. },
  294. bindProp: function(obj,prop,key,def,func,evt) {
  295. obj.prop(prop,utils.getObj(key,def));
  296. if(!evt) evt=['change'];
  297. 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);});});});
  298. return obj;
  299. },
  300. shortcut:function(key,func){
  301. key=key.toLowerCase();
  302. var d=[];
  303. if(key.slice(-2)=='--') {d.push('-');key=key.slice(0,-2).split('-');}
  304. else key=key.split('-');
  305. if(key.indexOf('m')>=0||key.indexOf('meta')>=0) d.unshift('m');
  306. if(key.indexOf('s')>=0||key.indexOf('shift')>=0) d.unshift('s');
  307. if(key.indexOf('a')>=0||key.indexOf('alt')>=0) d.unshift('a');
  308. if(key.indexOf('c')>=0||key.indexOf('ctrl')>=0) d.unshift('c');
  309. key=key.join('-');
  310. if(func) shortcuts[key]=func; else delete shortcuts[key];
  311. },
  312. list: function(lkey,ikey,dnew,def) { // def===true: not null
  313. var t={};t.last=0;
  314. t.load=function(i,nosave){
  315. if(i==undefined) i=ikey?utils.getObj(ikey,0):0;
  316. if(i<0||!t.length) i=0; else if(i>=t.length) i=t.length-1;
  317. if(ikey&&!nosave) utils.setObj(ikey,i);
  318. t.cur=t.list[t.last=i];
  319. return t;
  320. };
  321. t.push=function(d){if(!d) d=dnew();t.list.push(d);t.save();return t.length-1;};
  322. t.pop=function(i){var o=t.list.splice(i,1)[0];t.save();t.load(i);return o;}
  323. t.save=function(){if(lkey) utils.setObj(lkey,t.list);if(ikey) utils.setObj(ikey,t.last);};
  324. t.list=lkey?utils.getObj(lkey,[]):[];
  325. Object.defineProperty(t,'length',{get:function(){return t.list.length;}});
  326. if(!t.length&&def) {if(def.concat) {t.list=def.concat();t.save();} else t.push();}
  327. return t;
  328. },
  329. getLink:function(t,o){
  330. o=o||{};
  331. var l=links[t];
  332. if(l) o.href=l;
  333. if(!o['target']) o.target='_blank';
  334. t=o.html||'';delete t.html;l=['<a'];
  335. for(i in o) l.push(i+'="'+o[i]+'"');
  336. return l.join(' ')+'>'+t+'</a>';
  337. },
  338. dialog:{ // Popup Window
  339. dialog:null,
  340. className:'ge_popup',
  341. show:function(o){
  342. var t=this,d=t.dialog;
  343. t.hide();t.obj=o;
  344. d.className=o.className||'';
  345. d.classList.add(t.className);
  346. d.innerHTML=o.html;
  347. if(o.init) o.init(d);
  348. d.style.display='block';
  349. d.style.top=(innerHeight-d.offsetHeight)/2+'px';
  350. d.style.left=(innerWidth-d.offsetWidth)/2+'px';
  351. document.addEventListener('click',t._hide=t.hide.bind(this),false);
  352. },
  353. hide:function(){
  354. var t=this,o=t.obj,d=t.dialog;
  355. if(o) {
  356. if(o.dispose) o.dispose(d);
  357. d.innerHTML='';d.className=t.className;
  358. d.style.display='none';t.obj=null;
  359. document.removeEventListener('click',t._hide,false);
  360. }
  361. },
  362. },
  363. };
  364. utils.popup=utils.dialog;
  365. document.addEventListener('keydown',function(e){
  366. if(e.target==document.body){
  367. var k=[],f;
  368. if(e.ctrlKey) k.push('c');
  369. if(e.altKey) k.push('a');
  370. if(e.shiftKey) k.push('s');
  371. if(e.metaKey) k.push('m');
  372. k.push(String.fromCharCode(e.keyCode));
  373. k=k.join('-').toLowerCase();
  374. if(f=shortcuts[k]) {e.preventDefault();f();}
  375. }
  376. },false);
  377. window.addEventListener('DOMContentLoaded',function(){
  378. var d=document.createElement('div');document.body.appendChild(d);
  379. d.addEventListener('click',function(e){e.stopPropagation();},false);
  380. utils.dialog.dialog=d;
  381. utils.addStyle('\
  382. .ge_x{clear:both;}\
  383. .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;}\
  384. .ge_mask{background:rgba(0,0,0,0.6);position:fixed;top:0;bottom:0;left:0;right:0;z-index:1007;display:none;}\
  385. .ge_panel_p{position:relative;z-index:1006;}\
  386. .ge_panel{position:absolute;background:#eee;border:1px solid black;padding:10px;border-radius:5px;bottom:0;white-space:nowrap;}\
  387. .ge_caret,.ge_caret>i{position:absolute;border:solid;border-color:black transparent;border-width:8px 8px 0;z-index:889;}\
  388. .ge_caret>i{border-top-color:#eee;top:-9px;left:-8px;}\
  389. .ge_sbtn{background:#77f;color:white;border-radius:3px;border:1px solid;border:none;margin:2px;cursor:pointer;text-align:center;}\
  390. span.ge_sbtn{padding:2px 3px;}\
  391. .ge_disabled{background:gray;cursor:default;}\
  392. .ge_rsep{margin-right:10px;}\
  393. .ge_opt{padding:20px;border-radius:5px;}\
  394. .ge_opt fieldset{border:1px solid silver;border-radius:5px;padding:5px;}\
  395. .ge_opt textarea{min-height:100px;width:100%;}\
  396. ','ge_css');
  397. },false);
  398. var gkey='__ge_firefox',ff=window[gkey];
  399. if(!ff) ff=window[gkey]={};
  400. ff['utils']=utils;
  401. }
  402.  
  403. function injectScript(code){
  404. var s=document.createElement('script'),p=document.body||document.documentElement;
  405. s.innerHTML=code;
  406. p.appendChild(s);p.removeChild(s);
  407. }
  408. function setvalue(o){
  409. if(o) data[o.key]=o.val;
  410. GM_setValue('data',JSON.stringify(data));
  411. }
  412. var event_id='utils'+(Math.random()*65536).toString(36),data;
  413. try{
  414. data=JSON.parse(GM_getValue('data','{}'));
  415. }catch(e){
  416. data={};
  417. }
  418. if(!localStorage.ge_backup) {
  419. var i,k;
  420. if(data.backup) {
  421. for(i in data) localStorage.setItem('ge_'+i,data[i]);
  422. } else {
  423. data.backup=1;
  424. for(i=0;i<localStorage.length;i++) {
  425. k=localStorage.key(i);
  426. if(k.slice(0,3)=='ge_') data[k.slice(3)]=localStorage.getItem(k);
  427. }
  428. setvalue();
  429. }
  430. }
  431. document.addEventListener(event_id,function(e){
  432. var o=JSON.parse(e.attrName),
  433. mappings={
  434. setvalue:setvalue,
  435. },f=mappings[o.cmd];
  436. if(f) f(o.data);
  437. },false);
  438. injectScript('!'+initUtils.toString()+'('+JSON.stringify(event_id)+')');