Created
January 27, 2012 00:54
-
-
Save rlemon/1686228 to your computer and use it in GitHub Desktop.
Stack Exchange Chat input fix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Chat Input Fix | |
// @author rlemon | |
// @version 0.1 | |
// @namespace rlemon.com | |
// @description Sets a cookie to remember your chat input width setting. | |
// @include http://chat.stackexchange.com/rooms/* | |
// @include http://chat.stackoverflow.com/rooms/* | |
// ==/UserScript== | |
function EmbedCodeOnPage(jcode) { | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.textContent = jcode; | |
document.head.appendChild(script); | |
} | |
function EmbedFunctionOnPageAndExecute(function_contents) { | |
EmbedCodeOnPage('(' + function_contents.toString() + ')()'); | |
} | |
EmbedFunctionOnPageAndExecute(function() { | |
var createCookie = function(name, value, days) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
var expires = '; expires=' + date.toGMTString(); | |
} | |
else var expires = ''; | |
document.cookie = name + '=' + value + expires + '; path=/'; | |
}; | |
var readCookie = function(name) { | |
var nameEQ = name + '='; | |
var ca = document.cookie.split(';'); | |
for (var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') c = c.substring(1, c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); | |
} | |
return null; | |
}; | |
var input = $('#input'); | |
if( readCookie('sec_input_width') ) { | |
input.css({ | |
'width': readCookie('sec_input_width') + 'px' | |
}); | |
} | |
input.data('x', input.outerWidth()); | |
input.mouseup(function(){ | |
var _input = $(this); | |
_input.data('x', _input.outerWidth()); | |
createCookie('sec_input_width', _input.data('x'), 365); | |
}); | |
/* | |
* Fix for Chrome - | |
* in chrome the width of a textarea cannot be changed to anything less that the width value (width assumes min-width) | |
* this button resets the width of the textarea to the default width of 540px | |
*/ | |
var reset = $('<button>', { | |
text: '⇐', | |
title: 'Reset the chat input width (fix for chrome)', | |
click: function() { | |
input.css({ | |
'width': '540px' | |
}); | |
input.trigger('mouseup'); | |
} | |
}); | |
$('#chat-buttons').append(reset); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment