Skip to content

Commit e4d0025

Browse files
committed
fix: 在题库页和题单页跳转的时候有时候题单列表会发生错位
1 parent 2bf33e1 commit e4d0025

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

src/content/pages/problem-list/App.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import Rank from '../problemset/Rank'
22
import { FC, useEffect, useState } from 'react'
33
import { useAppSelector, useEffectMount } from '@/hooks'
44
import { selectOptions } from '../global/optionsSlice'
5-
import { awaitFn, findElementByXPath, problemsetPageIsLoad } from '@/utils'
5+
import { awaitFn, problemsetPageIsLoad } from '@/utils'
66
import { Portal } from '@/components/Portal'
77
import ProblemList from './ProblemList'
88
import { withPage } from '@/hoc'
99
import { fixRandom } from './fixRandom'
10+
import { useSetProblemListRoot } from './useSetProblemListRoot'
1011

1112
const App: FC = () => {
1213
const options = useAppSelector(selectOptions)
@@ -19,22 +20,10 @@ const App: FC = () => {
1920
})
2021
if (state.isMount) setIsLoad(true)
2122
})
22-
23-
useEffectMount(
24-
async state => {
25-
if (!isLoad) return
26-
const problemListXPath =
27-
'//*[@id="__next"]/div/div[2]/div/div[2]/div/*//span[text()="精选题单"]/../..'
28-
const el = await findElementByXPath(problemListXPath)
29-
30-
if (state.isMount) {
31-
const root = document.createElement('div')
32-
el.parentNode?.insertBefore(root, el)
33-
setProblemListRoot(root)
34-
state.unmount.push(() => root.remove())
35-
}
36-
},
37-
[isLoad]
23+
useSetProblemListRoot(
24+
'//*[@id="__next"]/div/div[2]/div/div[2]/div/*//span[text()="精选题单"]/../..',
25+
isLoad,
26+
setProblemListRoot
3827
)
3928

4029
useEffect(() => {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useEffectMount } from '@/hooks'
2+
import { findElementByXPath } from '@/utils'
3+
import { debounce } from 'src/utils'
4+
5+
export const useSetProblemListRoot = (
6+
problemListXPath: string,
7+
isLoad: boolean,
8+
setProblemListRoot: (root: HTMLElement) => void
9+
): void => {
10+
useEffectMount(
11+
async state => {
12+
if (!isLoad) return
13+
const handleMount = async () => {
14+
const el = await findElementByXPath(problemListXPath)
15+
16+
if (state.isMount && el.parentNode) {
17+
const root = document.createElement('div')
18+
el.parentNode.insertBefore(root, el)
19+
setProblemListRoot(root)
20+
state.unmount.push(() => root.remove())
21+
const handleChange = debounce(async () => {
22+
const el = await findElementByXPath(problemListXPath)
23+
24+
if (el.previousSibling === root) return
25+
if (!state.isMount) return
26+
observer.disconnect()
27+
root.remove()
28+
state.unmount = []
29+
handleMount()
30+
}, 100)
31+
const observer = new MutationObserver(handleChange)
32+
observer.observe(el.parentNode, { childList: true })
33+
}
34+
}
35+
handleMount()
36+
},
37+
[isLoad]
38+
)
39+
}

src/content/pages/problemset/App.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,25 @@ import './intercept'
33
import { FC, useState } from 'react'
44
import { useAppSelector, useEffectMount } from '@/hooks'
55
import { selectOptions } from '../global/optionsSlice'
6-
import { awaitFn, findElementByXPath, problemsetPageIsLoad } from '@/utils'
6+
import { awaitFn, problemsetPageIsLoad } from '@/utils'
77
import { Portal } from '@/components/Portal'
88
import ProblemList from '../problem-list/ProblemList'
99
import { withPage } from '@/hoc'
10+
import { useSetProblemListRoot } from '../problem-list/useSetProblemListRoot'
1011

1112
const App: FC = () => {
1213
const options = useAppSelector(selectOptions)
1314
const [problemListRoot, setProblemListRoot] = useState<HTMLElement>()
1415
const [isLoad, setIsLoad] = useState(false)
15-
console.log(isLoad)
16+
1617
useEffectMount(async state => {
1718
await awaitFn(() => problemsetPageIsLoad())
1819
if (state.isMount) setIsLoad(true)
1920
})
20-
21-
useEffectMount(
22-
async state => {
23-
if (!isLoad) return
24-
const problemListXPath =
25-
'//*[@id="__next"]/*//span[text()="精选题单"]/../../..'
26-
const el = await findElementByXPath(problemListXPath)
27-
if (state.isMount) {
28-
const root = document.createElement('div')
29-
el.parentNode?.insertBefore(root, el)
30-
setProblemListRoot(root)
31-
state.unmount.push(() => root.remove())
32-
}
33-
},
34-
[isLoad]
21+
useSetProblemListRoot(
22+
'//*[@id="__next"]/*//span[text()="精选题单"]/../../..',
23+
isLoad,
24+
setProblemListRoot
3525
)
3626

3727
if (!isLoad) return null

0 commit comments

Comments
 (0)