Last active
August 28, 2018 13:07
-
-
Save shaybensasson/c2275c0ad0e88f06af212ea07c7f74fc to your computer and use it in GitHub Desktop.
Slack RTL support
This file contains hidden or 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 Slack RTL | |
| // @namespace http://blah | |
| // @version 0.1 | |
| // @description Support RTL/Hebrew in slack.com, from here: https://github.com/gregvish/slackrtl | |
| // @author | |
| // @match https://*.slack.com/* | |
| // @grant GPL | |
| // ==/UserScript== | |
| /* jshint -W097 */ | |
| 'use strict'; | |
| // SHAY: @match was https://*.slack.com/messages/* | |
| function should_input_be_rtl(element) | |
| { | |
| if (typeof element.innerText != "string") { | |
| return false; | |
| } | |
| return null !== element.innerText.match(/^[\s\d"']*[א-ת]+/); | |
| } | |
| function should_message_be_rtl(element) | |
| { | |
| if (typeof element.innerText != "string") { | |
| return false; | |
| } | |
| return null !== element.innerText.match(/^[\s\d"']*[א-ת]+/); | |
| } | |
| function fix_all_emojis(element) | |
| { | |
| var iterator = document.evaluate('//*[@class="emoji emoji-sizer"]', element, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); | |
| var emoji_elemnt = iterator.iterateNext(); | |
| while (emoji_elemnt) { | |
| emoji_elemnt.style.setProperty("direction", "ltr"); | |
| emoji_elemnt = iterator.iterateNext(); | |
| } | |
| } | |
| function do_mod() | |
| { | |
| var targets = document.getElementsByClassName("c-message__body"); | |
| for (var j = 0; j < targets.length; j++) { | |
| var element = targets[j]; | |
| if (should_message_be_rtl(element)) { | |
| element.parentNode.style.setProperty("direction", "rtl"); | |
| fix_all_emojis(element); | |
| } | |
| } | |
| } | |
| var observer = new MutationObserver(function(mutations) { | |
| do_mod(); | |
| }); | |
| window.setTimeout(function() { | |
| observer.observe(document.getElementsByClassName("c-virtual_list__scroll_container")[0], { childList: true, subtree: true }); | |
| window.console.log("Observer installed"); | |
| do_mod(); | |
| }, 3000); | |
| document.getElementById("msg_input").addEventListener("keyup", function(event) { | |
| var target = event.target; | |
| if (should_input_be_rtl(target)) { | |
| target.style.setProperty("direction", "rtl"); | |
| target.style.setProperty("text-align", "right"); | |
| fix_all_emojis(target); | |
| } else { | |
| target.style.setProperty("direction", "ltr"); | |
| target.style.setProperty("text-align", "left"); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment