-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext-replacer.js
281 lines (227 loc) · 7.87 KB
/
text-replacer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
let replaceWords = {};
let USE_CAPITALIZE_WORDS = false;
let USE_BACKSPACE_INSTEAD_ESC = false;
function updateReplaceWords() {
browser.runtime.sendMessage({'action': 'get_list'}).then((data) => {
// console.log('force update', data);
replaceWords = data;
});
}
// get config
function canCapitalize() {
browser.storage.local.get('can_capitalize')
.then(can_capitalize => {
USE_CAPITALIZE_WORDS = !can_capitalize.can_capitalize;
})
}
function useBackspaceInsteadEsc() {
browser.storage.local.get('esc_cancel')
.then(esc_cancel => {
USE_BACKSPACE_INSTEAD_ESC = esc_cancel.esc_cancel;
})
}
// @see https://stackoverflow.com/a/33704783
function capitalizeFirstLetter(string) {
if (USE_CAPITALIZE_WORDS)
return string[0].toUpperCase() + string.slice(1);
return string;
}
function beforeIsPoint(string, start, end) {
if (!USE_CAPITALIZE_WORDS)
return;
let s = string.slice(start, end).trim();
return s[s.length-1] == '.';
}
// do it manually
updateReplaceWords();
canCapitalize();
useBackspaceInsteadEsc();
// run when the storage is updated
browser.storage.onChanged.addListener(() => {
// update words and settings
updateReplaceWords();
canCapitalize();
useBackspaceInsteadEsc();
});
let BACKSPACE_KEY = 8,
SPACE_KEY = 32,
ENTER_KEY = 13,
NON_ALPHANUM_KEY = 0,
ESC_KEY = 27,
A_SHIFT_KEY = 65,
A_KEY = 97;
var addTextBetween = function(text, p1, p2, p3, p4, newPart) {
return text.substr(p1, p2) + newPart + text.substr(p3, p4);
}
var textReplacer = function(element, wordsToReplace, typedWord, way_back) {
'use strict';
// default value
way_back = way_back ? way_back : 0;
if (typedWord.length == 0)
return;
let stringTyped = typedWord.join('');
if (Object.keys(wordsToReplace).indexOf(stringTyped) == -1 || way_back && !Object.keys(wordsToReplace).find(key => key === stringTyped))
return;
let initialPoint,
beforeWord,
afterWord,
finalPoint,
replacement,
value,
selectionRange;
let space_size = 1;
if (element.isContentEditable) {
selectionRange = document.getSelection();
// github usa CodeMirror
// https://stackoverflow.com/9a/24987585/3618650 - https://stackoverflow.com/a/42675264/3618650
// https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically/19883789#19883789
value = element.textContent;
initialPoint = 0;
beforeWord = selectionRange.getRangeAt(0).endOffset - typedWord.length;
afterWord = selectionRange.getRangeAt(0).endOffset;
finalPoint = value.length;
replacement = unescape(wordsToReplace[stringTyped]);
// return to shortcut
if (way_back) {
// change what is needed
beforeWord = selectionRange.getRangeAt(0).endOffset - wordsToReplace[stringTyped].length - space_size;
replacement = stringTyped;
}
} else {
// all kind of inputs
value = element.value;
initialPoint = 0;
beforeWord = element.selectionStart - typedWord.length;
afterWord = element.selectionStart;
finalPoint = element.textLength;
// @todo remove unescape from here
replacement = unescape(wordsToReplace[stringTyped]);
// return to shortcut
if (way_back) {
// change what is needed
beforeWord = element.selectionStart - wordsToReplace[stringTyped].length - space_size;
replacement = stringTyped;
}
}
if (!way_back && (beforeWord == initialPoint || beforeIsPoint(value, initialPoint, beforeWord))) {
replacement = capitalizeFirstLetter(replacement);
}
// console.log(value,initialPoint,beforeWord,afterWord,finalPoint,replacement);
let newContent = addTextBetween(
value,
initialPoint,
beforeWord,
afterWord,
finalPoint,
replacement
);
// console.log(newContent);
if (element.isContentEditable) {
element.textContent = newContent;
} else {
element.value = newContent;
}
// console.log(element.value, element.textContent);
let cursor;
if (way_back) {
cursor = afterWord + (stringTyped.length - wordsToReplace[stringTyped].length) - space_size;
} else {
cursor = afterWord + (replacement.length - stringTyped.length)
}
// console.log(cursor);
if (element.isContentEditable) {
let new_range = document.createRange();
new_range.setStart(element.childNodes[0], cursor);
new_range.setEnd(element.childNodes[0], cursor);
selectionRange.removeAllRanges();
selectionRange.addRange(new_range);
} else {
element.setSelectionRange(cursor, cursor);
}
}
var settingUpReplacer = function() {
'use strict';
// init word
let word = [];
let old_word = [];
return function(event) {
let key = event.which;
// osx's user => windows: event.ctrlKey
let superKey = event.metaKey;
// console.log('keypress');
// console.log(key);
switch(key) {
case BACKSPACE_KEY:
/* cancel replace using backspace */
if (USE_BACKSPACE_INSTEAD_ESC && old_word.length > 0) {
textReplacer(event.target, replaceWords, old_word, 1);
old_word = word = [];
event.preventDefault();
return;
}
if (!superKey && word.length)
word.pop();
if (superKey
// when the user select a part of the text and delete it
|| event.target.selectionStart != event.target.selectionEnd)
word = [];
break;
case ENTER_KEY:
case SPACE_KEY:
textReplacer(event.target, replaceWords, word);
old_word = word;
word = [];
break;
case NON_ALPHANUM_KEY:
// check if arrow is pressed
// that helps cleaning the wordsToReplace
if (event.code.toLowerCase().indexOf('arrow') >= 0)
word = [];
/* ESC pressed */
// return the replaced word
if (event.keyCode == ESC_KEY && !USE_BACKSPACE_INSTEAD_ESC) {
if (old_word.length > 0)
textReplacer(event.target, replaceWords, old_word, 1);
old_word = word = [];
}
break;
default:
if (superKey && (key == A_SHIFT_KEY || key == A_KEY))
// cleans words, because the user selected the whole text
word = [];
else
// default behavior
word.push(String.fromCharCode(key));
break;
}
}
}
/* constante which checks if the website is supported by the plugin */
const websitesNotWorking = !(window.location.href.indexOf('facebook') >= 0
|| window.location.href.indexOf('messenger') >= 0
|| window.location.href.indexOf('github') >= 0
|| window.location.href.indexOf('slack') >= 0);
function isSupportedElement(e) {
return websitesNotWorking
&& (e.isContentEditable
|| e.tagName.toLowerCase() == 'input'
|| e.tagName.toLowerCase() == 'textarea');
}
let replacerFnc = settingUpReplacer();
document.body.addEventListener('focus', function(e) {
let elem = e.target
if (isSupportedElement(elem)) {
elem.addEventListener('keypress', replacerFnc, true)
}
}, true);
document.body.addEventListener('blur', function(e) {
let elem = e.target
if (isSupportedElement(elem)) {
elem.removeEventListener('keypress', replacerFnc, true)
}
}, true);
// page already have an element in focus
if (isSupportedElement(document.activeElement)) {
document.activeElement.addEventListener('keypress', replacerFnc, true);
document.activeElement.addEventListener('blur', e => e.target.removeEventListener('keypress', replacerFnc, true), true);
}