Skip to content

Instantly share code, notes, and snippets.

@alirezas
Last active March 25, 2022 10:31
Show Gist options
  • Save alirezas/db5bbcf17e3304bf618834bf90d0921c to your computer and use it in GitHub Desktop.
Save alirezas/db5bbcf17e3304bf618834bf90d0921c to your computer and use it in GitHub Desktop.
RTL support for slack.com
// ==UserScript==
// @name RTL Slack
// @namespace slask.com
// @include https://*.slack.com/*
// @version 1.2.2
// @grant none
// ==/UserScript==
function isRTL(text) {
if (text !== "") {
if ("اآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیؤئيإأةك۰۱۲۳۴۵۶۷۸۹".indexOf(text[0]) > -1) {
return true;
}
}
return false;
}
var is_rtl = 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;
}
};
if ($ !== undefined) {
$(document).ready(function() {
var style = '<style type="text/css">';
style += '.message_body.rtl { direction: rtl; }';
style += '.rtl-input {direction: rtl;text-align: right;}';
style += '.rtl:before {content: "\u202B";}';
style += '.rtl:after {content: "\u200F";}';
style += '.edited { float: right; }';
style += '.text-rtl { direction: rtl; }';
style += '.text-rtl li[type=bullet], .text-rtl li[type=number] { padding-right: 2rem; padding-left: 0; }';
style += '.text-rtl li[type=bullet]:before, .text-rtl li[type=number]:before { margin-right: -61px; margin-left: 0; text-align: left; }';
style += '</style>';
$('head').append(style);
$('#footer_msgs').bind('DOMNodeInserted', function(e) {
$('.ql-editor').on('blur keyup paste input', function() {
if (is_rtl($(this).html())) {
$(this).addClass('rtl-input');
}
else {
$(this).removeClass('rtl-input');
}
});
});
$('#msgs_div').bind('DOMNodeInserted', function(e) {
$('.message_body').each(function() {
if ( is_rtl( $(this).html() ) ) {
$(this).addClass('rtl');
}
});
$('#msg_text').focus(function() {
if (is_rtl($(this).val())) {
$(this).addClass('rtl-input');
}
else {
$(this).removeClass('rtl-input');
}
});
});
$('ts-space .content').bind('DOMNodeInserted', function(e) {
$('.text').each(function() {
if ( is_rtl( $(this).find('.shadow').html() ) ) {
$(this).addClass('text-rtl');
}
});
});
});
}
@reza-baiat
Copy link

that is not 'RTL' support, but just persian support. there is another RTL languages like Hebrew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment