مستخدم:أنون/Numeral-converter.js
المظهر
يمكن توفير توثيق لسكربت المستخدم هذا في الصفحة : مستخدم:أنون/Numeral-converter. |
ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.
/**
* Numeral converter
* forked from [[mw:MediaWiki:Gadget-Numerakri.js]]
* maintainer حبيشان
*/
mw.loader.using(['mediawiki.util']).then(function () {
'use strict';
const skipclass = 'mwgadget-numconv-skip',
maps = [
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%'],
['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩', '٪']
],
rejectedTags = ['input', 'textarea', 'style', 'script', 'pre'];
var walker;
/**
* @param {HTMLElement|TextNode} node
* @return {number} NodeFilter.FILTER_* constant
*/
function filterNode(node) {
if (node.nodeType === Node.TEXT_NODE) {
return NodeFilter.FILTER_ACCEPT;
}
var n = node.nodeName && node.nodeName.toLowerCase();
if ( rejectedTags.indexOf(n) > -1 ||
// node.hasAttribute('contenteditable') ||
$(node).hasClass(skipclass)
) {
// Skip this element and skip its children
return NodeFilter.FILTER_REJECT;
}
// Skip this element, but check its children
return NodeFilter.FILTER_SKIP;
}
/**
* @param {TextNode} node
*/
function handleTextNode(node) {
var original = node.nodeValue,
changed = original,
// matchers = getMatchers(currentType),
i = 0;
for (; i <= 10; i++) {
changed = changed.replaceAll(maps[0][i], maps[1][i]);
}
if (original !== changed) {
node.nodeValue = changed;
}
}
// https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw-method-requestIdleCallback
function idleWalker(deadline) {
var el;
if (!walker) {
return;
}
while (deadline.timeRemaining() > 0) {
el = walker.nextNode();
if (!el) {
// Reached the end
walker = null;
return;
}
handleTextNode(el);
}
// The user may interact with the page. We pause so the browser can process
// interaction. The text handler will continue after that.
if (walker) {
mw.requestIdleCallback(idleWalker);
}
}
function startPageConversion() {
$('ol:lang(ar) li, ol.references, li.references').css('list-style-type', 'arabic-indic');
// If a walker is already active, replace it.
// If no walker is active yet, start it.
if (!walker) {
mw.requestIdleCallback(idleWalker);
}
walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, filterNode, false);
}
$(function () {
mw.requestIdleCallback(startPageConversion);
});
});