Tieba Float Movable

贴吧可移动悬浮窗

当前为 2014-08-15 提交的版本,查看 最新版本

  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.9.2
  7. // @description 贴吧可移动悬浮窗
  8. // @homepageURL 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 none
  13. // ==/UserScript==
  14.  
  15. function locate(m,css) {
  16. var c={};
  17. args.forEach(function(i){c[i]=/^-?\d+px/.test(css[i])?css[i]:'20px';});
  18. m.css(c);
  19. }
  20. function movable(o,name) {
  21. var m=$(o);
  22. if(!m.mousedown) return;
  23. o.moving=false;args=['right','bottom'];
  24. m.mousedown(function(e) {
  25. if(['DIV','TD'].indexOf(e.target.tagName)<0||e.target.contentEditable=='true') return;
  26. e.preventDefault();e.stopPropagation();
  27. o.x=e.pageX;
  28. if(args.indexOf('left')>=0) o.x-=parseInt(m.css('left'));
  29. else o.x+=parseInt(m.css('right'));
  30. o.y=e.pageY;
  31. if(args.indexOf('top')>=0) o.y-=parseInt(m.css('top'));
  32. else o.y+=parseInt(m.css('bottom'));
  33. if(!o.moving) $(document).mousemove(function(e) {
  34. if(o.moving) {
  35. var css={};
  36. for(var i in args) {
  37. var arg=args[i];
  38. if(arg=='left') css[arg]=e.pageX-o.x;
  39. else if(arg=='right') css[arg]=o.x-e.pageX;
  40. else if(arg=='top') css[arg]=e.pageY-o.y;
  41. else if(arg=='bottom') css[arg]=o.y-e.pageY;
  42. else continue;
  43. css[arg]+='px';
  44. }
  45. locate(m,css);
  46. }
  47. e.preventDefault();
  48. }).mouseup(function(e) {
  49. if(o.moving) {
  50. o.moving=false;
  51. var css={};
  52. for(var i in args) {
  53. var arg=args[i];
  54. css[arg]=m.css(arg);
  55. }
  56. utils.setObj('mcss_'+name,css);
  57. $(document).unbind('mousemove').unbind('mouseup');
  58. }
  59. });
  60. o.moving=true;
  61. });
  62. locate(m,utils.getObj('mcss_'+name,{}));
  63. }
  64. function unmovable(o) {$(o).unbind('mousedown').css({left:'',right:'',top:'',bottom:''});}
  65.  
  66. if(unsafeWindow.PosterContext&&unsafeWindow.PosterContext.isPostAllowed()) utils.wait(unsafeWindow,'test_editor',function(editor){
  67. function unminify(){
  68. if(utils.unminify) {
  69. mn.hide();ep.show();editor.focus();delete utils.unminify;
  70. }
  71. }
  72. function minify(){
  73. ep.hide();mn.show();utils.unminify=unminify;
  74. unsafeWindow.unminifyShare=unminify; // 献给大花猫
  75. }
  76. function floatUp(){
  77. if(sta=='normal'||!allowUp) styleUp.html('');
  78. else styleUp.html('\
  79. .edui-popup{bottom:44px;top:auto !important;}\
  80. .edui-popup-caret{bottom:-8px;top:auto !important;transform:scale(1,-1);-webkit-transform:scale(1,-1);}\
  81. ');
  82. }
  83. function showFloat() {
  84. if(!allowSimple||sta=='open') {
  85. styleFloat.html('\
  86. #tb_rich_poster{width:635px;}\
  87. #tb_rich_poster *{max-width:635px;}\
  88. #ueditor_replace{min-height:50px !important;}\
  89. .poster_success{top:50px !important}\
  90. ');
  91. if(allowSimple) ep.attr('title','双击精简');
  92. if(allowMinify) mx.show(); else mx.hide();
  93. } else {
  94. styleFloat.html('\
  95. #tb_rich_poster{width:360px;}\
  96. #tb_rich_poster *{max-width:360px;}\
  97. #ueditor_replace{min-height:24px !important;width:310px !important;}\
  98. .poster_success{top:0 !important;left:40px !important;}\
  99. .editor_bottom_panel,.edui-toolbar{display:none;}\
  100. .editor_textfield{padding:0 !important;}\
  101. .old_style_wrapper{width:330px !important;}\
  102. ');
  103. ep.attr('title','双击展开');
  104. mx.hide();
  105. }
  106. }
  107. function bindShortcut(){
  108. if(shortcut) utils.shortcut(shortcut,unminify);
  109. }
  110. var styleEditor=utils.addStyle('\
  111. #tb_rich_poster{border:3px double grey;position:fixed !important;z-index:10001;background-color:#E7EAEB;}\
  112. #tb_rich_poster .editor_wrapper{margin-left:2px;}\
  113. .poster_head,#bdInputObjWrapper,.tb_poster_placeholder,.poster_signature,.poster_draft_status,.poster_reply{display:none !important;}\
  114. #tb_rich_poster,.editor_bottom_panel{margin-bottom:0 !important;}\
  115. #tb_rich_poster,.poster_body,.poster_component{padding:0 !important;}\
  116. '),styleFloat=utils.addStyle(),styleUp=utils.addStyle(),args,
  117. buttonFloat=utils.addSButton('悬 浮'),
  118. panelFloat=utils.addRPopup(buttonFloat,null,showFloat).panel,
  119. shortcut=utils.getObj('hk-float',''),
  120. sta=utils.getObj('float','open'),
  121. allowUp=utils.getObj('allowUp',true),
  122. allowMinify=utils.getObj('allowMinify',true),
  123. allowSimple=utils.getObj('allowSimple',true),
  124. ep=$('#tb_rich_poster').dblclick(function(){
  125. if(allowSimple) {
  126. sta=sta=='open'?'close':'open';
  127. utils.setObj('float',sta);
  128. showFloat();
  129. }
  130. }),
  131. 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),
  132. 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);
  133. movable(ep[0],'float');
  134. editor.$container.attr('style','');
  135. showFloat();floatUp();bindShortcut();
  136. if(allowMinify) minify();
  137. $('.j_quick_reply').click(unminify);
  138. panelFloat.html('<div class="ge_sbtn" style="cursor:default">悬浮设置</div><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>');
  139. $('#btReset',panelFloat).click(function(){locate(ep,{});});
  140. utils.bindProp($('#allowUp',panelFloat),'checked','allowUp',0,function(e){
  141. allowUp=this.checked;floatUp();
  142. });
  143. utils.bindProp($('#allowMinify',panelFloat),'checked','allowMinify',0,function(e){
  144. allowMinify=this.checked;showFloat();
  145. });
  146. utils.bindProp($('#shortcut',panelFloat),'value','hk-float',0,function(e){
  147. if(shortcut) utils.shortcut(shortcut);
  148. shortcut=this.value;bindShortcut();
  149. });
  150. utils.bindProp($('#allowSimple',panelFloat),'checked','allowSimple',0,function(e){
  151. allowSimple=this.checked;showFloat();
  152. });
  153. });