【问题标题】:the proper use of execcommand("paste") in a chrome extension在 chrome 扩展中正确使用 execcommand("paste")
【发布时间】:2011-08-22 08:16:02
【问题描述】:

我正在尝试使用带有 chome 扩展名的 execcommand("paste") 将剪贴板数据粘贴到文本区域中,但我似乎无法让它工作。 权限已设置。 我试图在 textarea 上设置 focus(),但 document.execCommand("paste") 什么也没做,我也没有收到任何错误。 从后台页面调用execcommand("paste") 也无济于事。

<form>
     <textarea id="ta"></textarea>    
</form>
<script type="text/javascript">
    document.findElemetById("ta").focus();
    document.execCommand("paste");
</script>

【问题讨论】:

  • 小观察,但您的意思是document.getElementById 因为document.findElementById 不存在?我确定这不是您的真实代码的问题,因为这肯定会导致错误。
  • 也看到这个问题:stackoverflow.com/questions/6969403/…

标签: javascript google-chrome-extension


【解决方案1】:

剪贴板功能是my extension 的关键部分,所以我已经看到了所有正常问题。在我的背景页面上,我公开了一个copy 和一个paste 函数,并且页面本身包含&lt;textarea id="sandbox"&gt;&lt;/textarea&gt;

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,但你明白了。现在,每当我想使用剪贴板功能时,我只需调用相关函数即可。我的扩展程序中的其他页面可以通过chrome.extension.getBackgroundPage() 访问此API,但如果您的背景页面是event page,您也可以使用chrome.runtime.getBackgroundPage(callback)

我不确定这是否是最佳实践,或者这个功能是否存在这样的东西,但这绝对适合我并且非常干净。

【讨论】:

  • 非常感谢我无法让您的代码开箱即用,但更改结果 = sandbox.val();结果 = $("#sandbox").val();成功了
  • 啊,救命稻草!有一个问题,我复制了一些使用 textarea style='display: none;' 的其他代码这停止了​​它的工作,但这完美地解决了问题!
  • 确保您也设置了权限。 “权限”:[“剪贴板写入”]
  • 您好,今天 07/21/2015,document.execCommand('copy') 无需扩展即可正常工作,但 document.execCommand('paste') 不起作用。我还需要扩展吗?
  • 有人可以展示一个没有 jQuery 的例子吗?也许链接到github gist?这会有所帮助
【解决方案2】:

对于 Alasdair 的出色回应,评论太长了,所以我正在创建另一个答案。 Alasdair 的回答非常好,对我来说效果很好,但作为 Chrome 扩展的新手,我仍然需要一段时间才能让它工作。对于处于类似位置的任何人,这是他的答案的扩展。

背景/事件页面能够与系统剪贴板交互,前提是您已请求适当的权限。他们无法与用户加载的页面的 DOM 交互。内容脚本不能与系统剪贴板交互,但它们可以与用户加载的页面的 DOM 交互。请查看explanation of the extension architecture 以全面了解这一切。

这实质上意味着您需要在事件/背景页面中从系统剪贴板执行复制/粘贴操作,这就是 Alasdair 上面概述的内容。从用户正在查看的页面的 DOM 中进行的任何粘贴或复制都必须在您的内容脚本中进行。这两个脚本可以很容易地与message passing 通信。

我有an extension,其唯一目的是粘贴,架构主要来自这篇文章。如果您想在实践中看到上述技术,take a look at the code。特别是background.htmlbackground.jscontentscript.js

如果你真的很着急,here is a gist

【讨论】:

    【解决方案3】:
      function PasteString() {
        var editor = document.getElementById("TemplateSubPage");
        editor.focus();
      //  editor.select();
        document.execCommand('Paste');
    }
    
    function CopyString() {
        var input = document.getElementById("TemplateSubPage");
        input.focus();
       // input..select();
        document.execCommand('Copy');
        if (document.selection) {
            document.selection.empty();
        } else if (window.getSelection) {
            window.getSelection().removeAllRanges();
        }
    }
    

    希望这对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-03
      • 2020-05-26
      • 1970-01-01
      • 2014-08-05
      • 2018-04-15
      • 2018-09-11
      • 2011-02-06
      • 1970-01-01
      相关资源
      最近更新 更多