4
我发现有一个实验剪贴板类。但它只适用于开发渠道,对吧?任何想法如何我可以复制文本?如何从Google Chrome扩展程序将文本复制到剪贴板?
我发现有一个实验剪贴板类。但它只适用于开发渠道,对吧?任何想法如何我可以复制文本?如何从Google Chrome扩展程序将文本复制到剪贴板?
在你的内容脚本,有这样的:
// 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>
我有文字可在弹出的文本区域已经复制。我还需要使用背景页吗?我尝试了doc.execCommand(“copy”,false,null);命令,但似乎并不奏效。 :S – thameera 2011-03-11 11:16:32
这个问题/答案对初学者非常有用,这是一个常见问题。这个答案可以更新为使用'sendMessage'和'onMessage'吗? – LukeP 2015-01-11 18:47:29