Skip to content

Commit ffc5809

Browse files
committed
feat: add observe location state
1 parent 7cdc3d5 commit ffc5809

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

.eslintrc.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "@crashmax/eslint-config"
2+
"extends": "@crashmax/eslint-config",
3+
"rules": {
4+
"@typescript-eslint/unbound-method": "off"
5+
}
36
}

src/chat-observer.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import Store from './store'
22

3-
interface ChatObserver {
4-
chat: HTMLElement
5-
update: (message: HTMLElement) => void
6-
}
7-
8-
export function chatObserver({ chat, update }: ChatObserver): void {
3+
export function chatObserver(callback: (message: HTMLElement) => void): MutationObserver {
94
const observer = new MutationObserver((mutations) => {
105
for (const mutation of mutations) {
116
for (const element of mutation.addedNodes) {
@@ -15,13 +10,11 @@ export function chatObserver({ chat, update }: ChatObserver): void {
1510
const { self } = Store.value
1611

1712
if (chatMessage && (self || highlightMessage)) {
18-
update(message)
13+
callback(message)
1914
}
2015
}
2116
}
2217
})
2318

24-
observer.observe(chat, {
25-
childList: true
26-
})
19+
return observer
2720
}

src/index.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,37 @@ import './settings'
44
import './styles.scss'
55

66
window.addEventListener('load', () => {
7-
const chat = document
8-
.querySelector<HTMLElement>('.chat-scrollable-area__message-container')
7+
const observe = chatMount()
8+
const { history } = window
9+
const { pushState, replaceState } = history
910

10-
if (chat) {
11-
chatObserver({
12-
chat,
13-
update: (message) => {
14-
message.addEventListener('mouseenter', enterEvent)
15-
message.addEventListener('click', clickEvent)
16-
}
17-
})
11+
history.pushState = (...args) => {
12+
pushState.apply(history, args)
13+
observe()
1814
}
15+
16+
history.replaceState = (...args) => {
17+
replaceState.apply(history, args)
18+
observe()
19+
}
20+
21+
observe()
1922
})
23+
24+
function chatMount(): () => void {
25+
const observer = chatObserver((message) => {
26+
message.addEventListener('mouseenter', enterEvent)
27+
message.addEventListener('click', clickEvent)
28+
})
29+
30+
return () => {
31+
const chat = document
32+
.querySelector<HTMLElement>('.chat-scrollable-area__message-container')
33+
34+
if (chat) {
35+
observer.observe(chat, {
36+
childList: true
37+
})
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)