MediaWiki Title Copy

Adds a copy icon next to the title to copy it to the clipboard.

  1. // ==UserScript==
  2. // @name MediaWiki Title Copy
  3. // @namespace Violentmonkey Scripts
  4. // @version 1.0
  5. // @description Adds a copy icon next to the title to copy it to the clipboard.
  6. // @author Shou Ya
  7. // @match https://*.wikipedia.org/wiki/*
  8. // @grant GM_setClipboard
  9. // @grant GM_addStyle
  10. // @license WTFPL
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to handle clipboard copy
  17. function copyToClipboard(text) {
  18. if (typeof GM_setClipboard !== 'undefined') {
  19. GM_setClipboard(text);
  20. } else if (navigator.clipboard && navigator.clipboard.writeText) {
  21. navigator.clipboard.writeText(text);
  22. } else {
  23. console.error("Clipboard API not available");
  24. alert("Clipboard copy failed. Your browser may not support this feature.");
  25. return false;
  26. }
  27.  
  28. showNotification();
  29. return true;
  30. }
  31.  
  32. // Function to show notification
  33. function showNotification() {
  34. let notification = document.createElement('div');
  35. notification.textContent = 'Copied!';
  36. notification.classList.add('copy-notification');
  37. document.body.appendChild(notification);
  38.  
  39. setTimeout(() => {
  40. notification.remove();
  41. }, 1500);
  42. }
  43.  
  44. // Get the title element
  45. const titleElement = document.querySelector('#firstHeading');
  46.  
  47. if (!titleElement) {
  48. console.warn("Wikipedia title element not found.");
  49. return;
  50. }
  51.  
  52. // SVG icon code
  53. const svgIcon = `
  54. <svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  55. <path d="M16 1H4C2.9 1 2 1.9 2 3V17H4V3H16V1ZM19 5H8C6.9 5 6 5.9 6 7V21C6 22.1 6.9 23 8 23H19C20.1 23 21 22.1 21 21V7C21 5.9 20.1 5 19 5ZM19 21H8V7H19V21Z" fill="currentColor"/>
  56. </svg>
  57. `;
  58.  
  59. // Create the copy icon
  60. const copyIcon = document.createElement('span');
  61. copyIcon.innerHTML = svgIcon;
  62. copyIcon.classList.add('copy-icon');
  63. copyIcon.title = "Copy title to clipboard";
  64. titleElement.parentNode.insertBefore(copyIcon, titleElement.nextSibling);
  65.  
  66. // Add click event listener
  67. copyIcon.addEventListener('click', function() {
  68. const titleText = titleElement.textContent.trim();
  69. copyToClipboard(titleText);
  70. });
  71.  
  72. // Add CSS for the icon and notification
  73. GM_addStyle(`
  74. .copy-icon {
  75. cursor: pointer;
  76. margin-left: 5px;
  77. display: inline-flex; /* For flex alignment */
  78. align-items: center; /* Vertically center the SVG */
  79. }
  80.  
  81. .copy-notification {
  82. position: fixed;
  83. bottom: 20px;
  84. left: 50%;
  85. transform: translateX(-50%);
  86. background-color: rgba(0, 0, 0, 0.8);
  87. color: white;
  88. padding: 10px 20px;
  89. border-radius: 5px;
  90. z-index: 1000;
  91. }
  92. `);
  93. })();

QingJ © 2025

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