New Userscript

Extract the Topics of a LeetCode question as a string

目前為 2024-02-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2023-12-25
// @description  Extract the Topics of a LeetCode question as a string
// @author       NeraSnow
// @match        https://leetcode.com/problems/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
// @grant        none
// @license      MIT
// ==/UserScript==

// Function to get the text content of <a> elements inside the specified <div>
function getTextContent() {
  // Select the div element
  const divElement = document.querySelector('.mt-2.flex.gap-1.pl-7');

  // Select all the <a> elements inside the div
  const aElements = divElement.querySelectorAll('a');

  // Create an array to store the text content of each <a> element
  const textArray = [];

  // Iterate over each <a> element and store its text content in the array
  aElements.forEach((a) => {
    textArray.push(`"${a.textContent}"`);
  });

  // Log the array to the console (you can do other things with the array here)
  copyToClipboard(`"topic": [${textArray}]`,)
}

function copyToClipboard(text) {
  navigator.clipboard.writeText(text)
    .then(() => {
      console.log('Text successfully copied to clipboard');
    })
    .catch((err) => {
      console.error('Unable to copy text to clipboard', err);
    });
}

// Create a button element
const buttonElement = document.createElement('button');
buttonElement.textContent = 'Get Topics';

// Add an onClick event listener to the button, linking it to the getTextContent function
buttonElement.addEventListener('click', getTextContent);


// Add styles to make the button a floating element
buttonElement.style.position = 'fixed';
buttonElement.style.top = '13px'; // Adjust the top position as needed
buttonElement.style.right = '450px'; // Adjust the right position as needed
buttonElement.style.zIndex = '9999'; // Ensure it's on top of other elements
buttonElement.style.backgroundColor = 'rgba(255, 255, 255, 0.5)'; // 50% transparent white background

// Add an onClick event listener to the button, linking it to the getTextContent function
buttonElement.addEventListener('click', getTextContent);


function doStuff() {
    // Select the div with the specified class
    // const targetDivElement = document.querySelector('.relative.ml-4.flex.items-center.gap-2');
    // console.log(targetDivElement);
    // Prepend the button to the selected div
    document.body.prepend(buttonElement);
}

doStuff();