Firefox WebExtension没有复制到剪贴板 [英] Firefox webextension not copying to clipboard

查看:61
本文介绍了Firefox WebExtension没有复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firefox Web扩展程序,该扩展程序应该生成将链接复制到剪贴板的按钮。在插件的内容脚本中,我有:

I have a Firefox web extension which is supposed to generate buttons which copy a link to the clipboard. In my content script for the plugin, I have:

    button.onclick = function() {
        var link = window.location.href.replace(/#[0-9a-zA-Z_]+$/, '') + '#' + id;
        var txtToCopy = document.createElement('input');
        txtToCopy.value = link;
        txtToCopy.select();

        console.log(txtToCopy.value);
        var res = document.execCommand('copy');
        console.log(res);

    }

如您所见,我记录了值I正在尝试复制,以及从 execCommand 返回的结果。两者都是我所期望的。

As you can see, I have it logging the value I'm trying to copy, as well as the result returned from execCommand. Both are what I'd expect.

https://thing.example.com#12345
true

但是,它似乎并未将文本实际复制到剪贴板。根据MDN的说法,我不需要任何额外的权限,因为事件正在发生,并且 execCommand 的响应使我可以根据需要设置所有内容。

However, it does not appear to actually copy the text to the clipboard. According to MDN, I shouldn't need any extra permissions as it's is happening in an event, and the response from execCommand makes me thing everything is setup as needed.

我正在Ubuntu 16.04和Firefox 51.0.1上启用e10s。也许e10s是我的问题,会给予更新。

I'm running on Ubuntu 16.04, Firefox 51.0.1, with e10s enabled. Maybe e10s is my problem, will give update.

推荐答案

您必须将 txtToCopy 附加到DOM才能从中复制并且必须是可见的(或多或少)。

You have to append txtToCopy to the DOM to copy from it and it has to be "visible" (more or less).

button.onclick = function() {
    var link = window.location.href.replace(/#[0-9a-zA-Z_]+$/, '') + '#' + id;
    var txtToCopy = document.createElement('input');
    txtToCopy.style.left = '-300px';
    txtToCopy.style.position = 'absolute';
    txtToCopy.value = link;
    document.body.appendChild(txtToCopy);
    txtToCopy.select();

    console.log(txtToCopy.value);
    var res = document.execCommand('copy');
    console.log(res);

    txtToCopy.parentNode.removeChild(txtToCopy);

}

这篇关于Firefox WebExtension没有复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆