PageSourceSearch

https://anmda.fr/modules/contrib/better_social_sharing_buttons/js/copy-current-url.js?v=1.x

js anmda.fr collected 2026-10-02 04:53:47 UTC 2,463 bytes, 77 lines download raw bytes

1/**
2 * Copy the current page url to clipboard when clicking on the .btnCopy button.
3 * Based on
4 * https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript/30810322#30810322
5 */
6
7(function ($) {
8  'use strict';
9
10  // Selector of the element that copies link when clicked.
11  Drupal.behaviors.copyButtonElements = {
12    attach: function (context) {
13      var $copyButton = $('.btnCopy');
14      $copyButton.once('copy-current-url').each(function () {
15        // Adding click event on each anchor element.
16        $(this).on('click', function (e) {
17          e.preventDefault();
18          var popupElements = $('.social-sharing-buttons__popup');
19          Drupal.copyTextToClipboard(window.location.href, popupElements);
20        });
21      });
22    }
23  };
24
25  // Function to copy current url to clipboard.
26  // Shows a popupmessage on screen if url was copied successful.
27  Drupal.copyTextToClipboard = function (text, popupElements) {
28    if (!navigator.clipboard) {
29      Drupal.fallbackCopyTextToClipboard(text, popupElements);
30      return;
31    }
32
33    navigator.clipboard.writeText(text, popupElements)
34      .then(function () {
35        Drupal.showCopiedMessage(popupElements);
36      }, function (err) {
37        console.error('Error copying current url to clipboard: ', err);
38      });
39  };
40
41  // Fallback copy functionality using using older document.execCommand('copy') for when the normal clipboard
42  // functionality (navigator.clipboard) does not work. This generates a textarea with url as content and the copies that
43  // content using the document.execCommand('copy') command.
44  Drupal.fallbackCopyTextToClipboard = function (text, popupElements) {
45
46    var $inputURL = $("<input>");
47    $("body").append($inputURL);
48    $inputURL.val(window.location.href).select();
49
50    try {
51      document.execCommand("copy");
52      Drupal.showCopiedMessage(popupElements);
53    }
54    catch (err) {
55      console.error('Error copying current url to clipboard', err);
56    }
57
58    $inputURL.remove();
59  };
60
61  // Show a popup if the current url was successfully copied.
62  Drupal.showCopiedMessage = function (popupElements) {
63    var visibleClass = 'visible',
64      $popupElements = popupElements;
65
66    $popupElements.each(function () {
67      $(this).addClass(visibleClass);
68    });
69
70    setTimeout(function () {
71      $popupElements.each(function () {
72        $(this).removeClass(visibleClass);
73      });
74    }, 4000);
75  };
76
77})(jQuery, Drupal);

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.