NixOS Search Copy shell-command to clipboard button

Add a button when hovering over an element with the class shell-command. When the button is clicked it should copy the value of the element with the class shell-command to the clipboard

2023-03-30 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name         NixOS Search Copy shell-command to clipboard button
// @namespace    https://greasyfork.org/en/users/1050283
// @version      0.1
// @description  Add a button when hovering over an element with the class shell-command. When the button is clicked it should copy the value of the element with the class shell-command to the clipboard
// @author       sbm202
// @match        *://search.nixos.org/*
// @icon         https://nixos.org/favicon.png
// @license      MIT
// @grant        none
// ==/UserScript==

const script = document.createElement('script');
script.innerHTML =`
// Get all elements with class "shell-command"
const shellCommands = document.querySelectorAll('.shell-command');

// Loop through each element and add a "mouseenter" event listener
shellCommands.forEach((shellCommand) => {
  shellCommand.addEventListener('mouseenter', () => {
    // Create a button with a paperclip icon
    const button = document.createElement('button');
    button.innerText = 'Copy';
    shellCommand.appendChild(button);

    // Add a "click" event listener to the button
    button.addEventListener('click', () => {
      // Copy the value of the element to the clipboard
      const commandText = shellCommand.innerText;
      navigator.clipboard.writeText(commandText);
    });
  });

  // Add a "mouseleave" event listener to remove the button
  shellCommand.addEventListener('mouseleave', () => {
    const button = shellCommand.querySelector('button');
    if (button) {
      button.remove();
    }
  });
});
`;

(function() {
  var checkReady = setInterval(function() {
    if (true) {
      document.head.appendChild(script)
      clearInterval(checkReady);
    }
  }, 1000);
})();