如何从Google Chrome扩展程序将文本复制到剪贴板? [英] How to copy text to clipboard from a Google Chrome extension?
本文介绍了如何从Google Chrome扩展程序将文本复制到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
在您的内容脚本中,有这个:
//第1步:获取您要复制的文本
//(不包含实际实现)
//第2步:将其发送到您的背景页面
chrome.extension.sendRequest({text:您要复制的文本});
在你的背景页面中,有这个:
//第3步:设置您的背景页面HTML
//和
< html>
< head>
< script type =text / javascript>
chrome.extension.onRequest.addListener(函数(msg,sender,sendResponse){
var textarea = document.getElementById(tmp-clipboard);
//现在我们把消息放在textarea
textarea.value = msg.text;
//并且从textarea复制文本
textarea.select();
document.execCommand(copy,false,null);
//最后,清理/关闭连接
sendResponse({});
});
< / script>
< / head>
< body>
< textarea id =tmp-clipboard>< / textarea>
< / body>
< / html>
I found that there's an experimental clipboard class. But it works only in dev channel, right? Any idea how I can copy the text?
解决方案
In your content script, have this:
// 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" });
In your background page, have this:
// 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>
这篇关于如何从Google Chrome扩展程序将文本复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文