There are several problems in the code.
- From Chrome 20 sendRequest deprecated in favor of sendMessage
- From Chrome 20, onRequest.addListener is deprecated in favor of onMessage.addListener
- Due to CSP, you cannot have a tag in your code
After resolving these issues, your code is working properly.
Demonstration
Demonstration example of your use case
manifest.json
Guaranteed manifest has all permissions and registrations
{ "name":"Copy Command", "description":"http://stackoverflow.com/questions/14171654/chrome-extension-how-to-select-all-text-of-tab-and-copy", "version":"1", "manifest_version":2, "background":{ "page":"background.html" }, "permissions": [ "clipboardRead", "clipboardWrite" ], "content_scripts":[ { "matches":["<all_urls>"], "js":["script.js"] } ] }
background.html
Enforcing All Security Changes
<html> <head> <script src="background.js"></script> </head> <body> <textarea id="tmp-clipboard"></textarea> </body> </html>
background.js
Listener added to simulate Ctrl + A and Ctrl + C
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) { //Set Content document.getElementById("tmp-clipboard").value = msg.text; //Get Input Element document.getElementById("tmp-clipboard").select(); //Copy Content document.execCommand("Copy", false, null); });
contentscript.js
Transferring Content for Copy
chrome.extension.sendMessage({ text: "text you want to copy" });
References
source share