|
1 | 1 | // ==UserScript== |
2 | 2 | // @name lc-to-markdown-txt-html |
3 | 3 | // @author wuxin0011 |
4 | | -// @version 0.0.7 |
| 4 | +// @version 0.0.8 |
5 | 5 | // @namespace https://github.com/wuxin0011/tampermonkey-script/tree/main/lc-to-markdown-txt-html |
6 | | -// @description 力扣题目描述、题解,讨论发布内容复制 复制为 markdown、txt、html 等格式 |
| 6 | +// @description 力扣题目描述,讨论发布内容复制 复制为 markdown、txt、html 等格式 |
7 | 7 | // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAAXNSR0IArs4c6QAABsxJREFUeF7tnVvoZVMcxz9TkkQawoQUEYXECKHcybg+uBUevXibvBC55IXyIG/ePbglwwxyl0suo5FRbjVvNJFMkSTF/tU+Op32Oeu69z5rr+96UWat3/r9vr/P+e21z9n799+ARtUKbKg6egWPAKgcAgEgACpXoPLwVQEEQOUKVB6+KoAAqFyBysNXBRAAlStQefiqAAKgcgUqD18VQABUrkDl4asCCIDKFag8fFUAAVC5ApWHrwogACpXoPLwc1WAQ4GLgc3AccAm0ONmmdn6F9gL7AF2Au8C+1L3SAXgMOAR4E5gv1RntD5IgX+Ap4AHgV+DVs5NTgHgNOAt4IjYzbUuiwI/A5cBu2OsxQJwNvA2cFDMplqTXYE/gEuBz0ItxwBgn/hdwFGhm2l+rwr8CJweejmIAWA7cHWvoch4rALbgBtCFocCcCHwXsgGmju4Ape0dwheG4cCsKO5zmzxsqxJYykQVAVCANgf+B2w/2qsrwJ/AhuBv31cDAHAvuSxLyA01l8BOwx+5eNmCAB28LMDoMb6K3Al8IaPmyEA3Aw862NUc0ZXwHL1vI8XAsBHpfLmCIDycpbVYwGQVc7yjAmA8nKW1WMBkFXO8owJgPJyltVjAZBVzvKMCYDycpbVYwGQVc7yjAmA8nKW1WMBkFXO8owJgPJyltVjAZBVzvKMCYDycpbVYwGQVc7yjBUFwJPAa83LDR8C9nx7n+Os5qHWi5pH2+5uX1/Lt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BbgB2xHmndoApYWxvrbeQcrpcm5g1sBnY6LWrCOihwBvCljyMhANgfWfgNsD77GuurgLW12djR2KrT4xAAzMArTRW4Zn1jl2dN+7qX27Z2XmKEAnAdsM3LsiaNpYD9GZ7tvpuHAmDzdzfNok7x3UDzBlVgV0cX05UOhAJgxk5tW5umdLEaVJVKNtvXdhj7PiTeGADM/nltmbHDhsb4Ctjh3G7Trb1d0IgFwDY5GrB+fhcE7ajJuRWwzqLWP/GnGMMpAMz2sxbu1vXa/gCTxnAKfAPcD7yYsmUOAGb7n9+WoePbv8qZ03ZKjFNZa13N9wJ7gFfbFrXJsSlJyRKWbUAAlJ2/ZO8FQLKEZRsQAGXnL9l7AZAsYdkGBEDZ+Uv2XgAkS1i2AQFQdv6SvRcAyRKWbUAAlJ2/ZO8FQLKEZRsQAGXnL9l7AZAsYdkGBEDZ+Uv2XgAkS1i2AQFQdv6SvRcAyRKWbUAAlJ2/ZO8FQLKEZRsQAGXnL9l7AZAsYdkG/gORYKWQd56JTQAAAABJRU5ErkJggg== |
8 | 8 | // @match https://leetcode.cn/circle/discuss/* |
9 | 9 | // @match https://leetcode.cn/discuss/post/* |
10 | | -// @match https://leetcode.com/discuss/post/* |
11 | 10 | // @match https://leetcode.cn/problems/* |
| 11 | +// @match https://leetcode.cn/contest/weekly-contest-*/problems/* |
| 12 | +// @match https://leetcode.cn/contest/biweekly-contest-*/problems/* |
12 | 13 | // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js |
13 | 14 | // @require https://unpkg.com/[email protected]/dist/turndown.js |
14 | 15 | // @grant GM_registerMenuCommand |
15 | 16 | // @grant GM_unregisterMenuCommand |
| 17 | +// @grant GM_setClipboard |
| 18 | +// @grant GM_addStyle |
16 | 19 | // @grant GM_setValue |
17 | 20 | // @grant GM_getValue |
18 | | -// @grant GM_cookie |
| 21 | +// @grant GM_getResourceText |
| 22 | +// @require https://cdn.jsdelivr.net/npm/[email protected]/lib/noty.min.js |
| 23 | +// @resource notycss https://cdn.jsdelivr.net/npm/[email protected]/lib/noty.min.css |
| 24 | +// @downloadURL https://update.greasyfork.org/scripts/491969/lc-to-markdown-txt-html.user.js |
| 25 | +// @updateURL https://update.greasyfork.org/scripts/491969/lc-to-markdown-txt-html.meta.js |
19 | 26 | // @license MIT |
20 | 27 | // ==/UserScript== |
21 | 28 |
|
22 | 29 | (function () { |
23 | 30 | 'use strict'; |
24 | | - // 如果需要周赛中启用将下面两个复制到上面 |
25 | | - // @match https://leetcode.cn/contest/weekly-contest-*/problems/* |
26 | | - // @match https://leetcode.cn/contest/biweekly-contest-*/problems/* |
| 31 | + GM_addStyle(GM_getResourceText('notycss')); |
27 | 32 | const url = window.location.href |
28 | 33 | const HTML_CONVERT = '__HTML_CONVERT__' |
29 | 34 | const TXT_CONVERT = '__TXT_CONVERT__' |
|
40 | 45 | } |
41 | 46 | console.log('lx-md-html-txt tip:', ...args) |
42 | 47 | } |
43 | | - const isDiscuss = () => url.indexOf('https://leetcode.cn/circle/discuss') != -1 || url.indexOf('https://leetcode.cn/discuss') != -1 |
| 48 | + const isDiscuss = () => url.indexOf('https://leetcode.cn/circle/discuss') != -1 |
44 | 49 | const isProblem = () => url.indexOf('https://leetcode.cn/problems') != -1 |
45 | 50 | const isContest = () => url.indexOf('https://leetcode.cn/contest/weekly-contest') != -1 || url.indexOf('https://leetcode.cn/contest/biweekly-contest') != -1 |
46 | 51 |
|
|
111 | 116 | } |
112 | 117 |
|
113 | 118 |
|
114 | | - function runQuestionActionsContainer() { |
| 119 | + function runQuestionActionsContainer() { |
115 | 120 | const className = '.break-words'; |
116 | 121 | const questionActionsContainer = document.querySelector('.sticky.bottom-0') |
117 | 122 | // console.log('questionActionsContainer',questionActionsContainer) |
|
139 | 144 |
|
140 | 145 |
|
141 | 146 |
|
142 | | - |
143 | | - |
144 | 147 | const toMarkdown = (htmlContent) => { |
145 | 148 | try { |
146 | 149 | var turndownService = new TurndownService() |
|
166 | 169 | } |
167 | 170 |
|
168 | 171 | function runProblems() { |
169 | | - // log('~~~ run problem ~~~~', url) |
| 172 | + // console.log('~~~ run problem ~~~~', url) |
170 | 173 | addSolutionButton() |
171 | 174 | addClickWatch() |
172 | 175 | let buttonClassName = 'relative inline-flex items-center justify-center text-caption px-2 py-1 gap-1 rounded-full bg-fill-secondary text-difficulty-easy dark:text-difficulty-easy' |
|
243 | 246 |
|
244 | 247 | loadSolution(document) |
245 | 248 | } |
| 249 | + |
| 250 | + // console.log('container',container) |
246 | 251 | if (!container) { |
247 | 252 | if (times >= MAX_CNT - 2) { |
248 | 253 | log('找不到 容器,将手动创建容器!', url) |
|
252 | 257 | if (loadOk) { |
253 | 258 | return |
254 | 259 | } |
| 260 | + console.log('install contianer ....') |
255 | 261 | markdownButton.className = buttonClassName |
256 | 262 | txtButton.className = buttonClassName |
257 | 263 | htmlButton.className = buttonClassName |
|
313 | 319 | buttonContainer.style.marginTop = '10px' |
314 | 320 | buttonContainer.style.marginBottom = '10px' |
315 | 321 | buttonContainer.className = solutionClass |
| 322 | + if(solutionContainer.querySelector('span')) { |
| 323 | + for(let span of Array.from(solutionContainer.querySelectorAll('span') ?? {length : 0})) { |
| 324 | + let cur = span.textContent |
| 325 | + cur = cur.toLowerCase() |
| 326 | + if(cur.indexOf('create the variable') != -1) { |
| 327 | + span.textContent = '' |
| 328 | + span.remove() |
| 329 | + // console.log('del') |
| 330 | + } |
| 331 | + } |
| 332 | + } |
316 | 333 | let t = solutionContainer.innerHTML |
317 | 334 | let buttons = loadButton() |
318 | 335 | runCopy(buttonContainer, buttons[0], t, SUPPORT_TYPE['md']) |
|
396 | 413 |
|
397 | 414 |
|
398 | 415 |
|
| 416 | + let CARRAY = ['','',''] |
| 417 | + |
| 418 | + |
| 419 | + |
| 420 | + |
| 421 | + |
399 | 422 |
|
400 | 423 | function runCopy(container, ele, htmlContent, type = SUPPORT_TYPE['md'], title = '') { |
401 | 424 |
|
|
418 | 441 |
|
419 | 442 | if (type == SUPPORT_TYPE['md']) { |
420 | 443 | const markdown = toMarkdown(htmlContent) |
| 444 | + CARRAY[1] = markdown |
421 | 445 | copy(markdown, ele) |
422 | 446 | } else if (type == SUPPORT_TYPE['txt']) { |
423 | 447 | const d = document.createElement('div') |
424 | 448 | d.innerHTML = htmlContent |
425 | 449 | const txt = handlerText(d.textContent, title) |
| 450 | + CARRAY[0] = txt |
426 | 451 | copy(txt, ele) |
427 | 452 | } else if (type == SUPPORT_TYPE['html']) { |
428 | 453 | // html |
| 454 | + CARRAY[2] = htmlContent |
429 | 455 | copy(htmlContent, ele) |
430 | 456 | } else { |
431 | 457 | console.warn('no support format ' + type) |
|
503 | 529 |
|
504 | 530 |
|
505 | 531 |
|
| 532 | + for(let i = 0;i<3;i++) { |
| 533 | + let flag = i == 0 ? 'txt' : i == 1 ? 'markdown' : 'html' |
| 534 | + GM_registerMenuCommand(`复制为${flag}`, () => { |
| 535 | + GM_setClipboard(CARRAY[i]) |
| 536 | + new Noty({ |
| 537 | + type: `${CARRAY[i] ? 'success' : 'error'}`, |
| 538 | + layout: "topRight", |
| 539 | + text: `${flag}复制${CARRAY[i] ? '成功🥰' : '失败😥'}`, |
| 540 | + timeout: 2000 |
| 541 | + }).show(); |
| 542 | + }, { title: `复制为${flag}`}) |
| 543 | + } |
| 544 | + |
506 | 545 | const html_to_markdown_web = GM_registerMenuCommand('html转换markdown网站', () => { |
507 | 546 | window.open(markdownURL, '_blank') |
508 | 547 | }, { title: '如果格式转换有问题,请复制为 html 然后用这个网站转换' }) |
|
522 | 561 | } |
523 | 562 |
|
524 | 563 | let times = 0 |
525 | | - const MAX_CNT = 15 |
526 | | - const TIME_OUT = 1500 |
| 564 | + const MAX_CNT = 6 |
| 565 | + const TIME_OUT = 1000 |
527 | 566 | initConmand() |
528 | 567 |
|
529 | 568 |
|
|
593 | 632 | } |
594 | 633 |
|
595 | 634 |
|
596 | | - window.onload = () => { |
| 635 | + function core() { |
597 | 636 | times = 0 |
598 | | - start() |
599 | 637 | try { |
600 | | - // loadOK(); |
| 638 | + start() |
601 | 639 | } catch (e) { |
602 | | - |
| 640 | + console.log('start error',e) |
| 641 | + } |
| 642 | + try { |
| 643 | + addClickWatch() |
| 644 | + } catch (e) { |
| 645 | + console.log('addClickWatch error',e) |
603 | 646 | } |
604 | | - addClickWatch() |
605 | 647 | } |
606 | 648 |
|
| 649 | + core() |
| 650 | + |
607 | 651 | // 监听地址改变 |
608 | 652 | // 重新修改描述 |
609 | 653 | let urlChangeLoadOk = false |
|
0 commit comments