Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Intelligent RTL functionality for tweetdeck.com
// ==UserScript==
// @name tweetdeck_rtl
// @namespace http://alimsvi.ir/
// @version 0.5
// @description Intelligent RTL functionality for tweetdeck.com (Supports Arabic and Persian, Ask for more).
// @author Ali Mousavi
// @match https://tweetdeck.twitter.com/
// @require https://cdn.rawgit.com/brandonaaron/livequery/1.1.1/jquery.livequery.js
// @grant none
// ==/UserScript==
var isRTL = function(string) {
// Function to check if String is RTL
// match all unicode characters from ۹ to ؟
re_rtl = /[؟-۹]/;
// match other characters
re_notRtl = /[^؟-۹]/;
// remove html links/images from the text along with punctuations and ...
var re_trim = /(<a\b.*?<\/a>)|(<img\b.*?src=\".*?\">)|[ !\.:\"\'#~@\*()\-+%$/\\{}\[\]<>\|]/g;
var stringTrimmed = string.replace(re_trim,"");
if (!stringTrimmed.match(re_rtl)) {
// if there are no rtl characters:
return false;
}
if (stringTrimmed[0].match(re_rtl)) {
// if the first character is rtl, the text is most likely rtl.
return true;
}
if (stringTrimmed.match(re_notRtl)){
// if there are also nonRTL characters which characters are more?
var sumRTL = 0;
var sumLTR = 0;
for (var idx = 0; idx < stringTrimmed.length; idx++) {
if (stringTrimmed[idx].match(re_rtl)) {
sumRTL += 1;
}
else {
sumLTR += 1;
}
}
if (sumRTL >= sumLTR) {
return true;
}
return false;
}
};
$(document).ready(function(){
$("textarea").livequery(function() {
$(this).keyup(function() {
var textCompose = $(this).val();
if ( isRTL(textCompose) ) {
$(this).css({"direction": "rtl", "text-align": "right"});
}
else {
$(this).css({"direction": "ltr", "text-align": "left"});
}
});
});
$('.js-tweet-text, .js-quoted-tweet-text').livequery(function() {
var textTweet = $(this).html();
if ( isRTL(textTweet) ){
$(this).css({"direction": "rtl", "text-align": "right"});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment