2011-03-08 104 views

回答

7

在你的内容脚本,有这样的:

// step 1: get the text you mean to copy 
// (actual implementation not included) 

// step 2: send it to your background page 
chrome.extension.sendRequest({ text: "text you want to copy" }); 

在你的背景页,有这样的:

// step 3: set up your background page HTML 
// and 
<html> 
<head> 
<script type="text/javascript"> 
    chrome.extension.onRequest.addListener(function (msg, sender, sendResponse) { 

     var textarea = document.getElementById("tmp-clipboard"); 

     // now we put the message in the textarea 
     textarea.value = msg.text; 

     // and copy the text from the textarea 
     textarea.select(); 
     document.execCommand("copy", false, null); 


     // finally, cleanup/close the connection 
     sendResponse({}); 
    }); 
    </script> 
    </head> 

    <body> 
    <textarea id="tmp-clipboard"></textarea> 
    </body> 
</html> 
+0

我有文字可在弹出的文本区域已经复制。我还需要使用背景页吗?我尝试了doc.execCommand(“copy”,false,null);命令,但似乎并不奏效。 :S – thameera 2011-03-11 11:16:32

+0

这个问题/答案对初学者非常有用,这是一个常见问题。这个答案可以更新为使用'sendMessage'和'onMessage'吗? – LukeP 2015-01-11 18:47:29

相关问题