Tieba Float Movable

贴吧可移动悬浮窗

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

  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.1
  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 switchFloat(e) {
  84. if(e) {
  85. sta=sta=='open'?'close':'open';
  86. utils.setObj('float',sta);
  87. } else {
  88. ep.unbind('dblclick');if(allowSimple) ep.dblclick(switchFloat);
  89. if(allowMinify) {minify();mx.show();} else {unminify();mx.hide();}
  90. }
  91. if(!allowSimple||sta=='open') {
  92. styleFloat.html('\
  93. #tb_rich_poster{width:635px;}\
  94. #tb_rich_poster *{max-width:635px;}\
  95. #ueditor_replace{min-height:50px !important;}\
  96. .poster_success{top:50px !important}\
  97. ');
  98. if(allowSimple) ep.attr('title','双击精简');
  99. } else {
  100. styleFloat.html('\
  101. #tb_rich_poster{width:360px;}\
  102. #tb_rich_poster *{max-width:360px;}\
  103. #ueditor_replace{min-height:24px !important;width:310px !important;}\
  104. .poster_success{top:0 !important;left:40px !important;}\
  105. .editor_bottom_panel,.edui-toolbar{display:none;}\
  106. .editor_textfield{padding:0 !important;}\
  107. .old_style_wrapper{width:330px !important;}\
  108. ');
  109. ep.attr('title','双击展开');
  110. }
  111. }
  112. function bindShortcut(){if(shortcut) utils.shortcut(shortcut,unminify);}
  113. var styleEditor=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. #tb_rich_poster,.editor_bottom_panel{margin-bottom:0 !important;}\
  118. #tb_rich_poster,.poster_body,.poster_component{padding:0 !important;}\
  119. '),styleFloat=utils.addStyle(),styleUp=utils.addStyle(),args,
  120. ep=$('#tb_rich_poster'),buttonFloat=utils.addSButton('悬 浮'),
  121. panelFloat=utils.addRPopup(buttonFloat,null,function(){switchFloat();}).panel,
  122. shortcut=utils.getObj('hk-float',''),sta=utils.getObj('float','open'),
  123. allowUp=utils.getObj('allowUp',true),allowMinify=utils.getObj('allowMinify',true),
  124. allowSimple=utils.getObj('allowSimple',true),
  125. 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),
  126. 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);
  127. movable(ep[0],'float');editor.$container.attr('style','');
  128. switchFloat();floatUp();bindShortcut();
  129. $('.j_quick_reply').click(unminify);
  130. 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>');
  131. $('#btReset',panelFloat).click(function(){locate(ep,{});});
  132. utils.bindProp($('#allowUp',panelFloat),'checked','allowUp',0,function(e){allowUp=this.checked;floatUp();});
  133. utils.bindProp($('#allowMinify',panelFloat),'checked','allowMinify',0,function(e){allowMinify=this.checked;});
  134. utils.bindProp($('#shortcut',panelFloat),'value','hk-float',0,function(e){if(shortcut) utils.shortcut(shortcut);shortcut=this.value;bindShortcut();});
  135. utils.bindProp($('#allowSimple',panelFloat),'checked','allowSimple',0,function(e){allowSimple=this.checked;});
  136. });