我正在尝试使用以下命令将剪贴板数据粘贴到文本区域中execcommand("paste")
带有 chrome 扩展,但我似乎无法让它工作。
权限已设置。
我尝试过设置focus()
在文本区域上,但是document.execCommand("paste")
什么也没做,我也没有收到任何错误。
呼叫execcommand("paste")
从后台页面也不执行任何操作。
<form>
<textarea id="ta"></textarea>
</form>
<script type="text/javascript">
document.findElemetById("ta").focus();
document.execCommand("paste");
</script>
剪贴板功能是一个关键部分我的分机 https://chrome.google.com/webstore/detail/template/dcjnfaoifoefmnbhhlbppaebgnccfddf所以我已经看到了所有正常的问题。在我的背景页面上我公开了一个copy
and a paste
函数和页面本身包含<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;
}
我在用着jQuery http://jquery.com/为了简单起见,但你明白了。现在,每当我想使用剪贴板功能时,我只需调用相关函数即可。我的扩展程序中的其他页面可以通过以下方式访问此 APIchrome.extension.getBackgroundPage() https://developer.chrome.com/extensions/extension#method-getBackgroundPage但你也可以使用chrome.runtime.getBackgroundPage(回调) https://developer.chrome.com/extensions/runtime#method-getBackgroundPage如果您的背景页面是活动页面 https://developer.chrome.com/extensions/event_pages.
我不确定这是否是最佳实践,或者是否存在此功能,但这绝对适合我并且非常干净。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)