Skip to content

Commit 6f97f4e

Browse files
committed
fix: 修复跳转逻辑
1 parent cbc82f8 commit 6f97f4e

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import { render, unmountComponentAtNode } from 'react-dom'
22

3-
import {
4-
autoMount,
5-
awaitFn,
6-
findElementByXPath,
7-
pageIsLoad,
8-
sleep,
9-
} from '@/utils'
3+
import { autoMount, findElementByXPath, sleep } from '@/utils'
104

115
import App from './App'
126
import { fixRandom, removeFixRandom } from './fixRandom'
137
import RankApp from '../problemset/App'
148

159
let _root: HTMLDivElement | null = null,
1610
rankTitle: HTMLDivElement | null = null
17-
11+
const xpath =
12+
'//*[@id="__next"]/div/div[2]/div/div[2]/div/*//span[text()="精选题单"]/../..'
1813
function getListEl() {
19-
return findElementByXPath(
20-
'//*[@id="__next"]/*//span[text()="精选题单"]/../..'
21-
)
14+
return findElementByXPath(xpath)
2215
}
2316

2417
const [mountProblemList, unmountProblemList] = autoMount(
25-
'//*[@id="__next"]/*//span[text()="精选题单"]/../..',
18+
xpath,
2619
async () => {
2720
const el = await getListEl()
21+
if (!isProblemList()) return
2822
if (_root) {
2923
if (_root?.nextElementSibling === el) return
3024
// 可能因为加载比较慢之类的原因,导致自定义题单的侧边栏已经加载而其他的一些元素还没加载,
@@ -97,10 +91,10 @@ window.addEventListener('urlchange', async function () {
9791
await getListEl()
9892
await Promise.race([
9993
sleep(500),
100-
findElementByXPath('//span[text()="分享"]'),
94+
findElementByXPath(
95+
'//*[@id="__next"]/div/div[2]/div/div[2]/div/*//span[text()="分享"]'
96+
),
10197
])
102-
// 等待跳转到题单页,通过判断「导航栏的题单按钮是否取消高亮」
103-
await awaitFn(async () => !(await pageIsLoad('题库')))
10498
mount()
10599
fixRandom()
106100
} else {

src/content/pages/problemset/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { render, unmountComponentAtNode } from 'react-dom'
2-
import {
3-
autoMount,
4-
awaitFn,
5-
findElementByXPath,
6-
pageIsLoad,
7-
sleep,
8-
} from '@/utils'
2+
import { autoMount, awaitFn, findElementByXPath, pageIsLoad } from '@/utils'
93
import ProblemListApp from '../problem-list/App'
104
import App from './App'
115
import './intercept'
@@ -16,6 +10,7 @@ const [mountProblemList, unmountProblemList] = autoMount(
1610
problemListXPath,
1711
async () => {
1812
const el = await findElementByXPath(problemListXPath)
13+
if (!isProblemset()) return
1914
if (_root) {
2015
if (_root?.nextElementSibling === el) return
2116
// 可能因为加载比较慢之类的原因,导致自定义题单的侧边栏已经加载而其他的一些元素还没加载,
@@ -104,7 +99,6 @@ void (async function main() {
10499

105100
window.addEventListener('urlchange', async function () {
106101
if (isProblemset()) {
107-
await sleep(500)
108102
await awaitFn(() => pageIsLoad('题库'))
109103
mount()
110104
} else {

src/content/utils/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export async function findElementByXPath<T = HTMLElement | HTMLElement[]>(
143143
} else {
144144
xpath = evaluateParam.xpath
145145
nodeType = evaluateParam.nodeType
146-
fn = el => !!(Array.isArray(el) && el.length)
146+
if (!fn) fn = el => !!(Array.isArray(el) && el.length)
147147
}
148148
const element = await findBase<T>(
149149
() => {
@@ -334,6 +334,7 @@ export function autoMount(
334334

335335
const mountFn = debounce(async () => {
336336
await unmount?.()
337+
if (!_observer) return
337338
mount()
338339
}, 100)
339340
mountFn()
@@ -344,6 +345,7 @@ export function autoMount(
344345
},
345346
() => {
346347
_observer?.disconnect()
348+
_observer = null
347349
unmount?.()
348350
},
349351
]

0 commit comments

Comments
 (0)