Greasy Fork is available in English.

Copy LeetCode Title

Adds a button to copy the LeetCode problem title

  1. // ==UserScript==
  2. // @name Copy LeetCode Title
  3. // @namespace http://tampermonkey.net/
  4. // @version 2025-05-17
  5. // @description Adds a button to copy the LeetCode problem title
  6. // @author You
  7. // @match https://leetcode.cn/problems/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function copyToClipboard(text) {
  17. navigator.clipboard.writeText(text).then(function() {
  18. console.log('Async: Copying to clipboard was successful!');
  19. }, function(err) {
  20. console.error('Async: Could not copy text: ', err);
  21. });
  22. }
  23.  
  24. function addButton() {
  25. const titleElement = document.querySelector('.text-title-large');
  26. if (!titleElement) {
  27. console.error("Could not find the title element.");
  28. return;
  29. }
  30.  
  31. const problemTitle = titleElement.textContent.trim();
  32.  
  33. const copyButton = document.createElement('button');
  34. copyButton.textContent = '复制标题';
  35. copyButton.style.marginLeft = '10px'; // Add some spacing
  36. copyButton.style.padding = '5px 10px';
  37. copyButton.style.backgroundColor = '#f0f0f0';
  38. copyButton.style.border = '1px solid #ccc';
  39. copyButton.style.borderRadius = '5px';
  40. copyButton.style.cursor = 'pointer';
  41.  
  42. copyButton.addEventListener('click', function() {
  43. copyToClipboard(problemTitle);
  44. // alert('标题已复制到剪贴板!'); // Provide feedback
  45. });
  46.  
  47. const titleContainer = titleElement.parentNode;
  48. if (titleContainer) {
  49. titleContainer.appendChild(copyButton);
  50. } else {
  51. console.error("Could not find the title container.");
  52. }
  53. }
  54.  
  55. // Add the button after the page loads
  56. window.addEventListener('load', addButton);
  57. })();