@@ -183,20 +183,23 @@ function insertButton() {
183
183
buttons [ i ] . parentElement . prepend ( copyButton ) ;
184
184
185
185
// const writeSolutionButton = document.createElement("div");
186
- const writeSolutionButton = buttons [ i ] . cloneNode ( true ) ;
187
- writeSolutionButton . innerText = "写题解" ;
188
- writeSolutionButton . style [ "margin-left" ] = "10px" ;
186
+ const writeSolutionButton = document . createElement ( "a" ) ;
187
+ writeSolutionButton . innerText = "去写题解" ;
188
+ writeSolutionButton . style [ "margin-right" ] = "20px" ;
189
+ writeSolutionButton . style [ "line-height" ] = "32px" ;
190
+ let ele = document . querySelector ( `[data-cypress="QuestionTitle"]` ) ;
189
191
190
192
writeSolutionButton . onclick = ( ) => {
191
193
// d: "<a href="/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/">1579. 保证图可完全遍历</a>"
192
- const ele = document . querySelector ( `[data-cypress="QuestionTitle"]` ) ;
194
+ if ( ! ele ) {
195
+ ele = document . querySelector ( `[data-cypress="QuestionTitle"]` ) ;
196
+ }
193
197
if ( ! ele ) {
194
198
return message . warn ( {
195
199
content : "获取题目描述失败,请先切换到题目描述标签" ,
196
200
} ) ;
197
201
}
198
- const d = document . querySelector ( `[data-cypress="QuestionTitle"]` )
199
- . innerHTML ;
202
+ const d = ele . innerHTML ;
200
203
const title = d . match ( / ( \d + \. .+ ) (? = < ) / ) [ 1 ] ;
201
204
const link = window . location . origin + d . match ( / h r e f = " ( .* ?) " / ) [ 1 ] ;
202
205
const language = document . querySelector ( "#lang-select" ) . innerText ;
@@ -206,6 +209,13 @@ function insertButton() {
206
209
207
210
const desc = document . querySelector ( "#question-detail-main-tabs" )
208
211
. children [ 1 ] . children [ 0 ] . children [ 1 ] . innerText ;
212
+ const hide = message . loading ( "正在存储题目信息,请稍后~" , 0 ) ;
213
+ writeSolutionButton . setAttribute ( "disabled" , true ) ;
214
+ // Dismiss manually and asynchronously
215
+ setTimeout ( ( ) => {
216
+ hide ( ) ;
217
+ writeSolutionButton . removeAttribute ( "disabled" ) ;
218
+ } , 30000 ) ; // 超时 30s 都没好,那就别转了
209
219
getStorage ( "leetcode-cheatsheet-token" )
210
220
. then ( ( res ) => res . result . value )
211
221
. then ( ( res ) => {
@@ -229,29 +239,37 @@ function insertButton() {
229
239
{
230
240
token : t ,
231
241
}
232
- ) . then ( ( res ) => {
233
- if ( res . id ) {
234
- window . open (
235
- `https://leetcode-pp.github.io/leetcode-cheat/?issue_number=${ res . id } &tab=solution-template`
236
- ) ;
237
- } else {
238
- message . warn ( {
239
- content :
240
- "使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。" ,
241
- } ) ;
242
- setTimeout ( ( ) => {
242
+ )
243
+ . then ( ( res ) => {
244
+ hide ( ) ;
245
+ writeSolutionButton . removeAttribute ( "disabled" ) ;
246
+ if ( res . id ) {
243
247
window . open (
244
- `https://leetcode-pp.github.io/leetcode-cheat/?title =${ title } &link= ${ link } &language= ${ language } &tab=solution-template`
248
+ `https://leetcode-pp.github.io/leetcode-cheat/?issue_number =${ res . id } &tab=solution-template`
245
249
) ;
246
- } , 2000 ) ;
247
- }
248
- } ) ;
250
+ } else {
251
+ message . warn ( {
252
+ content :
253
+ "使用 Github API 失败,已为您切换为普通模式,普通模式仅可自动带入题目名称,题目地址以及题解语言。" ,
254
+ } ) ;
255
+ setTimeout ( ( ) => {
256
+ window . open (
257
+ `https://leetcode-pp.github.io/leetcode-cheat/?title=${ title } &link=${ link } &language=${ language } &tab=solution-template`
258
+ ) ;
259
+ } , 2000 ) ;
260
+ }
261
+ } )
262
+ . catch ( ( ) => {
263
+ hide ( ) ;
264
+ writeSolutionButton . removeAttribute ( "disabled" ) ;
265
+ } ) ;
249
266
} ) ;
250
267
} ;
251
268
252
269
// ReactDOM.render(<SolutionButton />, writeSolutionButton);
253
270
254
271
buttons [ i ] . parentElement . prepend ( writeSolutionButton ) ;
272
+ // ele.appendChild(writeSolutionButton);
255
273
return true ;
256
274
}
257
275
}
0 commit comments