-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand.js
51 lines (40 loc) · 984 Bytes
/
expand.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
function getPairs() {
return new Promise((resolve, reject) => {
const result = {};
chrome.storage.local.get(null, (entries) => {
for (let [k, v] of Object.entries(entries)) {
Object.assign(result, {[k]: v});
}
resolve(result);
});
});
}
async function expand() {
let pairs = await getPairs();
let focus = document.activeElement;
let i;
let j;
let str;
let len;
if ((focus.type === 'text') || (focus.type === 'textarea')) {
i = focus.selectionStart;
j = focus.selectionEnd;
str = focus.value
len = str?.length ?? focus.textLength;
}
while (i > 0 && (str.charAt(i-1) !== ' ') && (str.charAt(i-1) !== '\n')) {
i -= 1;
}
while (j <= len && (str.charAt(j) !== ' ') && (str.charAt(j) !== '\n')) {
j += 1;
}
sub = str.slice(i, j);
let replace;
if (sub in pairs) {
replace = pairs[sub];
} else {
return;
}
focus.setRangeText(replace, i, j);
}
expand()