-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_scripts.js
More file actions
79 lines (69 loc) · 2.41 KB
/
content_scripts.js
File metadata and controls
79 lines (69 loc) · 2.41 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
window.onload = () => {
setInterval(() => {
try {
let chatframe = document.getElementById("chatframe");
let el;
let inputContainer;
let _window;
// 새 창에서 새 탭 열기 -> chatframe이 존재하지 않음
// null값을 체크해 inputContainer를 document에서 찾을지, el에서 찾을지 지정
if (chatframe == null) {
inputContainer = document.querySelector("#input");
_window = window;
} else {
el = chatframe.contentWindow.document;
inputContainer = el.querySelectorAll("#input")[1];
_window = chatframe.contentWindow;
}
let input = inputContainer.children[1];
// focus 이벤트
inputContainer.addEventListener("focusin", function (e) {
e.preventDefault();
addPrefix(inputContainer, input, _window);
});
// click 이벤트
inputContainer.addEventListener("click", function (e) {
e.preventDefault();
addPrefix(inputContainer, input, _window);
});
// enter 이벤트
inputContainer.addEventListener("keyup", function (e) {
if (e.keyCode === 13 && inputContainer.hasAttribute("focused")) {
e.preventDefault();
addPrefix(inputContainer, input, _window);
}
});
} catch (err) {
console.log(err);
}
}, 500);
};
// prefix 삽입 함수
function addPrefix(inputContainer, input, _window) {
try {
// On Off 상태 획득
chrome.storage.local.get(["toggleState"], function (toggleResult) {
// unchecked or undefined 시 리턴
if (!toggleResult) return;
if (toggleResult.toggleState != true) return;
chrome.storage.local.get(["prefix"], function (result) {
let prefix = result.prefix + "\u00a0";
let inputText = input.innerText;
if (!prefix || inputText) {
return;
}
inputContainer.setAttribute("has-text", true);
input.removeAttribute("aria-invalid");
input.innerText = `${prefix}`;
// 커서를 맨 뒤로 이동 -- start
const range = document.createRange();
range.setStart(input.childNodes[0], prefix.length);
range.setEnd(input.childNodes[0], prefix.length);
const selection = _window.document.getSelection();
selection.removeAllRanges();
selection.addRange(range);
// 커서를 맨 뒤로 이동 -- end
});
});
} catch (err) {}
}