Monday, June 3, 2024
76
rated 0 times [  77] [ 1]  / answers: 1 / hits: 42968  / 13 Years ago, mon, august 22, 2011, 12:00:00

I'm trying to paste clipboard data into a textarea using execcommand(paste) with a chome extension, but i cannot seem to get it to work.
permissions are set.
I have tried to set focus() on the textarea, but document.execCommand(paste) does nothing, and I get no error.
calling execcommand(paste) from background page also does nothing.



<form>
<textarea id=ta></textarea>
</form>
<script type=text/javascript>
document.findElemetById(ta).focus();
document.execCommand(paste);
</script>

More From » google-chrome-extension

 Answers
6

Clipboard functionality is a key part of my extension so I've seen all the normal problems. On my background page I expose a copy and a paste function and the page itself contains <textarea id=sandbox></textarea>;



function copy(str) {
var sandbox = $('#sandbox').val(str).select();
document.execCommand('copy');
sandbox.val('');
}

function paste() {
var result = '',
sandbox = $('#sandbox').val('').select();
if (document.execCommand('paste')) {
result = sandbox.val();
}
sandbox.val('');
return result;
}


I'm using jQuery for simplicity but you get the idea. Now any time I want to use the clipboard functionality I simply call the relevant function. Other pages in my extension can access this API via chrome.extension.getBackgroundPage() but you can also use chrome.runtime.getBackgroundPage(callback) if your background page is an event page.



I'm not sure if this is best practice or if such a thing even exists for this functionality yet but this definitely works for me and is very clean.


[#90497] Friday, August 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
timothyc

Total Points: 233
Total Questions: 103
Total Answers: 103

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
timothyc questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Sun, May 22, 22, 00:00, 2 Years ago
Fri, Jun 12, 20, 00:00, 4 Years ago
;