Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Copying to Clipboard in a content Script

Can someone explain how I can write to clipboard please? I've seen a lot of ways but I cant seem to understand how. Currently using manifest v3 and no background.js script.

popup.js (action: download) -> contentscript.js (see below)

function listener(info) {

    chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {

            if (!request.action) return sendResponse({
                err: 'Error: No Request Action'
            });

            switch (request.action) {
                case "copy":

                    var textCopy = info.links.join(', ').replace(/"/g, '') // Text to Copy (String)

                    navigator.clipboard.writeText(textCopy).then(() => {
                        sendResponse('success')
                    }, () => {
                        sendResponse('failed')
                    });

                    break;

                // Other Requests
               
            }

        }
    );
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

At least in manifest v2, this method works.


First you need to add the permission clipboardWrite to your manifest:

...
"permissions": ["clipboardWrite", ... ],
...

Then do something like what was answered here: https://stackoverflow.com/a/18455088

The idea is to create a temporary text area to put the text in which then allows you to select all the text inside with .select() and then copy it with document.execCommand('copy').

about 4 years ago · Juan Pablo Isaza Report

0

thanks to @cactus12 for the help here's the code that worked for me!

var textCopy = '' // Text to Copy
var copyFrom = document.createElement("textarea");

copyFrom.textContent = `${textCopy}`;
document.body.appendChild(copyFrom);

copyFrom.select();
document.execCommand(`copy`);

copyFrom.blur();
document.body.removeChild(copyFrom);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!