Skip to content

Instantly share code, notes, and snippets.

@emanamini
Last active October 26, 2017 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emanamini/4a03158acc76e459c7fb1252b3d65eee to your computer and use it in GitHub Desktop.
Save emanamini/4a03158acc76e459c7fb1252b3d65eee to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Standardnotes RTL Support
// @namespace https://ilola.ir
// @version 1.1
// @description Change directions of Arabic and Persian lines.
// @author Eman Amini
// @match https://app.standardnotes.org/
// @grant none
// ==/UserScript==
var css = ".rtl {direction: rtl;} strong, b {font-weight: 900;} #note-text-editor {font-family: IRANSansWebNoEn, DroidSans, sans-serif !important; text-align: justify;}",
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 checkLtr(character) {
var LTR = new RegExp('[a-zA-Z]', 'g');
if (LTR.test(character)) {
return true;
} else {
return false;
}
}
function init() {
//Check existed text and RTL it if it's needed
y = document.getElementById("note-text-editor").value;
eVal(y, 0, "note-text-editor");
//Check Text on real time
document.getElementById("note-text-editor").onkeyup = function() {
eVal(document.getElementById("note-text-editor").value, 1, "note-text-editor");
};
//Check existed title and RTL it if it's needed
x = document.getElementById("note-title-editor").value;
eVal(x, 0, "note-title-editor");
//Check Title on real time
document.getElementById("note-title-editor").onkeyup = function() {
eVal(document.getElementById("note-title-editor").value, 0, "note-title-editor");
};
}
function eVal(x, counter, id) {
var fa = 0;
var en = 0;
if ((counter === 0) || (x.length < 100) || (x.length < 500 && x % 5 === 0) || (x.length % 20 === 0)) {
for (var i = 0; i < x.length; i++) {
var eachchar = x[i];
if (checkRtl(eachchar)) {
fa = fa + 1;
} else if (checkLtr(eachchar)) {
en = en + 1;
}
}
if (fa > en) {
document.getElementById(id).classList.remove("rtl");
document.getElementById(id).classList.add("rtl");
} else {
document.getElementById(id).classList.remove("rtl");
}
}
}
setTimeout(init, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment