compare above code with below: [code]
- User
compare above code with below:
code// ==UserScript== // @name Perso-Arabic Character and Digit Converter - Optimized // @namespace http://tampermonkey.net/ // @version 1.1 // @description Convert ی, ک to ي, ك and Persian/Arabic digits to English digits in input and textarea with event delegation and efficient replacement // @author Your Name // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // Map for digits replacement: Persian & Arabic-Indic digits to ASCII digits const digitMap = { '۰': '0','١': '1', '۱': '1', '۲': '2','٢': '2', '۳': '3','٣': '3', '۴': '4','٤': '4', '۵': '5','٥': '5', '۶': '6','٦': '6', '۷': '7','٧': '7', '۸': '8','٨': '8', '۹': '9','٩': '9', '٠': '0' }; // Regex: match ی(06CC), ک(06A9), Persian digits, Arabic-Indic digits, Arabic digits if any const regex = /[\u06cc\u06a9۰-۹٠-٩]/g; function convertText(text) { return text.replace(regex, ch => { if (ch === '\u06cc') return '\u064a'; // ی -> ي if (ch === '\u06a9') return '\u0643'; // ک -> ك return digitMap[ch] || ch; }); } function onInput(e) { const el = e.target; if (!((el.tagName === 'INPUT' && (el.type === 'text' || el.type === 'search' || el.type === 'tel' || el.type === 'url' || el.type === 'password' || el.type === 'email')) || el.tagName === 'TEXTAREA')) { return; // Ignore other inputs like checkbox, number, etc. } const val = el.value; const converted = convertText(val); if (val !== converted) { // Preserve cursor position relative to changes const start = el.selectionStart; const end = el.selectionEnd; el.value = converted; // Restore selection if possible try { el.setSelectionRange(start, end); } catch {} } } // Use event delegation: listen on document for input events document.addEventListener('input', onInput, true); })(); - Assistant
- User
- Assistant
- User
thanks for compare 2 codes, but script1 not work at all. why?
- Assistant