Path: blob/main/documentation/themes/beastie/assets/js/copy-clipboard.js
18096 views
/*1BSD 2-Clause License23Copyright (c) 1994-2026, The FreeBSD Documentation Project4Copyright (c) 2021-2026, Sergio Carlavilla5All rights reserved.67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions are met:9101. Redistributions of source code must retain the above copyright notice, this11list of conditions and the following disclaimer.12132. Redistributions in binary form must reproduce the above copyright notice,14this list of conditions and the following disclaimer in the documentation15and/or other materials provided with the distribution.1617THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829;(function () {30'use strict'3132document.querySelectorAll(".rouge, .highlight").forEach(function(codeItem) {33var sourceCode = codeItem.textContent;3435var icon = document.createElement("i");36icon.className = "fa fa-clipboard";3738var tooltip = document.createElement("span");39tooltip.className = "tooltip";40tooltip.innerHTML = "Copied!";4142var button = document.createElement("button");43button.title = "Copy to clipboard";44button.appendChild(icon);45button.appendChild(tooltip);4647var clipboardWrapper = document.createElement("div");48clipboardWrapper.className = "copy-to-clipboard-wrapper";49clipboardWrapper.appendChild(button);5051codeItem.appendChild(clipboardWrapper);5253button.addEventListener('click', copyToClipboard.bind(button, sourceCode));54});5556function copyToClipboard(text, item) {57const tooltip = item.target.nextElementSibling;58window.navigator.clipboard.writeText(text).then(function() {59if (tooltip) {60tooltip.classList.add("show-tooltip");61setTimeout(function(){62tooltip.classList.remove("show-tooltip");63}, 1200);64}65});66}6768})();697071