-
Notifications
You must be signed in to change notification settings - Fork 0
/
trimtr.js
39 lines (36 loc) · 1.31 KB
/
trimtr.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
const triggerEvent = (el, ev) => {
const evt = new Event(ev, {bubbles: true});
el.dispatchEvent(evt);
}
window.addEventListener('load', () => {
chrome.runtime.sendMessage({ action: "judgeDomain" });
chrome.runtime.onMessage.addListener(message => {
let textarea;
if (message.target == "google") {
textarea = document.querySelector("textarea");
} else if (message.target == "deepl") {
textarea = document.getElementsByClassName("lmt__source_textarea")[0];
}
textarea.addEventListener('change', e => {
const text = e.target.value;
fetch('https://trimtr.herokuapp.com/trim', {
method: 'post',
headers: {
"Content-Type": "text/plain; charset=UTF-8"
},
body: text
})
.then((res) => {
if (!res.ok) {
throw new Error(`Request failed: ${res.status}`);
}
res.json()
.then((data) => {
textarea.value = data[0].text;
triggerEvent(textarea, 'input');
})
})
.catch((err) => console.log(err));
});
});
});