Copy to clipboard without Flash

后端 未结 8 841
情深已故
情深已故 2020-11-28 05:28

I found many solutions for copying to the clipboard, but they all either with flash, or for websites side. I\'m looking for method copy to clipboard automatically, without f

相关标签:
8条回答
  • 2020-11-28 06:02

    clipboard.js has just been released to copy to clipboard without the need of Flash

    See it in action here > http://zenorocha.github.io/clipboard.js/#example-action

    0 讨论(0)
  • 2020-11-28 06:08

    While waiting impatiently for Xbrowser support of the Clipboard API...


    This will work beautifully in Chrome, Firefox, Edge, IE

    IE will only prompt the user once to access the Clipboard.
    Safari (5.1 at the time of writing) does not support execCommand for copy/cut

    /**
     * CLIPBOARD
     * https://stackoverflow.com/a/33337109/383904
     */
    const clip = e => {
      e.preventDefault();
      
      const cont = e.target.innerHTML;
      const area = document.createElement("textarea");
      
      area.value = e.target.innerHTML; // or use .textContent
      document.body.appendChild(area);
      area.select();
     
      if(document.execCommand('copy')) console.log("Copied to clipboard");
      else prompt("Copy to clipboard:\nSelect, Cmd+C, Enter", cont); // Saf, Other
      
      area.remove();
    };
    
    
    [...document.querySelectorAll(".clip")].forEach(el => 
      el.addEventListener("click", clip)
    );
    <a class="clip" href="#!">Click an item to copy</a><br>
    <a class="clip" href="#!"><i>Lorem</i></a><br>
    <a class="clip" href="#!"><b>IPSUM</b></a><br>
    
    <textarea placeholder="Paste here to test"></textarea>

    All browsers (except Firefox which is able to only handle mime type "plain/text" as far as I've tested) have not implemented the Clipboard API. I.e, trying to read the clipboard event in Chrome using

    var clipboardEvent = new ClipboardEvent("copy", {
            dataType: "plain/text",
            data: "Text to be sent to clipboard"
    });
    

    throws: Uncaught TypeError: Illegal constructor

    The best resource of the unbelievable mess that's happening among browsers and the Clipboard can be seen here (caniuse.com) (→ Follow the comments under "Notes").
    MDN says that basic support is "(YES)" for all browsers which is inaccurate cause one would expect at least the API to work, at all.

    0 讨论(0)
  • 2020-11-28 06:13

    It's finally here! (As long as you don't support Safari or IE8... -_- )

    You can now actually handle clipboard actions without Flash. Here's the relevant documentation:

    https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

    https://developers.google.com/web/updates/2015/04/cut-and-copy-commands?hl=en

    https://msdn.microsoft.com/en-us/library/hh801227%28v=vs.85%29.aspx#copy

    0 讨论(0)
  • 2020-11-28 06:15

    There is not way around, you have to use flash. There is a JQuery plugin called jquery.copy that provided cross browser copy and paste by using a flash (swf) file. This is similar to how the syntax highlighter on my blog works.

    Once you reference the jquery.copy.js file all you need to do to push data into the clipboard is run the following:

    $.copy("some text to copy");
    

    Nice and easy ;)

    0 讨论(0)
  • 2020-11-28 06:20

    I had tryed the flash solution and I don't liked too. Too complex and too slow. What I did was to create a textarea, put the data into that and use the browser "CTRL + C" behavior.

    The jQuery javascript part:

    // catch the "ctrl" combination keydown
    $.ctrl = function(key, callback, args) {
        $(document).keydown(function(e) {
            if(!args) args=[]; // IE barks when args is null
            if(e.keyCode == key && e.ctrlKey) {
                callback.apply(this, args);
                return false;
            }
        });
    };
    
    // put your data on the textarea and select all
    var performCopy = function() {
        var textArea = $("#textArea1");
        textArea.text('PUT THE TEXT TO COPY HERE. CAN BE A FUNCTION.');
        textArea[0].focus();
        textArea[0].select();
    };
    
    // bind CTRL + C
    $.ctrl('C'.charCodeAt(0), performCopy);
    

    The HTML part:
    <textarea id="textArea1"></textarea>

    Now, put what do you want to copy in 'PUT THE TEXT TO COPY HERE. CAN BE A FUNCTION.' area. Works fine for me me. You just have to make one CTRL+C combination. The only drawback is that you are going to have an ugly textarea displayed in you site. If you use the style="display:none" the copy solution will not work.

    0 讨论(0)
  • 2020-11-28 06:23

    Without flash, it's simply not possible in most browsers. The user's clipboard is a security-relevant resource since it could contain things like passwords or credit card numbers. Thus, browsers rightly don't allow Javascript access to it (some allow it with a warning shown that the user has confirm, or with signed Javascript code, but none of that is cross-browser).

    0 讨论(0)
提交回复
热议问题