Skip to content

Commit 20dffdd

Browse files
committed
fix: 偶尔自动添加备注功能失效
可能由于某种原因导致提交失败,触发重试机制,这时需要继续获取后续的重新提交的结果中的 submissionId
1 parent 1304e03 commit 20dffdd

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

src/content/pages/problems/Clock.tsx

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const Button = styled.button<{ primary?: boolean }>`
3434
`
3535

3636
const Clock: FC = () => {
37+
const pathnames = location.pathname.split('/').filter(Boolean)
38+
const slug = pathnames[1]
39+
3740
const [leetCodeApi] = useState(new LeetCodeApi(location.origin))
3841
const [hidden, setHidden] = useState(false)
3942

@@ -45,18 +48,43 @@ const Clock: FC = () => {
4548

4649
async function getSubmissionId(): Promise<string> {
4750
return new Promise(function (resolve, reject) {
48-
const originalSend = XMLHttpRequest.prototype.send
49-
XMLHttpRequest.prototype.send = function (...args) {
50-
XMLHttpRequest.prototype.send = originalSend
51-
originalSend.apply(this, args)
52-
this.addEventListener('load', function () {
53-
try {
54-
const data = JSON.parse(this.responseText)
55-
resolve(data.submission_id + '')
56-
} catch (error) {
57-
reject(error)
58-
}
59-
})
51+
const originalOpen = XMLHttpRequest.prototype.open
52+
XMLHttpRequest.prototype.open = function newOpen(
53+
this: XMLHttpRequest,
54+
method: string,
55+
url: string,
56+
async?: boolean,
57+
user?: string,
58+
password?: string
59+
) {
60+
if (
61+
method.toLocaleLowerCase() === 'post' &&
62+
url === `/problems/${slug}/submit/`
63+
) {
64+
this.addEventListener('readystatechange', function () {
65+
const DONE = XMLHttpRequest.DONE ?? 4
66+
if (this.readyState === DONE) {
67+
const status = this.status
68+
if (status === 0 || (status >= 200 && status < 400)) {
69+
const data = JSON.parse(this.responseText)
70+
if (XMLHttpRequest.prototype.open === newOpen) {
71+
XMLHttpRequest.prototype.open = originalOpen
72+
}
73+
74+
resolve(data.submission_id + '')
75+
} else {
76+
if (status !== 429) {
77+
if (XMLHttpRequest.prototype.open === newOpen) {
78+
XMLHttpRequest.prototype.open = originalOpen
79+
}
80+
81+
reject(`提交错误,状态 ${status}`)
82+
}
83+
}
84+
}
85+
})
86+
}
87+
originalOpen.apply(this, [method, url, async!, user, password])
6088
}
6189
})
6290
}

0 commit comments

Comments
 (0)