Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write text to Clipboard

I want to write some text variable into clipboard via Chrome Extension, it will be happened when user presses a short-key. I've done all parts except writing to clipboard.

I've searched entire StackOverflow using these keywords: "[google-chrome-extension] Clipboard"

So I want to say, I've seen all related to:

  • Add clipboardRead and clipboardWrite permission (already done)
  • Add text into a <textarea>, call document.execCommand('Copy'); or document.execCommand("Copy", false, null);

Even I tried my extension on StackOverflow's textarea and I inserted my text into wmd-input part of StackOverflow textarea, then selected it, then called copy. Nothing, nothing, nothing...

Everything tried. Please advise... What am I missing?

like image 721
David Brisker Avatar asked Dec 16 '12 06:12

David Brisker


4 Answers

Based on https://stackoverflow.com/a/12693636

function directCopy(str){
    //based on https://stackoverflow.com/a/12693636
        document.oncopy = function(event) {
    event.clipboardData.setData("Text", str);
    event.preventDefault();
        };
    document.execCommand("Copy");
        document.oncopy = undefined;
}
like image 76
user2573802 Avatar answered Nov 13 '22 08:11

user2573802


You can try the following code, it writes text to clipboard

As an example i wrote Sample to clipboard

Output

enter image description here

manifest.json

manifest file is key for all chrome extensions, ensured it is with all permissions

 {
  "name": "Copy to ClipBoard Demo",
  "description" : "This is used for demonstrating Copy to Clip Board Functionality",
  "version": "1",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions":["clipboardWrite"],
    "manifest_version": 2
}

popup.html

A trivial Browser action HTML File, with input box and button

<html>

    <head>
        <script src="popup.js"></script>
    </head>

    <body>
        <input type="text" id="text" placeHolder="Enter Text To Copy"></input>
        <button id="copy">Copy</button>
    </body>

</html>

popup.js

It copies content in <input> to clipboard

function copy() {

    //Get Input Element
    var copyDiv = document.getElementById('text');

    //Give the text element focus
    copyDiv.focus();

    //Select all content
    document.execCommand('SelectAll');

    //Copy Content
    document.execCommand("Copy", false, null);
}

//Add Event Listeners to Button Click
document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("copy").onclick = copy;
});

OR

function copy(){

    //Get Input Element
    document.getElementById("text").select();

    //Copy Content
    document.execCommand("Copy", false, null);
}

//Add Event Listeners to Button Click
document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("copy").onclick = copy;
});
like image 40
Sudarshan Avatar answered Nov 13 '22 09:11

Sudarshan


Usage example:

copyStringToClipboard("abc123");

    function copyStringToClipboard (str) {
      // Create new element
      var el = document.createElement('textarea');
      // Set value (string to be copied)
      el.value = str;
      // Set non-editable to avoid focus and move outside of view
      el.setAttribute('readonly', '');
      el.style = {position: 'absolute', left: '-9999px'};
      document.body.appendChild(el);
      // Select text inside element
      el.select();
      // Copy text to clipboard
      document.execCommand('copy');
      // Remove temporary element
      document.body.removeChild(el);
    }
like image 39
Petr Varyagin Avatar answered Nov 13 '22 09:11

Petr Varyagin


Suonds like you're trying to copy from a content script. Building off joelpt and Jeff Gran's answers from this answer, here's how to copy any piece of text from a content script:

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

In your main.js or any background script:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  var copyFrom = document.createElement("textarea");
  copyFrom.textContent = request.text;
  var body = document.getElementsByTagName('body')[0];
  body.appendChild(copyFrom);
  copyFrom.select();
  document.execCommand('copy');
  body.removeChild(copyFrom);
})

From your content script:

chrome.runtime.sendMessage({text:textToCopy});
like image 1
SethWhite Avatar answered Nov 13 '22 09:11

SethWhite