-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
164 lines (153 loc) · 3.61 KB
/
content.js
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
let isShiftMode = false;
let isBalochiMode = false; // Track Balochi input mode
// Balochi character mapping
const normalMap = {
q: "ق",
w: "و",
e: "ع",
r: "ر",
t: "ت",
y: "ے",
u: "ء",
i: "ی",
o: "ہ",
p: "پ",
a: "ا",
s: "س",
d: "د",
f: "ف",
g: "گ",
h: "ھ",
j: "ج",
k: "ک",
l: "ل",
z: "ز",
x: "ش",
c: "چ",
v: "ط",
b: "ب",
n: "ن",
m: "م",
Q: "ءُ",
W: "ؤ",
E: "ءِ",
R: "ڑ",
T: "ٹ",
Y: "ۓ",
U: "ٰ",
I: "ئ",
O: "ْ",
P: "ُ",
A: "آ",
S: "ص",
D: "ڈ",
F: "ءَ",
G: "غ",
H: "ح",
J: "ض",
K: "خ",
L: "ئِے",
Z: "ذ",
X: "ژ",
C: "ث",
V: "ظ",
B: "یٔ",
N: "ں",
M: "اَنت",
"`": "ً",
";": "؛",
"'": "ۂ",
",": "،",
".": "۔",
"/": "ْ",
};
const shiftMap = {
",": "ِ",
"<": "ِ",
".": "َ",
">": "َ",
"/": "؟",
"?": "؟",
";": ":",
":": ":",
"'": "‘",
'"': "‘",
0: "ّ",
")": "ّ",
};
// Convert English text to Balochi
const convertToBalochi = (text) => {
let balochiText = "";
for (const char of text) {
if (isShiftMode && shiftMap[char]) {
balochiText += shiftMap[char];
} else if (normalMap[char]) {
balochiText += normalMap[char];
} else {
balochiText += char; // Keep unchanged if no mapping
}
}
return balochiText;
};
// Listen for keyboard events on any input field
document.addEventListener("keydown", (e) => {
// Check for Ctrl + Space to toggle Balochi mode
if (e.ctrlKey && e.keyCode === 32) {
e.preventDefault(); // Prevent the default action (e.g., scrolling)
isBalochiMode = !isBalochiMode; // Toggle Balochi mode
const modeMessage = isBalochiMode
? "Balochi Input Mode: ON"
: "Balochi Input Mode: OFF";
showNotification(modeMessage); // Notify the user
}
// Handle Shift key for uppercase in Balochi
if (isBalochiMode && (e.key === "Shift" || e.getModifierState("CapsLock"))) {
isShiftMode = true;
}
});
document.addEventListener("keyup", (e) => {
if (isBalochiMode && (e.key === "Shift" || e.getModifierState("CapsLock"))) {
isShiftMode = false;
}
});
document.addEventListener("input", (e) => {
const target = e.target;
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA") {
const { value, selectionStart } = target;
// If Balochi mode is active, convert the input
if (isBalochiMode) {
target.setAttribute("dir", "rtl"); // Set direction to RTL
const newValue = convertToBalochi(
value.slice(selectionStart - 1, selectionStart)
);
if (newValue !== value.charAt(selectionStart - 1)) {
target.value =
value.slice(0, selectionStart - 1) +
newValue +
value.slice(selectionStart);
target.setSelectionRange(selectionStart, selectionStart); // Keep cursor in the right position
}
} else {
target.setAttribute("dir", "unset"); // Reset to LTR when not in Balochi mode
}
}
});
// Create a notification element for the input mode
const notification = document.createElement("div");
notification.style.position = "fixed";
notification.style.bottom = "20px";
notification.style.right = "10px";
notification.style.padding = "5px 10px";
notification.style.backgroundColor = "#000";
notification.style.color = "#fff";
notification.style.borderRadius = "5px";
notification.style.display = "none";
notification.style.zIndex = 9999;
document.body.appendChild(notification);
const showNotification = (message) => {
notification.innerText = message;
notification.style.display = "block";
setTimeout(() => {
notification.style.display = "none";
}, 3000);
};