Tieba Float Movable

贴吧可移动悬浮窗

Versione datata 27/07/2014. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name	Tieba Float Movable
// @namespace	http://gera2ld.blog.163.com/
// @author	Gerald <[email protected]>
// @icon	http://s.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
// @version	1.2.8.3
// @description	贴吧可移动悬浮窗
// @homepageURL	http://geraldl.ml/userjs/TiebaFloatMovable
// @include	http://tieba.baidu.com/*
// @exclude	http://tieba.baidu.com/tb/*
// @require	https://greasyfork.org/scripts/144/code.user.js
// @grant	GM_registerMenuCommand
// ==/UserScript==

function main(utils,unsafeWindow){
function locate(m,css) {
	var c={};
	args.forEach(function(i){c[i]=/^-?\d+px/.test(css[i])?css[i]:'20px';});
	m.css(c);
}
function movable(o,name) {
	var m=$(o);
	if(!m.mousedown) return;
	o.moving=false;args=['right','bottom'];
	m.mousedown(function(e) {
		if(['DIV','TD'].indexOf(e.target.tagName)<0||e.target.contentEditable=='true') return;
		e.preventDefault();e.stopPropagation();
		o.x=e.pageX;
		if(args.indexOf('left')>=0) o.x-=parseInt(m.css('left'));
		else o.x+=parseInt(m.css('right'));
		o.y=e.pageY;
		if(args.indexOf('top')>=0) o.y-=parseInt(m.css('top'));
		else o.y+=parseInt(m.css('bottom'));
		if(!o.moving) $(document).mousemove(function(e) {
			if(o.moving) {
				var css={};
				for(var i in args) {
					var arg=args[i];
					if(arg=='left') css[arg]=e.pageX-o.x;
					else if(arg=='right') css[arg]=o.x-e.pageX;
					else if(arg=='top') css[arg]=e.pageY-o.y;
					else if(arg=='bottom') css[arg]=o.y-e.pageY;
					else continue;
					css[arg]+='px';
				}
				locate(m,css);
			}
			e.preventDefault();
		}).mouseup(function(e) {
			if(o.moving) {
				o.moving=false;
				var css={};
				for(var i in args) {
					var arg=args[i];
					css[arg]=m.css(arg);
				}
				utils.setObj('mcss_'+name,css);
				$(document).unbind('mousemove').unbind('mouseup');
			}
		});
		o.moving=true;
	});
	locate(m,utils.getObj('mcss_'+name,{}));
}
function unmovable(o) {$(o).unbind('mousedown').css({left:'',right:'',top:'',bottom:''});}

if(unsafeWindow.PosterContext&&unsafeWindow.PosterContext.isPostAllowed()) utils.wait(unsafeWindow,'test_editor',function(editor){
	function unminify(){
		if(utils.unminify) {
			mn.hide();ep.show();editor.focus();delete utils.unminify;
		}
	}
	function minify(){
		ep.hide();mn.show();utils.unminify=unminify;
		unsafeWindow.unminifyShare=unminify;	// 献给大花猫
	}
	function floatUp(){
		if(sta=='normal'||!allowUp) styleUp.html('');
		else styleUp.html('\
.edui-popup{bottom:44px;top:auto !important;}\
.edui-popup-caret{bottom:-8px;top:auto !important;transform:scale(1,-1);}\
');
	}
	function switchFloat(e) {
		if(e) {
			if(e.type=='dblclick') sta=sta=='open'?'close':'open';
			else {
				sta=sta=='normal'?'open':'normal';
				floatUp();
			}
			utils.setObj('float',sta);
		}
		if(!e||e.type=='click') {
			if(sta=='normal') {
				styleEditor.html('');styleFloat.html('');
				buttonFloat.label.html('悬 浮');ep.unbind('dblclick');
				unmovable(ep[0]);ep.prop('title','');ea.attr('style',sea);mx.hide();return;
			} else {
				styleEditor.html(s);buttonFloat.label.html('停 靠');
				if(allowSimple) ep.unbind('dblclick').dblclick(switchFloat);
				movable(ep[0],'float');ea.prop('style','');
				if(allowMinify) {minify();mx.show();} else {unminify();mx.hide();}
			}
		}
		if(!allowSimple||sta=='open') {
			styleFloat.html('\
#tb_rich_poster{width:635px;}\
#tb_rich_poster *{max-width:635px;}\
#ueditor_replace{min-height:50px !important;}\
.poster_success{top:50px !important}\
');
			if(allowSimple) ep.attr('title','双击精简');
		} else {
			styleFloat.html('\
#tb_rich_poster{width:360px;}\
#tb_rich_poster *{max-width:360px;}\
#ueditor_replace{min-height:24px !important;width:310px !important;}\
.poster_success{top:0 !important;left:40px !important;}\
.editor_bottom_panel,.edui-toolbar{display:none;}\
.editor_textfield{padding:0 !important;}\
.old_style_wrapper{width:330px !important;}\
');
			ep.attr('title','双击展开');
		}
	}
	utils.showOptionsFloat=function(){
		utils.popup.show({
			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>',
			className:'ge_opt',
			init:function(d){
				d.querySelector('#btReset').onclick=function(){locate(ep,{});};
				utils.bindProp($('#allowUp',d),'checked','allowUp',0,function(e){allowUp=this.checked;floatUp();});
				utils.bindProp($('#allowMinify',d),'checked','allowMinify',0,function(e){allowMinify=this.checked;});
				utils.bindProp($('#shortcut',d),'value','hk-float',0,function(e){if(shortcut) utils.shortcut(shortcut);shortcut=this.value;bindShortcut();});
				utils.bindProp($('#allowSimple',d),'checked','allowSimple',0,function(e){allowSimple=this.checked;});
			},
			dispose:function(d){
				switchFloat();
			},
		});
	};
	function bindShortcut(){if(shortcut) utils.shortcut(shortcut,unminify);}
	var s='\
#tb_rich_poster{border:3px double grey;position:fixed !important;z-index:10001;background-color:#E7EAEB;}\
#tb_rich_poster .editor_wrapper{margin-left:2px;}\
.poster_head,#bdInputObjWrapper,.tb_poster_placeholder,.poster_signature,.poster_draft_status,.poster_reply{display:none !important;}\
#tb_rich_poster,.editor_bottom_panel{margin-bottom:0 !important;}\
#tb_rich_poster,.poster_body,.poster_component{padding:0 !important;}\
',styleFloat=utils.addStyle(),styleEditor=utils.addStyle(),styleUp=utils.addStyle(),args,
	ea=editor.$body,ep=$('#tb_rich_poster'),sea=ea.attr('style'),buttonFloat=utils.addSButton().click(switchFloat),
	shortcut=utils.getObj('hk-float',''),sta=utils.getObj('float','normal'),
	allowUp=utils.getObj('allowUp',true),allowMinify=utils.getObj('allowMinify',true),
	allowSimple=utils.getObj('allowSimple',true),
	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),
	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);
	switchFloat();floatUp();bindShortcut();
	$('.j_quick_reply').click(unminify);
});
}
if(firefox) injectScript(main,['window']); else main(utils,unsafeWindow);
GM_registerMenuCommand('悬浮窗设置',function(){
	if(firefox) injectScript(function(utils){utils.showOptionsFloat();}); else utils.showOptionsFloat();
});