代码之家  ›  专栏  ›  技术社区  ›  nh2

如何在Firefox 3.5中将文本复制到剪贴板?

  •  1
  • nh2  · 技术社区  · 14 年前

    如何将文本复制到剪贴板 使用javascript (甚至更好的jquery函数) 不涉及闪存 ?

    我不在乎IE和其他浏览器; 火狐3.5 更改本地FF设置的唯一重要浏览器是否正常?

    编辑 : 对不起,我不清楚 尝试一下我通过谷歌找到的前30种方法,它们都不适合我。

    4 回复  |  直到 11 年前
        3
  •  0
  •   Don Carnage    14 年前

    默认情况下,出于安全原因,禁止火狐访问剪贴板。 您可以在firefox中编辑设置以允许:

    在火狐地址栏中,输入:“about:config” (没有引号),然后按Enter。

    在现在看到的“过滤器”框中,输入单词 “签名”,您应该只有一个结果 起来。设置为禁用。双击它,它 应改为启用。关上窗户。从那时起 在上,当网站试图 访问你的剪贴板,你可以告诉它“总是 允许此网站…”

        4
  •  0
  •   nh2    11 年前

    现在问题解决了,我想添加一些组成的javascript。

    • 按照接受答案中的描述配置Firefox。

    然后使用类似的东西(从Mozilla资源复制):

    function copyToClipboard(text) {
    
    // ask for permission to access clipboard
    
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch(e) {
        alert("Clipboard copying is not allowed. Set signed.applets.codebase_principal_support to 'true' in Mozilla Firefox.");
        return false;
    }
    
    // make a copy of the Unicode
    
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    if (!str) return false; // couldn't get string obj
    str.data = text; // unicode string?
    
    
    // add Unicode & HTML flavors to the transferable widget
    
    var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
    if (!trans) return false; //no transferable widget found
    
    trans.addDataFlavor("text/unicode");
    trans.setTransferData("text/unicode", str, text.length * 2); // *2 because it's unicode 
    
    
    // copy the transferable widget!
    
    var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
    if (!clipboard) return false; // couldn't get the clipboard
    
    clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
    return true;
    
    }