Skip to content

Instantly share code, notes, and snippets.

@emanamini
Last active June 3, 2016 04:47
Show Gist options
  • Save emanamini/c03285db74f24f74f0c71850d41a71a8 to your computer and use it in GitHub Desktop.
Save emanamini/c03285db74f24f74f0c71850d41a71a8 to your computer and use it in GitHub Desktop.
Auto RTL support for gitter.im
// ==UserScript==
// @name RTL For Gitter
// @namespace Sheman
// @include *gitter.im/*
// @version 1.2.5
// @grant none
// @description This Script add "Auto RTL Support" the the gitter.im website so users like Persians could use it better than before.
// @Author Eman Amini
// ==/UserScript==
var css = ".rtl {direction: rtl;}",
head = document.head || document.getElementByTagName("head")[0],
style = document.createElement("style");
style.type = "text/css";
if (style.styleSheet)
{
style.stylesheet.cssText = css;
}
else
{
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
function checkRtl( character )
{
var RTL = new RegExp('[آ-ی]', 'g');
if( RTL.test(character))
{
return true;
} else
{
return false;
}
}
function init()
{
var chatText = document.getElementsByClassName("chat-item__text");
for (var i = 0; i < chatText.length; i++)
{
var eachLine = chatText[i].innerHTML;
var firstChar = eachLine.charAt(0);
if (checkRtl(firstChar))
{
chatText[i].classList.add('rtl');
//chatText[i].setAttribute("class", "rtl");
}
}
var chatBox = document.getElementById("chat-input-textarea");
var eachLineBox = chatBox.innerHTML;
var firstChar = eachLineBox.charAt(0);
if (checkRtl(firstChar))
{
chatText[i].classList.add('rtl');
//chatText[i].setAttribute("class", "rtl");
}
}
//setTimeout(init, 5000);
window.onload = init;
// select the target node
var target = document.getElementById('chat-container');
// create an observer instance
var observer = new MutationObserver(function(mutations)
{
mutations.forEach(function(mutation)
{
init();
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
document.documentElement.onkeyup = checkMe;
function checkMe()
{
var chatMe = document.getElementById("chat-input-textarea").value;
var firstChatMeChar = chatMe.charAt(0);
if (checkRtl(firstChatMeChar))
{
var elementID = document.getElementById("chat-input-box-region");
elementID.classList.add("rtl");
}
else
{
document.getElementById("chat-input-box-region").className = "";
document.getElementById("chat-input-box-region").className = "chat-input-box__region";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment