I am trying to implement a feature in my Chrome extension where I can write data to the clipboard. I have already specified permissions for clipboardRead and clipboardWrite in the manifest file.
I came across a function that I thought would work, which I found here. However, it seems that the line "document.execCommand('copy');" is not functioning as expected.
All of this code is written in the content script.
Thanks, manifest:
{
"manifest_version":2,
"name":"easyCopy",
"description":"just a small tool",
"version":"1.0.0",
"permissions":[
"clipboardWrite", "http://*/*", "clipboardRead"
],
"content_scripts":[
{
"matches":["http://*/*"],
"js":["jquery-1.9.1.min.js", "main_feature.js"]
}
],
"background":{
"persistent":false,
"page":"background.html"
}
}
main_feature.js:
copyOrderId();
function copyOrderId() {
$(".order-num").click(function () {
var curOrderNum = $(this).text();
copyTextToClipboard(curOrderNum);
// chrome.extension.sendMessage({method:"copy", content:curOrderNum}, function (response) {
// clog(response);
// });
});
}
function copyTextToClipboard(text) {
var copyFrom = $('<textarea/>');
copyFrom.text(text);
$('body').append(copyFrom);
copyFrom.select();
document.execCommand('copy', true);
copyFrom.remove();
}
function clog(message) {
console.log(message);
}
the background.html is just a blank page with basic html body.