Tieba Float Movable

贴吧可移动悬浮窗

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

  1. // ==UserScript==
  2. // @name Tieba Float Movable
  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 1.2.8.1
  7. // @description 贴吧可移动悬浮窗
  8. // @homepage http://geraldl.ml/userjs/TiebaFloatMovable
  9. // @include http://tieba.baidu.com/*
  10. // @exclude http://tieba.baidu.com/tb/*
  11. // @require https://greasyfork.org/scripts/144/code.user.js
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_registerMenuCommand
  15. // ==/UserScript==
  16.  
  17. function locate(m,css) {
  18. var c={};
  19. args.forEach(function(i){c[i]=/^-?\d+px/.test(css[i])?css[i]:'20px';});
  20. m.css(c);
  21. }
  22. function movable(o,name) {
  23. var m=$(o);
  24. if(!m.mousedown) return;
  25. o.moving=false;args=['right','bottom'];
  26. m.mousedown(function(e) {
  27. if(['DIV','TD'].indexOf(e.target.tagName)<0||e.target.contentEditable=='true') return;
  28. e.preventDefault();e.stopPropagation();
  29. o.x=e.pageX;
  30. if(args.indexOf('left')>=0) o.x-=parseInt(m.css('left'));
  31. else o.x+=parseInt(m.css('right'));
  32. o.y=e.pageY;
  33. if(args.indexOf('top')>=0) o.y-=parseInt(m.css('top'));
  34. else o.y+=parseInt(m.css('bottom'));
  35. if(!o.moving) $(document).mousemove(function(e) {
  36. if(o.moving) {
  37. var css={};
  38. for(var i in args) {
  39. var arg=args[i];
  40. if(arg=='left') css[arg]=e.pageX-o.x;
  41. else if(arg=='right') css[arg]=o.x-e.pageX;
  42. else if(arg=='top') css[arg]=e.pageY-o.y;
  43. else if(arg=='bottom') css[arg]=o.y-e.pageY;
  44. else continue;
  45. css[arg]+='px';
  46. }
  47. locate(m,css);
  48. }
  49. e.preventDefault();
  50. }).mouseup(function(e) {
  51. if(o.moving) {
  52. o.moving=false;
  53. var css={};
  54. for(var i in args) {
  55. var arg=args[i];
  56. css[arg]=m.css(arg);
  57. }
  58. utils.setObj('mcss_'+name,css);
  59. $(document).unbind('mousemove').unbind('mouseup');
  60. }
  61. });
  62. o.moving=true;
  63. });
  64. locate(m,utils.getObj('mcss_'+name,{}));
  65. }
  66. function unmovable(o) {$(o).unbind('mousedown').css({left:'',right:'',top:'',bottom:''});}
  67.  
  68. if(unsafeWindow.PosterContext&&unsafeWindow.PosterContext.isPostAllowed()) utils.wait(unsafeWindow,'test_editor',function(editor){
  69. function unminify(){
  70. if(utils.unminify) {
  71. mn.hide();ep.show();editor.focus();delete utils.unminify;
  72. }
  73. }
  74. function minify(){ep.hide();mn.show();utils.unminify=unminify;}
  75. function floatUp(){
  76. if(sta=='normal'||!allowUp) styleUp.html('');
  77. else styleUp.html('\
  78. .edui-popup{bottom:44px;top:auto !important;}\
  79. .edui-popup-caret{bottom:-8px;top:auto !important;transform:scale(1,-1);}\
  80. ');
  81. }
  82. function switchFloat(e) {
  83. if(e) {
  84. if(e.type=='dblclick') sta=sta=='open'?'close':'open';
  85. else {
  86. sta=sta=='normal'?'open':'normal';
  87. floatUp();
  88. }
  89. utils.setObj('float',sta);
  90. }
  91. if(!e||e.type=='click') {
  92. if(sta=='normal') {
  93. styleEditor.html('');styleFloat.html('');
  94. buttonFloat.label.html('悬 浮');ep.unbind('dblclick');
  95. unmovable(ep[0]);ep.prop('title','');ea.attr('style',sea);mx.hide();return;
  96. } else {
  97. styleEditor.html(s);buttonFloat.label.html('停 靠');
  98. if(allowSimple) ep.unbind('dblclick').dblclick(switchFloat);
  99. movable(ep[0],'float');ea.prop('style','');
  100. if(allowMinify) {minify();mx.show();} else {unminify();mx.hide();}
  101. }
  102. }
  103. if(!allowSimple||sta=='open') {
  104. styleFloat.html('\
  105. #tb_rich_poster{width:635px;}\
  106. #tb_rich_poster *{max-width:635px;}\
  107. #ueditor_replace{min-height:50px !important;}\
  108. .poster_success{top:50px !important}\
  109. ');
  110. if(allowSimple) ep.attr('title','双击精简');
  111. } else {
  112. styleFloat.html('\
  113. #tb_rich_poster{width:360px;}\
  114. #tb_rich_poster *{max-width:360px;}\
  115. #ueditor_replace{min-height:24px !important;width:310px !important;}\
  116. .poster_success{top:0 !important;left:40px !important;}\
  117. .editor_bottom_panel,.edui-toolbar{display:none;}\
  118. .editor_textfield{padding:0 !important;}\
  119. .old_style_wrapper{width:330px !important;}\
  120. ');
  121. ep.attr('title','双击展开');
  122. }
  123. }
  124. function showOptions(){
  125. utils.popup.show({
  126. html:'<h3>贴吧悬浮窗脚本设置</h3><label><input type=checkbox id=allowUp>编辑框中的弹出窗口向上弹出</label><br><label><input type=checkbox id=allowMinify>自动隐藏悬浮窗口到右下角</label><br><label style="margin-left:20px;">从右下角呼出快捷键:'+utils.getLink('hotkey',{title:'帮助',html:'(?)'})+'<input id=shortcut style="width:60px"></label><br><label><input type=checkbox id=allowSimple>双击悬浮窗口使其精简或展开</label><br><button id=btReset>重置悬浮窗位置</button>',
  127. className:'ge_opt',
  128. init:function(d){
  129. d.querySelector('#btReset').onclick=function(){locate(ep,{});};
  130. utils.bindProp($('#allowUp',d),'checked','allowUp',0,function(e){allowUp=this.checked;floatUp();});
  131. utils.bindProp($('#allowMinify',d),'checked','allowMinify',0,function(e){allowMinify=this.checked;});
  132. utils.bindProp($('#shortcut',d),'value','hk-float',0,function(e){if(shortcut) utils.shortcut(shortcut);shortcut=this.value;bindShortcut();});
  133. utils.bindProp($('#allowSimple',d),'checked','allowSimple',0,function(e){allowSimple=this.checked;});
  134. },
  135. dispose:function(d){
  136. switchFloat();
  137. },
  138. });
  139. }
  140. function bindShortcut(){if(shortcut) utils.shortcut(shortcut,unminify);}
  141. var s='\
  142. #tb_rich_poster{border:3px double grey;position:fixed !important;z-index:10001;background-color:#E7EAEB;}\
  143. #tb_rich_poster .editor_wrapper{margin-left:2px;}\
  144. .poster_head,#bdInputObjWrapper,.tb_poster_placeholder,.poster_signature,.poster_draft_status,.poster_reply{display:none !important;}\
  145. #tb_rich_poster,.editor_bottom_panel{margin-bottom:0 !important;}\
  146. #tb_rich_poster,.poster_body,.poster_component{padding:0 !important;}\
  147. ',styleFloat=utils.addStyle(),styleEditor=utils.addStyle(),styleUp=utils.addStyle(),args,
  148. ea=editor.$body,ep=$('#tb_rich_poster'),sea=ea.attr('style'),buttonFloat=utils.addSButton().click(switchFloat),
  149. shortcut=utils.getObj('hk-float',''),sta=utils.getObj('float','normal'),
  150. allowUp=utils.getObj('allowUp',true),allowMinify=utils.getObj('allowMinify',true),
  151. allowSimple=utils.getObj('allowSimple',true),
  152. mn=$('<div id=btUnminify style="position:fixed;bottom:0;right:0;background:white;padding-top:40px;z-index:10;">◀</div>').appendTo(document.body).hide().mouseover(unminify),
  153. mx=$('<div style="position:absolute;border:1px solid gray;background:inherit;height:100%;cursor:pointer;right:0;" title="最小化">▶</div>').appendTo($('<div style="position:relative;background:inherit;height:100%;">').appendTo($('<div style="position:absolute;top:0;background:inherit;height:100%;">').appendTo(ep))).hide().click(minify);
  154. switchFloat();floatUp();bindShortcut();
  155. GM_registerMenuCommand('悬浮窗设置',showOptions);
  156. $('.j_quick_reply').click(unminify);
  157. });