Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

Hi

  1. User

    Hi

  2. Assistant

    Hello! How can I assist you today?

  3. User

    TamperMonkey script to copy text button

  4. Assistant

    Sure, you can create a Tampermonkey script that adds a button to a webpage, and when this button is clicked, it copies specific text to the clipboard. Below is a basic example of how you might achieve this.

    Steps to Create the Script

    1. Install Tampermonkey: If you haven't already, you need to install the Tampermonkey extension for your browser.

    2. Create a New Script: Open the Tampermonkey dashboard and create a new script.

    3. Paste the Script: Copy and paste the following script into the script editor:

    // ==UserScript==
    // @name         Copy Text Button
    // @namespace    http://tampermonkey.net/
    // @version      1.0
    // @description  Adds a button to copy text to clipboard
    // @author       You
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Provide the text you want to copy here
        const textToCopy = "This is the text to copy!";
    
        // Create the button
        const button = document.createElement('button');
        button.innerHTML = 'Copy Text';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.zIndex = 1000; // Ensure the button is on top of other elements
    
        // Add click event to the button
        button.addEventListener('click', () => {
            // Create a temporary textarea element to hold the text
            const tempTextArea = document.createElement('textarea');
            tempTextArea.value = textToCopy;
            document.body.appendChild(tempTextArea);
    
            // Select the text and copy it to clipboard
            tempTextArea.select();
            document.execCommand('copy');
    
            // Remove the temporary textarea
            document.body.removeChild(tempTextArea);
            
            // Provide feedback to the user (Optional)
            alert('Text copied to clipboard!');
        });
    
        // Append the button to the body
        document.body.appendChild(button);
    })();
    

    Explanation

    • Script Metadata:

      • @name: The name of your script.
      • @namespace: Used to distinguish your script from others of the same name.
      • @version: Version of the script.
      • @description: A brief description of what the script does.
      • @author: Your name or username.
      • @match: Specifies the URLs where the script should run (currently set to all URLs).
    • Main Function:

      • Creates a button with the label "Copy Text".
      • Attaches the button to the page, positioning it fixed at the top-right corner.
      • Adds an event listener to the button to copy predefined text to the clipboard when clicked.
      • Uses a temporary textarea element to facilitate copying the text to the clipboard.
      • Optionally, provides a popup alert to inform the user that the text has been copied.
    1. Save and Activate: Save the script and make sure it's enabled in the Tampermonkey dashboard.

    2. Test: Navigate to any webpage within the @match domain(s) specified to see the button and test its functionality.

    Feel free to customize the textToCopy variable and the button's styles and behaviors to better suit your needs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.