Extract LeetCode Question Topics

Extract the Topics of a LeetCode question as a string

2024-02-22 기준 버전입니다. 최신 버전을 확인하세요.

  1. // ==UserScript==
  2. // @name Extract LeetCode Question Topics
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-02-22
  5. // @description Extract the Topics of a LeetCode question as a string
  6. // @author NeraSnow
  7. // @match https://leetcode.com/problems/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // Function to get the text content of <a> elements inside the specified <div>
  14. function getTextContent() {
  15. // Select the div element
  16. const divElement = document.querySelector('.mt-2.flex.gap-1.pl-7');
  17.  
  18. // Select all the <a> elements inside the div
  19. const aElements = divElement.querySelectorAll('a');
  20.  
  21. // Create an array to store the text content of each <a> element
  22. const textArray = [];
  23.  
  24. // Iterate over each <a> element and store its text content in the array
  25. aElements.forEach((a) => {
  26. textArray.push(`"${a.textContent}"`);
  27. });
  28.  
  29. // Log the array to the console (you can do other things with the array here)
  30. copyToClipboard(`"topic": [${textArray}]`,)
  31. }
  32.  
  33. function copyToClipboard(text) {
  34. navigator.clipboard.writeText(text)
  35. .then(() => {
  36. console.log('Text successfully copied to clipboard');
  37. })
  38. .catch((err) => {
  39. console.error('Unable to copy text to clipboard', err);
  40. });
  41. }
  42.  
  43. // Create a button element
  44. const buttonElement = document.createElement('button');
  45. buttonElement.textContent = 'Get Topics';
  46.  
  47. // Add an onClick event listener to the button, linking it to the getTextContent function
  48. buttonElement.addEventListener('click', getTextContent);
  49.  
  50.  
  51. // Add styles to make the button a floating element
  52. buttonElement.style.position = 'fixed';
  53. buttonElement.style.top = '13px'; // Adjust the top position as needed
  54. buttonElement.style.right = '450px'; // Adjust the right position as needed
  55. buttonElement.style.zIndex = '9999'; // Ensure it's on top of other elements
  56. buttonElement.style.backgroundColor = 'rgba(255, 255, 255, 0.5)'; // 50% transparent white background
  57.  
  58. // Add an onClick event listener to the button, linking it to the getTextContent function
  59. buttonElement.addEventListener('click', getTextContent);
  60.  
  61.  
  62. function doStuff() {
  63. // Select the div with the specified class
  64. // const targetDivElement = document.querySelector('.relative.ml-4.flex.items-center.gap-2');
  65. // console.log(targetDivElement);
  66. // Prepend the button to the selected div
  67. document.body.prepend(buttonElement);
  68. }
  69.  
  70. doStuff();
  71.