Last active
October 26, 2017 23:21
-
-
Save emanamini/4a03158acc76e459c7fb1252b3d65eee to your computer and use it in GitHub Desktop.
This file contains 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 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