LeetCode 問題描述內容擷取器

在 LeetCode 問題頁面上,按下按鈕後擷取問題描述內容。

目前为 2025-02-26 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name LeetCode 問題描述內容擷取器
  3. // @namespace https://your.namespace/
  4. // @version 1.1
  5. // @description 在 LeetCode 問題頁面上,按下按鈕後擷取問題描述內容。
  6. // @author abc0922001
  7. // @match https://leetcode.com/problems/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // 等待指定選取器元素出現
  16. function waitForElement(selector, timeout = 5000) {
  17. return new Promise((resolve, reject) => {
  18. const el = document.querySelector(selector);
  19. if (el) {
  20. return resolve(el);
  21. }
  22. const observer = new MutationObserver((mutations, obs) => {
  23. const el = document.querySelector(selector);
  24. if (el) {
  25. obs.disconnect();
  26. resolve(el);
  27. }
  28. });
  29. observer.observe(document.body, { childList: true, subtree: true });
  30. setTimeout(() => {
  31. observer.disconnect();
  32. reject(new Error('Timeout'));
  33. }, timeout);
  34. });
  35. }
  36.  
  37. // 建立一個按鈕
  38. const btn = document.createElement('button');
  39. btn.textContent = '擷取描述';
  40. btn.style.position = 'fixed';
  41. btn.style.top = '10px';
  42. btn.style.right = '10px';
  43. btn.style.zIndex = 1000;
  44. document.body.appendChild(btn);
  45.  
  46. btn.addEventListener('click', () => {
  47. // 根據提供的 HTML,等待目標元素出現
  48. waitForElement('div.elfjS[data-track-load="description_content"]', 5000)
  49. .then(targetDiv => {
  50. const htmlContent = targetDiv.innerHTML;
  51. // 在頁面上建立一個 textarea 來顯示擷取的內容
  52. const textarea = document.createElement('textarea');
  53. textarea.style.position = 'fixed';
  54. textarea.style.top = '50px';
  55. textarea.style.right = '10px';
  56. textarea.style.width = '300px';
  57. textarea.style.height = '300px';
  58. textarea.value = htmlContent;
  59. document.body.appendChild(textarea);
  60. console.log(htmlContent);
  61. })
  62. .catch(err => {
  63. alert('找不到目標內容');
  64. console.error(err);
  65. });
  66. });
  67. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址