Created
April 3, 2014 05:35
-
-
Save billpull/9948774 to your computer and use it in GitHub Desktop.
Twitter Message Count With Shortened Links
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
/* Character Count for Posts */ | |
function checkPostForURL(post){ | |
var matches = [], | |
urlexp = new RegExp("(^|[ \t\r\n])((http|https):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))","g"), | |
$linkShort = $('#linkstoshort'), | |
matchIdx = 0; | |
if ( post !== undefined ){ | |
if ( urlexp.test(post) ){ | |
var offset = 0; | |
$('.shortenlinks').show(); | |
matches = post.match(urlexp); | |
$linkShort.html(''); | |
for(; matchIdx < matches.length; matchIdx++) { | |
var match = matches[matchIdx], | |
matchOffset = match.length - 23; | |
offset += matchOffset; | |
$linkShort.append('<li>'+match+'</li>'); | |
} | |
return offset; | |
} | |
} | |
} | |
$(function(){ | |
var $postMsg = $('#post-msg'), | |
message = $postMsg.text(), | |
twstartchars = 140 - message.length, | |
fbstartchars = 420 - message.length, | |
$fbCount = $("#fb-char"), | |
$twCount = $("#tw-char"), | |
setRemainingChars = function (evt) { | |
var a = $postMsg.val().length, | |
post = $postMsg.val(); | |
var offset = checkPostForURL(post); | |
if ( offset ){ | |
a = a - offset; | |
} | |
$fbCount.text((420-a)); | |
$twCount.text((140-a)); | |
if ( a > 120 ){ | |
$fbCount.css('color','red'); | |
if ( a > 380 ){ | |
$fbCount.css('color','red'); | |
} | |
}else{ | |
$fbCount.css('color','#333'); | |
$twCount.css('color','#333'); | |
} | |
}; | |
$fbCount.text(fbstartchars); | |
$twCount.text(twstartchars); | |
$postMsg.on('keypress change', setRemainingChars); | |
}); |
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
<textarea id="post-msg" style="width:300px;height:100px"></textarea> | |
<div id="charcnt-wrap"> | |
<span class="shortenlinks">Links will appear shortened:</span><br> | |
<ul id="linkstoshort"> | |
</ul> | |
<h5 style="margin-bottom:0;">Facebook Limit: <span id="fb-char">420</span></h5><br> | |
<h5 style="margin-top:-12px;">Twitter Limit: <span id="tw-char">140</span></h5> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment