Tieba Float Movable

贴吧可移动悬浮窗

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

  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.3
  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(){
  74. ep.hide();mn.show();utils.unminify=unminify;
  75. unsafeWindow.unminifyShare=unminify; // 献给大花猫
  76. }
  77. function floatUp(){
  78. if(sta=='normal'||!allowUp) styleUp.html('');
  79. else styleUp.html('\
  80. .edui-popup{bottom:44px;top:auto !important;}\
  81. .edui-popup-caret{bottom:-8px;top:auto !important;transform:scale(1,-1);}\
  82. ');
  83. }
  84. function switchFloat(e) {
  85. if(e) {
  86. if(e.type=='dblclick') sta=sta=='open'?'close':'open';
  87. else {
  88. sta=sta=='normal'?'open':'normal';
  89. floatUp();
  90. }
  91. utils.setObj('float',sta);
  92. }
  93. if(!e||e.type=='click') {
  94. if(sta=='normal') {
  95. styleEditor.html('');styleFloat.html('');
  96. buttonFloat.label.html('悬 浮');ep.unbind('dblclick');
  97. unmovable(ep[0]);ep.prop('title','');ea.attr('style',sea);mx.hide();return;
  98. } else {
  99. styleEditor.html(s);buttonFloat.label.html('停 靠');
  100. if(allowSimple) ep.unbind('dblclick').dblclick(switchFloat);
  101. movable(ep[0],'float');ea.prop('style','');
  102. if(allowMinify) {minify();mx.show();} else {unminify();mx.hide();}
  103. }
  104. }
  105. if(!allowSimple||sta=='open') {
  106. styleFloat.html('\
  107. #tb_rich_poster{width:635px;}\
  108. #tb_rich_poster *{max-width:635px;}\
  109. #ueditor_replace{min-height:50px !important;}\
  110. .poster_success{top:50px !important}\
  111. ');
  112. if(allowSimple) ep.attr('title','双击精简');
  113. } else {
  114. styleFloat.html('\
  115. #tb_rich_poster{width:360px;}\
  116. #tb_rich_poster *{max-width:360px;}\
  117. #ueditor_replace{min-height:24px !important;width:310px !important;}\
  118. .poster_success{top:0 !important;left:40px !important;}\
  119. .editor_bottom_panel,.edui-toolbar{display:none;}\
  120. .editor_textfield{padding:0 !important;}\
  121. .old_style_wrapper{width:330px !important;}\
  122. ');
  123. ep.attr('title','双击展开');
  124. }
  125. }
  126. utils.showOptionsFloat=function(){
  127. utils.popup.show({
  128. 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>',
  129. className:'ge_opt',
  130. init:function(d){
  131. d.querySelector('#btReset').onclick=function(){locate(ep,{});};
  132. utils.bindProp($('#allowUp',d),'checked','allowUp',0,function(e){allowUp=this.checked;floatUp();});
  133. utils.bindProp($('#allowMinify',d),'checked','allowMinify',0,function(e){allowMinify=this.checked;});
  134. utils.bindProp($('#shortcut',d),'value','hk-float',0,function(e){if(shortcut) utils.shortcut(shortcut);shortcut=this.value;bindShortcut();});
  135. utils.bindProp($('#allowSimple',d),'checked','allowSimple',0,function(e){allowSimple=this.checked;});
  136. },
  137. dispose:function(d){
  138. switchFloat();
  139. },
  140. });
  141. };
  142. function bindShortcut(){if(shortcut) utils.shortcut(shortcut,unminify);}
  143. var s='\
  144. #tb_rich_poster{border:3px double grey;position:fixed !important;z-index:10001;background-color:#E7EAEB;}\
  145. #tb_rich_poster .editor_wrapper{margin-left:2px;}\
  146. .poster_head,#bdInputObjWrapper,.tb_poster_placeholder,.poster_signature,.poster_draft_status,.poster_reply{display:none !important;}\
  147. #tb_rich_poster,.editor_bottom_panel{margin-bottom:0 !important;}\
  148. #tb_rich_poster,.poster_body,.poster_component{padding:0 !important;}\
  149. ',styleFloat=utils.addStyle(),styleEditor=utils.addStyle(),styleUp=utils.addStyle(),args,
  150. ea=editor.$body,ep=$('#tb_rich_poster'),sea=ea.attr('style'),buttonFloat=utils.addSButton().click(switchFloat),
  151. shortcut=utils.getObj('hk-float',''),sta=utils.getObj('float','normal'),
  152. allowUp=utils.getObj('allowUp',true),allowMinify=utils.getObj('allowMinify',true),
  153. allowSimple=utils.getObj('allowSimple',true),
  154. 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),
  155. 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);
  156. switchFloat();floatUp();bindShortcut();
  157. $('.j_quick_reply').click(unminify);
  158. });
  159. }
  160. if(firefox) injectScript(main,['window']); else main(utils,unsafeWindow);
  161. GM_registerMenuCommand('悬浮窗设置',function(){
  162. if(firefox) injectScript(function(utils){utils.showOptionsFloat();}); else utils.showOptionsFloat();
  163. });