Saturday, May 11, 2024
94
rated 0 times [  98] [ 4]  / answers: 1 / hits: 25635  / 11 Years ago, thu, february 21, 2013, 12:00:00

I want to write some data in to clipborad from a chrome extension which I'm creating.
In the manifest file i gave permissions to both clipboardRead and clipboardWrite.



i use this function which i found here



but it doesn't work. seems that document.execCommand('copy'); can not work.



i write all of these codes in content script.



thx
manifest:



{
manifest_version:2,

name:easyCopy,
description:just a small toll,
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.


More From » google-chrome-extension

 Answers
42

Thanks everyone, I ended up using this:



document.execCommand can not work in content script.
Instead, I send data to background page and then run the copyTextToClipboard function.



Notice that you must put your JavaScript into single .js file instead of mixing it with background.html.



Additionally, the textarea must have an id or class property.


[#80081] Wednesday, February 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;