Skip to content

Instantly share code, notes, and snippets.

@kholis
Created April 19, 2016 13:17
Show Gist options
  • Save kholis/1dc2bce67867aa3d5bfe68ce2c4b4d26 to your computer and use it in GitHub Desktop.
Save kholis/1dc2bce67867aa3d5bfe68ce2c4b4d26 to your computer and use it in GitHub Desktop.
javascript using tampermonkey or greasemonkey
// ==UserScript==
// @name Arabic font size fixer
// @namespace Arminius99
// @include *.wikipedia.org*
// @include *.wiktionary.org*
// @include *.aljazeera.net*
// @include *reddit.com*
// @include *web.whatsapp.com*
// @include *.wordpress.com*
// @include *.facebook.com*
// @description Increases the font size of Arabic text
// @grant GM_addStyle
// ==/UserScript==
// adjust font, font size and font color according to your preferences
//GM_addStyle("span[lang~='ar'] {font-family: 'Droid Arabic Naskh'; direction: rtl; font-size: 100% !important; }");
//GM_addStyle("span[lang~='ar'] {font-family: 'Sakkal Majalla'; direction: rtl; font-size: 130% !important; }");
GM_addStyle("span[lang~='ar'] {font-family: 'Traditional Arabic'; direction: rtl; font-size: x-large !important; }");
// code by James Padolsey; http://james.padolsey.com/javascript/find-and-replace-text-with-javascript/
function findAndReplace(searchText, replacement, searchNode) {
if (!searchText || typeof replacement === 'undefined') {
// Throw error here if you want...
return;
}
var regex = typeof searchText === 'string' ?
new RegExp(searchText, 'g') : searchText,
childNodes = (searchNode || document.body).childNodes,
cnLength = childNodes.length,
excludes = 'html';
while (cnLength--) {
var currentNode = childNodes[cnLength];
if (currentNode.nodeType === 1 &&
(excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
arguments.callee(searchText, replacement, currentNode);
}
if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
continue;
}
var parent = currentNode.parentNode,
frag = (function(){
var html = currentNode.data.replace(regex, replacement),
wrap = document.createElement('div'),
frag = document.createDocumentFragment();
wrap.innerHTML = html;
while (wrap.firstChild) {
frag.appendChild(wrap.firstChild);
}
return frag;
})();
parent.insertBefore(frag, currentNode);
parent.removeChild(currentNode);
}
}
findAndReplace('([\u0600-\u06FF]+( [\u0600-\u06FF]+)*)', '<span lang="ar">$1</span>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment