Skip to content

Commit

Permalink
fix: 修复有时候不出现按钮的情况
Browse files Browse the repository at this point in the history
之前去获取根元素的方式有误
  • Loading branch information
XYShaoKang committed Jan 1, 2022
1 parent 9fb4ff2 commit 0d0eaa8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
56 changes: 23 additions & 33 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,34 @@ import { render } from 'react-dom'
import minimatch from 'minimatch'

import App from './App'
import { getElement } from './utils'

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

render(
<StrictMode>
<App />
</StrictMode>,
root
)
}
render(
<StrictMode>
<App />
</StrictMode>,
root
)
}
}

if (minimatch(location.href, 'https://leetcode.com/submissions/detail/**')) {
window.onload = () => {
const parent =
document.getElementById('submission-app')?.children?.[0]?.children?.[0]

if (parent && parent instanceof HTMLElement) {
parent.style.display = 'flex'
parent.style.justifyContent = 'space-between'
const root = document.createElement('div')
parent.append(root)
if (minimatch(location.href, 'https://leetcode-cn.com/submissions/detail/**')) {
window.onload = async () => {
const main = await getElement('.css-smuvek-Main')
loadDownload(main[0]?.children?.[0])
}
}

render(
<StrictMode>
<App />
</StrictMode>,
root
)
}
if (minimatch(location.href, 'https://leetcode.com/submissions/detail/**')) {
window.onload = async () => {
const parent = await getElement('#submission-app>.row>div:first-child')
loadDownload(parent[0])
}
}
24 changes: 23 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,26 @@ function download(str: string, filename = 'contest.md'): void {
document.body.removeChild(a)
}

export { download }
function getElement(
query: string,
fn: (e: NodeListOf<Element>) => boolean = e => e.length > 0,
timeout = 10000
): Promise<NodeListOf<Element>> {
const delay = 100
return new Promise(function (resolve, reject) {
const timer = setInterval(() => {
const element = document.querySelectorAll(query)
if (fn(element)) {
clearInterval(timer)
resolve(element)
}
if (timeout <= 0) {
clearInterval(timer)
reject('超时')
}
timeout -= delay
}, delay)
})
}

export { download, getElement }

0 comments on commit 0d0eaa8

Please sign in to comment.