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:
clipboardRead
and clipboardWrite
permission (already done)<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?
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;
}
You can try the following code, it writes text to clipboard
As an example i wrote Sample
to clipboard
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
}
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>
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;
});
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;
});
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);
}
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});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With