Tieba Float Movable

贴吧可移动悬浮窗

当前为 2014-10-17 提交的版本,查看 最新版本

  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.10.2
  7. // @description 贴吧可移动悬浮窗
  8. // @homepageURL http://geraldl.net/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. function main(editor) {
  66. function unminify(){
  67. if(utils.unminify) {
  68. mn.hide();ep.show();editor.focus();delete utils.unminify;
  69. }
  70. }
  71. function minify(){
  72. ep.hide();mn.show();utils.unminify=unminify;
  73. unsafeWindow.unminifyShare=unminify; // 献给大花猫
  74. }
  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);-webkit-transform:scale(1,-1);}\
  80. ');
  81. }
  82. function showFloat() {
  83. if(sta=='open') {
  84. styleFloat.html('\
  85. #tb_rich_poster{width:635px;}\
  86. #tb_rich_poster *{max-width:635px;}\
  87. #ueditor_replace{min-height:50px !important;}\
  88. .poster_success{top:50px !important}\
  89. .old_style_wrapper{width:600px !important;}\
  90. ');
  91. if(allowSimple) ep.attr('title','双击精简');
  92. } else {
  93. styleFloat.html('\
  94. #tb_rich_poster{width:360px;}\
  95. #tb_rich_poster *{max-width:360px;}\
  96. #ueditor_replace{min-height:24px !important;width:310px !important;}\
  97. .poster_success{top:0 !important;left:40px !important;}\
  98. .editor_bottom_panel,.edui-toolbar{display:none;}\
  99. .editor_textfield{padding:0 !important;}\
  100. .old_style_wrapper{width:330px !important;}\
  101. ');
  102. ep.attr('title','双击展开');
  103. }
  104. if(sta=='open'&&allowMinify) {
  105. mx.show();styleMinify.html('#tb_rich_poster{padding-left:10px;}');
  106. } else {
  107. mx.hide();styleMinify.html('');
  108. }
  109. }
  110. function bindShortcut(){
  111. if(shortcut) utils.shortcut(shortcut,unminify);
  112. }
  113. utils.addStyle('\
  114. #tb_rich_poster{border:3px double grey;position:fixed !important;z-index:10001;background-color:#E7EAEB;}\
  115. #tb_rich_poster .editor_wrapper{margin-left:2px;}\
  116. .poster_head,#bdInputObjWrapper,.tb_poster_placeholder,.poster_signature,.poster_draft_status,.poster_reply{display:none !important;}\
  117. .editor_bottom_panel{margin-bottom:0 !important;}\
  118. .poster_body .poster_component{padding-bottom:0 !important;}\
  119. ');
  120. var styleFloat=utils.addStyle(),styleUp=utils.addStyle(),styleMinify=utils.addStyle(),
  121. args,buttonFloat=utils.addSButton('悬 浮'),
  122. panelFloat=utils.addRPopup(buttonFloat,null,showFloat).panel,
  123. shortcut=utils.getObj('hk-float',''),
  124. sta=utils.getObj('float','open'),
  125. allowUp=utils.getObj('allowUp',true),
  126. allowMinify=utils.getObj('allowMinify',true),
  127. allowSimple=utils.getObj('allowSimple',true),
  128. ep=$('#tb_rich_poster').dblclick(function(){
  129. if(allowSimple) {
  130. sta=sta=='open'?'close':'open';
  131. utils.setObj('float',sta);
  132. showFloat();
  133. }
  134. }),
  135. 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),
  136. mx=$('<div style="position:absolute;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);
  137. movable(ep[0],'float');
  138. editor.$container.attr('style','');
  139. showFloat();floatUp();bindShortcut();
  140. if(allowMinify) minify();
  141. $('.j_quick_reply').click(unminify);
  142. 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>');
  143. $('#btReset',panelFloat).click(function(){locate(ep,{});});
  144. utils.bindProp($('#allowUp',panelFloat),'checked','allowUp',0,function(e){
  145. allowUp=this.checked;floatUp();
  146. });
  147. utils.bindProp($('#allowMinify',panelFloat),'checked','allowMinify',0,function(e){
  148. allowMinify=this.checked;showFloat();
  149. });
  150. utils.bindProp($('#shortcut',panelFloat),'value','hk-float',0,function(e){
  151. if(shortcut) utils.shortcut(shortcut);
  152. shortcut=this.value;bindShortcut();
  153. });
  154. utils.bindProp($('#allowSimple',panelFloat),'checked','allowSimple',0,function(e){
  155. allowSimple=this.checked;showFloat();
  156. });
  157. }
  158.  
  159. if(unsafeWindow.PosterContext&&unsafeWindow.PosterContext.isPostAllowed()) {
  160. if(unsafeWindow.test_editor) main(test_editor);
  161. else _.Module.use("common/widget/RichPoster",{prefix:{}},function(t){t.init();t.unbindScrollEvent();main(unsafeWindow.test_editor);});
  162. }