PageSourceSearch

https://engineer-history.ru/js/jquery.copy-to-clipboard.js?v=1790194167

js engineer-history.ru collected 2026-10-03 21:33:42 UTC 2,639 bytes, 77 lines download raw bytes

1/*
2 * Copyright (c) 2016 Milan Kyncl
3 * Licensed under the MIT license.
4 *
5 * jquery.copy-to-clipboard plugin
6 * https://github.com/mmkyncl/jquery-copy-to-clipboard
7 *
8 * Forked: async Clipboard API with the original execCommand as fallback (BTX-21501).
9 */
10
11$.fn.CopyToClipboard = function() {
12    var textToCopy = false;
13    if(this.is('select') || this.is('textarea') || this.is('input')){
14        textToCopy = this.val();
15    }else {
16        textToCopy = this.text();
17    }
18    return CopyToClipboard(textToCopy);
19};
20
21/**
22 * Copies val to the system clipboard.
23 *
24 * Returns a promise resolving to true when the text really got there. It never
25 * rejects: most callers ignore the result and must not leave it unhandled.
26 */
27function CopyToClipboard( val ){
28    // execCommand copies nothing on iOS Safari, so the async API goes first.
29    // It needs a secure context, and dev still runs on http (BTX-21501).
30    // Same condition as backend/views/tour/index.php.
31    if (window.isSecureContext && navigator.clipboard) {
32        return navigator.clipboard.writeText(val).then(function(){
33            return true;
34        }, function(){
35            // Denied by policy, or the document lost focus. Measured: the promise
36            // rejects in ~2ms, so the click still counts as a user gesture here.
37            return CopyToClipboardFallback(val);
38        });
39    }
40
41    return Promise.resolve(CopyToClipboardFallback(val));
42}
43
44/** The original plugin body: execCommand over a hidden textarea. Copies nothing on iOS. */
45function CopyToClipboardFallback( val ){
46    var hiddenClipboard = $('#_hiddenClipboard_');
47    if(!hiddenClipboard.length){
48        $('body').append('<textarea style="position:absolute;top: -9999px;" id="_hiddenClipboard_"></textarea>');
49        hiddenClipboard = $('#_hiddenClipboard_');
50    }
51    hiddenClipboard.html(val);
52    hiddenClipboard.select();
53    var copied = document.execCommand('copy');
54    document.getSelection().removeAllRanges();
55    hiddenClipboard.remove();
56
57    return copied;
58}
59
60$(function(){
61    $('[data-clipboard-target]').each(function(){
62        $(this).click(function() {
63            $($(this).data('clipboard-target')).CopyToClipboard();
64        });
65    });
66    $('[data-clipboard-text]').each(function(){
67        $(this).click(function(){
68            var $el = $(this);
69            // Listeners show a "copied" state, so announce a real copy only (BTX-21501).
70            CopyToClipboard($el.data('clipboard-text')).then(function(copied){
71                if (copied) {
72                    $el.trigger('copied.clipboard');
73                }
74            });
75        });
76    });
77});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.