2013-08-20 65 views
1

我有一個複製文本按鈕,我正在使用ZeroClipboard以複製頁面上的某些文本。它適用於Chrome和IE,但不會在Firefox中複製文本,並且事件永遠不會被解僱。在Firefox中破解ZeroClipboard

我設置了按鈕的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中工作正常,所以我懷疑這是我正在做的事情。

+0

唯一的可能是,錯誤的部分,我可以看到的是'source.val()'。 Firefox的範圍是'source',並且該變量是否具有'val'功能?如果該代碼不是問題,那麼如果沒有[示例](http://jsfiddle.net/)進行測試,則無法找出問題。 –

+0

我更新了我的問題以顯示更好。這是在範圍內,問題不在於正在設置的文本。 – coreyschram

回答

0

我的開發環境:

  • .NET 4.5
  • ASP.NET MVC4剃刀引擎
  • jQuery的

這是我做得到複製到剪貼板在整個工作5種瀏覽器:

  • FF 23
  • IE 10
  • 鉻29
  • 的Safari 5.1.7
  • 歌劇院16

我的情景: 我想複製到剪貼板中的文本生成放於一個DIV隨着HTML休息(br)。 對於副本,我需要刪除這些html中斷,並用/ r/n替換它們。 副本是通過按鈕單擊。

不是這可能不是編碼的最好方法,但它對我有用。

抓鬥最新ZeroClipboard從github

在我的.cshtml文件,我定義按鈕和股利和包括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> 
1

我不知道這是否會幫助,但最近我一直在使用zeroclipboard超過一個月。有時它的工作原理,但有時由於某種非常微小的事情(也許是因爲我很新的JavaScript和HTML的東西)......我明白心煩非常好...

你的情況,你有沒有曾經嘗試過這個事件嗎? 改爲使用clip.setText(source。VAL()單獨),將它「dataRequested」事件中是這樣的:

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

然後點擊按鈕之後,看看是否完整事件被炒魷魚。

順便說一句,我想知道你的'noflash'或'wrongflash'被正常解僱了嗎?在我的情況下,如果沒有安裝的用戶閃存,事件仍然沒有燒......不知道什麼地方錯了...

不管怎麼說,祝你好運:)

+0

我可能在[PR#273](https://github.com/zeroclipboard/zeroclipboard/pull/273)中解決了這個問題。試試最新的beta版:[v1.3.0-beta.1](https://github.com/zeroclipboard/zeroclipboard/releases/tag/v1.3.0-beta.1) –