【问题标题】:ZeroClipboard broken in FireFoxZeroClipboard 在 Firefox 中损坏
【发布时间】:2014-10-15 16:26:46
【问题描述】:

我有一个复制文本按钮,我正在使用 ZeroClipboard 来复制页面上的某些文本。它适用于 Chrome 和 IE,但它不会在 Firefox 中复制文本,并且永远不会触发 complete 事件。

我用于设置按钮的 JavaScript 如下所示:

ZeroClipboard.setDefaults({
  moviePath: '/js/zeroclipboard/ZeroClipboard.swf',
  allowScriptAccess: 'always',
  forceHandCursor: true
});

function enableCopyButton(container) {
  var button = container.find('.text-copy'),
      source = container.find('.text'),
      clip = new ZeroClipboard(button);

  clip.setText(source.val());

  clip.on('load', function (client) {
    console.log('ZeroClipboard loaded.');

    client.on('complete', function (client, args) {
      console.log('Text copied: ' + args.text);
    });
  });

  clip.on('noFlash', function () {
    console.error('No Flash installed!');
  });
  clip.on('wrongFlash', function () {
    console.error('Wrong Flash installed!');
  });
}

控制台最终显示"ZeroClipboard loaded." 而没有其他内容。没有抛出任何错误,并且我已经确认 ZeroClipboard.swf 正在加载并放置在页面上。 mousedownmouseup 事件也被触发。正在运行的页面正在使用有效的 SSL 证书,并且页面上的所有资产都是通过 HTTPS 加载的。

GitHub 上的库演示页面在 FireFox 中运行良好,所以我怀疑这是我正在做的事情。

【问题讨论】:

  • 我能看到的唯一可能错误的部分是source.val()source 是否在 Firefox 的范围内,并且该变量是否具有 val 函数?如果该代码不是问题,如果没有 example 进行测试,我将无法找出问题所在。
  • 我更新了我的问题以更好地展示这一点。它在范围内,问题不在于正在设置的文本。

标签: javascript zeroclipboard


【解决方案1】:

我不确定这是否会有所帮助,但最近我已经使用 zeroclipboard 一个多月了。有时它可以工作,但有时它会因为一些非常小的事情而失败(可能是因为我对 javascript 和 html 的东西很陌生)......而且我非常理解这种不安......

在您的情况下,您以前尝试过这个活动吗? 而是单独使用 clip.setText(source.val()),将其移动到 'dataRequested' 事件中,如下所示:

clip.on('dataRequested', function(client, args) {
    client.setText(source.val());
});

然后单击按钮后,查看是否触发了 complete 事件。

顺便说一句,我想知道您的“noflash”或“wrongflash”是否正常触发?在我的情况下,如果用户没有安装 flash,事件仍然没有被触发......不确定它有什么问题......

无论如何,祝你好运:)

【讨论】:

【解决方案2】:

我的开发环境:

  • .NET 4.5
  • 带有 Razor 引擎的 ASP.NET MVC4
  • jQuery

为了让 Copy to Clipboard 能够在 5 个浏览器上运行,我做了以下操作:

  • FF 23
  • IE 10
  • 铬 29
  • Safari 5.1.7
  • 歌剧 16

我的场景: 我要复制到剪贴板的文本是生成的,并与 html 中断 (br) 一起放入 Div。 对于副本,我需要删除这些 html 中断并将它们替换为 /r/n。 复制是通过按钮单击。

这可能不是最好的编码方式,但它对我有用。

github获取最新的ZeroClipboard

在我的 .cshtml 文件中,我定义了按钮和 div 并包含 ZeroClipboard.min.js 文件。

<!-- language-all: lang-html -->
<button id="btCopyToClipboard" name="btCopyToClipboard" type="button">Copy To Clipboard</button>
<div id="Basket" ></div>

Javascript 部分:

<!-- language: lang-js -->
<script type="text/javascript">
    $(document).ready(function () {
        //"style" the buttons
        $("#btCopyToClipboard").button();   

        //ZeroClipboard code
        var clip = new ZeroClipboard(document.getElementById('btCopyToClipboard'), {
            moviePath: "/Scripts/ZeroClipboard.swf",  // URL to movie
            trustedOrigins: null, //null Page origins that the SWF should trust (single string or array of strings)
            hoverClass: "",   // The class used to hover over the object
            activeClass: "",  // The class used to set object active
            allowScriptAccess: "always",               //sameDomain SWF outbound scripting policy
            useNoCache: true,                       // Include a nocache query parameter on requests for the SWF
            forceHandCursor: true                       //false Forcibly set the hand cursor ("pointer") for all glued elements
        });
        //if just using var clip = new ZeroClipboard(); then need to use .glue(..)
        //clip.glue(document.getElementById('btCopyToClipboard'));
        clip.on('load', function (client, args) {
            DebugLog("ZeroClipboard.swf is loaded and user's flash version is: " + args.flashVersion);
        });

        //The complete event is fired when the text is successfully copied to the clipboard.
        clip.on('complete', function (client, args) {
            //alert("clip.onComplete(..) -- Copied text to clipboard args.text: " + args.text);
        });

        clip.on('mouseover', function (client) {
            // alert("mouse over");
        });

        clip.on('mouseout', function (client) {
            //alert("mouse out");
        });

        clip.on('mousedown', function (client) {
            //alert("mouse down");
        });

        clip.on('mouseup', function (client) {
            //alert("mouse up");
        });

        clip.on('dataRequested', function (client, args) {
            //get text from basket
            var txt = $("#Basket").html();
            //to make Notepad honour line breaks, we have to do some magic
            var windowsText = txt.replace(/\n/g, '\r\n');
            //replace html break with line breaks
            windowsText = windowsText.replace(/<br\s*\/?>/g, "\r\n");
            client.setText(windowsText);
        });

        clip.on('noflash', function (client, args) {
            var msg = "You don't support flash, therefore the Copy To Clipboard will not work.";
            DebugLog(msg);
            alert(msg);
        });
        clip.on('wrongflash', function (client, args) {
            var msg = 'Your flash is too old ' + args.flashVersion + '.  The Copy To Clipboard supports version 10 and up.';
            DebugLog(msg);
            alert(msg);
        });

        function DebugLog(message) {
            if (console && console.log) {
                console.log(message);
            }
        }
    });//eof $(document).ready
</script>

【讨论】:

    【解决方案3】:

    zeroclipboard 的最新版本使用 event.stopImmediatePropagation,它在 28.0 版之前的 firefox 中不存在,它失败并出现错误:

    event.stopImmediatePropagation is not a function
    

    您可以在此处查看浏览器比较:

    http://compatibility.shwups-cms.ch/de/home?&property=TrackEvent.prototype.stopImmediatePropagation

    我使用这个 polifil 来添加缺失的功能:

    https://github.com/mattcg/stopImmediatePropagation

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      相关资源
      最近更新 更多