Repository navigation
Expand file tree
/
Copy pathscript.user.js
More file actions
171 lines (142 loc) · 6.06 KB
/
Copy pathscript.user.js
File metadata and controls
171 lines (142 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ==UserScript==
// @name בני ברק & מתמחים - אימוג'י בהקלדה
// @namespace https://github.com/tsoolgee/IMOGI2
// @version 0.0.1
// @description מחליף מילים לאימוג'ים בזמן הקלדה (עם נקודתיים בתחילה) - תומך בכל סוגי תיבות הטקסט
// @author You
// @match https://bnebrak.com/*
// @match http://mitmachim.top/*
// @match https://mitmachim.top/*
// @match https://forum-gabai.onrender.com/*
// @grant none
// @updateURL https://raw.githubusercontent.com/tsoolgee/IMOGI2/main/script.user.js
// @downloadURL https://raw.githubusercontent.com/tsoolgee/IMOGI2/main/script.user.js
// ==/UserScript==
(function () {
'use strict';
// מערך מסודר מהארוך לקצר למניעת באגים בהחלפה
const map = [
['אני כועס', '😠'],
['גררר', '😠'],
['מחיאות כפיים', '👏'],
['כפיים', '👏'],
['מזל טוב', '🎉'],
['אני עייף', '🥱'],
['אני ישן', '😴'],
['שים לב', '📢'],
['דיסלייק', '👎'],
['קריצה', '😉'],
['מתלבט', '🤔'],
['מגניב', '😎'],
['אונק', '😛'],
['עודכן', '🔄'],
['אזהרה', '⚠️'],
['פתרון', '✅'],
['מחשב', '💻'],
['הודעה', '✉️'],
['חיפוש', '🔍'],
['שעון', '🕒'],
['חחחח', '😂'],
['חחח', '😄'],
['נחש', '🐍'],
['חח', '😊'],
['עצוב', '😞'],
['שמח', '🙂'],
['גרר', '😡'],
['חושב', '🤔'],
['שוקל', '⚖️'],
['חדש', '🆕'],
['לייק', '👍'],
['דחוף', '🚨'],
['שאלה', '❓'],
['עסוק', '💼'],
['ספר', '📖'],
['עט', '✍️'],
['ששש', '🤫'],
['תודה', '👍'],
['כוכב', '⭐'],
['לב', '❤️'],
['רעיון', '💡'],
['אש', '🔥'],
['חריף', '🌶️'],
// ===== חדש =====
['בהצלחה', '🍀'],
['וואו', '😮'],
['בכיתי', '😭'],
['מעולה', '🔥']
];
// flag גלובלי שמונע ריצה כפולה כתוצאה מה-dispatchEvent המלאכותי
let isReplacing = false;
// פונקציית עזר להחלפה בתיבות טקסט רגילות (input / textarea)
function handleStandardInput(el, key) {
if (key !== ' ' && key !== 'Enter') return;
const val = el.value;
const cursor = el.selectionStart;
const textBefore = val.slice(0, cursor);
const triggerChar = textBefore.slice(-1);
if (triggerChar !== ' ' && triggerChar !== '\n') return;
for (const [from, to] of map) {
const target = ':' + from + triggerChar;
if (!textBefore.endsWith(target)) continue;
const wordStart = cursor - target.length;
const charBefore = val[wordStart - 1];
if (charBefore !== undefined && charBefore !== ' ' && charBefore !== '\n') continue;
const newVal = val.slice(0, wordStart) + to + triggerChar + val.slice(cursor);
isReplacing = true;
el.value = newVal;
const newCursor = wordStart + to.length + triggerChar.length;
el.setSelectionRange(newCursor, newCursor);
el.dispatchEvent(new Event('input', { bubbles: true }));
isReplacing = false;
return;
}
}
// פונקציית עזר להחלפה בעורכי טקסט מבוססי HTML (contenteditable)
function handleEditableInput(el, key) {
if (key !== ' ' && key !== 'Enter') return;
const selection = window.getSelection();
if (!selection.rangeCount) return;
const range = selection.getRangeAt(0);
const textNode = range.startContainer;
if (textNode.nodeType !== Node.TEXT_NODE) return;
const text = textNode.nodeValue;
const cursor = range.startOffset;
const textBefore = text.slice(0, cursor);
const triggerChar = textBefore.slice(-1);
if (triggerChar !== ' ' && triggerChar !== '\n' && triggerChar !== '\u00A0') return; // \u00A0 הוא רווח HTML נפוץ
for (const [from, to] of map) {
const target = ':' + from + triggerChar;
if (!textBefore.endsWith(target)) continue;
const wordStart = cursor - target.length;
const charBefore = text[wordStart - 1];
if (charBefore !== undefined && charBefore !== ' ' && charBefore !== '\n' && charBefore !== '\u00A0') continue;
const newVal = text.slice(0, wordStart) + to + triggerChar + text.slice(cursor);
isReplacing = true;
textNode.nodeValue = newVal;
const newRange = document.createRange();
const newOffset = wordStart + to.length + triggerChar.length;
newRange.setStart(textNode, newOffset);
newRange.setEnd(textNode, newOffset);
selection.removeAllRanges();
selection.addRange(newRange);
el.dispatchEvent(new Event('input', { bubbles: true }));
isReplacing = false;
return;
}
}
// האזנה גלובלית לאירועי הקלדה
let lastKey = '';
document.addEventListener('keydown', function (e) {
lastKey = e.key;
}, true);
document.addEventListener('input', function (e) {
if (isReplacing) return; // מונע ריצה חוזרת כתוצאה מה-dispatchEvent המלאכותי
const target = e.target;
if (!target) return;
if (target.tagName === 'TEXTAREA' || target.tagName === 'INPUT') {
handleStandardInput(target, lastKey);
} else if (target.isContentEditable || target.closest('[contenteditable="true"]')) {
handleEditableInput(target, lastKey);
}
}, true);
})();