Skip to content

Commit 0d0eaa8

Browse files
committed
fix: 修复有时候不出现按钮的情况
之前去获取根元素的方式有误
1 parent 9fb4ff2 commit 0d0eaa8

2 files changed

Lines changed: 46 additions & 34 deletions

File tree

src/index.tsx

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,34 @@ import { render } from 'react-dom'
33
import minimatch from 'minimatch'
44

55
import App from './App'
6+
import { getElement } from './utils'
67

7-
if (minimatch(location.href, 'https://leetcode-cn.com/submissions/detail/**')) {
8-
window.onload = () => {
9-
const parent =
10-
document.querySelectorAll('#lc-header+div')?.[0]?.children?.[0]
11-
if (parent && parent instanceof HTMLElement) {
12-
parent.style.display = 'flex'
13-
parent.style.justifyContent = 'space-between'
14-
const root = document.createElement('div')
15-
parent.append(root)
8+
function loadDownload(parent: Element) {
9+
if (parent && parent instanceof HTMLElement) {
10+
parent.style.display = 'flex'
11+
parent.style.justifyContent = 'space-between'
12+
const root = document.createElement('div')
13+
parent.append(root)
1614

17-
render(
18-
<StrictMode>
19-
<App />
20-
</StrictMode>,
21-
root
22-
)
23-
}
15+
render(
16+
<StrictMode>
17+
<App />
18+
</StrictMode>,
19+
root
20+
)
2421
}
2522
}
2623

27-
if (minimatch(location.href, 'https://leetcode.com/submissions/detail/**')) {
28-
window.onload = () => {
29-
const parent =
30-
document.getElementById('submission-app')?.children?.[0]?.children?.[0]
31-
32-
if (parent && parent instanceof HTMLElement) {
33-
parent.style.display = 'flex'
34-
parent.style.justifyContent = 'space-between'
35-
const root = document.createElement('div')
36-
parent.append(root)
24+
if (minimatch(location.href, 'https://leetcode-cn.com/submissions/detail/**')) {
25+
window.onload = async () => {
26+
const main = await getElement('.css-smuvek-Main')
27+
loadDownload(main[0]?.children?.[0])
28+
}
29+
}
3730

38-
render(
39-
<StrictMode>
40-
<App />
41-
</StrictMode>,
42-
root
43-
)
44-
}
31+
if (minimatch(location.href, 'https://leetcode.com/submissions/detail/**')) {
32+
window.onload = async () => {
33+
const parent = await getElement('#submission-app>.row>div:first-child')
34+
loadDownload(parent[0])
4535
}
4636
}

src/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,26 @@ function download(str: string, filename = 'contest.md'): void {
1111
document.body.removeChild(a)
1212
}
1313

14-
export { download }
14+
function getElement(
15+
query: string,
16+
fn: (e: NodeListOf<Element>) => boolean = e => e.length > 0,
17+
timeout = 10000
18+
): Promise<NodeListOf<Element>> {
19+
const delay = 100
20+
return new Promise(function (resolve, reject) {
21+
const timer = setInterval(() => {
22+
const element = document.querySelectorAll(query)
23+
if (fn(element)) {
24+
clearInterval(timer)
25+
resolve(element)
26+
}
27+
if (timeout <= 0) {
28+
clearInterval(timer)
29+
reject('超时')
30+
}
31+
timeout -= delay
32+
}, delay)
33+
})
34+
}
35+
36+
export { download, getElement }

0 commit comments

Comments
 (0)