Tieba Float Movable

贴吧可移动悬浮窗

目前為 2014-07-13 提交的版本,檢視 最新版本

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