Skip to content

Commit

Permalink
fix(problems): 修复查找定时器挂载元素错误
Browse files Browse the repository at this point in the history
  • Loading branch information
XYShaoKang committed Dec 30, 2022
1 parent 30535e2 commit e55cd21
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/content/pages/problems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ window.addEventListener('urlchange', async function () {
* 而第三第四种清理可以不用处理
*/

if (!(isProblemPage() || location.pathname === '/problemset/all/')) {
if (!isProblemPage()) {
// 从答题页跳转到非答题页时,卸载计时组件
unmountRoot()
unmountRandomRoot()
Expand Down
10 changes: 4 additions & 6 deletions src/content/pages/problems/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IS_MAC, findElement, isBetaUI } from '@/utils'
import { IS_MAC, findElement, isBetaUI, findElementByXPath } from '@/utils'

import { logger } from '../../../utils'

Expand Down Expand Up @@ -146,11 +146,9 @@ const checkIfGlobalSubmitIsDisabled = (): boolean =>
async function getRoot(): Promise<HTMLElement> {
const useBetaUI = await isBetaUI()
if (useBetaUI) {
let parent = await findElement('.ssg__qd-splitter-secondary-h>div>div')
parent = [...parent.children].slice(-1)[0].children[0]
.children[0] as HTMLElement
parent = [...parent.children].slice(-1)[0] as HTMLElement
return parent
const xpath = "//button[text()='提交']"
const submit = await findElementByXPath(xpath)
return submit.parentElement!
} else {
const parent = await findElement('.container__Kjnx>.action__KaAP')
return parent
Expand Down
37 changes: 33 additions & 4 deletions src/content/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ function findBase<T>(
*
* 返回一个 Promise,当找到任何与选择器匹配的元素时,则 resolve 一个包含所有与选择器匹配元素的数组;如果在超时时间内未找到与选择器匹配的元素时,则 Promise 会被拒绝.
* @param selectors 需要匹配的选择器
* @param timeout 超时设置,默认为 10000
* @param fn 判断是否找到元素的函数
* @param timeout 超时设置,默认为 10000
* @returns 返回找到的所有元素
*/
export async function findAllElement(
selectors: string,
timeout = 10000,
fn = (e: Element[]) => e.length > 0
fn = (e: Element[]) => e.length > 0,
timeout = 10000
): Promise<Element[]> {
const elements = await findBase<Element[]>(
() => Array.from(document.querySelectorAll(selectors)),
Expand All @@ -60,16 +60,45 @@ export async function findAllElement(
/** 查找匹配选择器的第一个元素
* 返回一个 Promise,当找到第一个与选择器匹配的元素时,则 resolve 找到的这个元素;如果在超时时间内未找到与选择器匹配的元素时,则 Promise 会被拒绝.
* @param selectors 需要匹配的选择器
* @param fn 判断是否找到元素的函数
* @param timeout 超时设置,默认为 10000
* @returns 返回找到的元素
*/
export async function findElement(
selectors: string,
fn = (el: HTMLElement) => !!el,
timeout = 10000
): Promise<HTMLElement> {
const element = await findBase<HTMLElement>(
() => document.querySelector(selectors),
el => !!el,
fn,
timeout
)
return element
}

/** 通过 XPath 查找元素
* 返回一个 Promise,当找与 XPath 匹配的元素时,返回找到的这个元素;如果在超时时间内未找到与选择器匹配的元素时,则 Promise 会被拒绝.
* @param xpath 需要匹配的 XPath
* @param fn 判断是否找到元素的函数
* @param timeout 超时设置,默认为 10000
* @returns 返回找到的元素
*/
export async function findElementByXPath(
xpath: string,
fn = (el: HTMLElement) => !!el,
timeout = 10000
): Promise<HTMLElement> {
const element = await findBase<HTMLElement>(
() =>
document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue,
fn,
timeout
)
return element
Expand Down

0 comments on commit e55cd21

Please sign in to comment.