Skip to content

Instantly share code, notes, and snippets.

@danlei
Last active May 20, 2020 15:08
Show Gist options
  • Save danlei/17478a08b53ba9367e83111104bfa002 to your computer and use it in GitHub Desktop.
Save danlei/17478a08b53ba9367e83111104bfa002 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Enlarge Arabic
// @namespace danlei
// @author danlei
// @version 1.1.5
// @updateURL https://gist.githubusercontent.com/danlei/17478a08b53ba9367e83111104bfa002/raw
// @installURL https://gist.githubusercontent.com/danlei/17478a08b53ba9367e83111104bfa002/raw
// @include /^https?://.*$/
// @exclude https://twitter.com*
// @description Increase Arabic font size relatively (adapted from Arminius99's script)
// @grant GM_addStyle
// ==/UserScript==
// TODO: - what's wrong with twitter?
// - test arabic in nested elements
GM_addStyle("span[lang~='ar'] {direction: rtl; font-size: 110% !important; }");
// cf. https://stackoverflow.com/a/58166435/271324
function addArabicSpan(textNode) {
var div = document.createElement('div');
textNode.parentNode.insertBefore(div, textNode);
div.insertAdjacentHTML('afterend',
textNode.data.replace(/((?=\s*[\u0600-\u06FF])[\u0600-\u06FF\s]+)/g,
"<span lang='ar'>$1</span>"));
div.remove();
textNode.remove();
}
[...document.querySelectorAll('*')].
map(n => [...n.childNodes]).
flat().
filter(n => n.nodeType === 3).
map(addArabicSpan);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment