Jump to content

MediaWiki:Common.js: Difference between revisions

From IoT Colabs Wiki
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function() {
/* Add copy ICON to syntaxhighlight blocks */
    $('.mw-highlight').each(function() {
mw.hook('wikipage.content').add(function($content) {
        var $this = $(this);
    // Standard "Clipboard" SVG icon
        // Create button
    var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
        var $button = $('<button class="copy-button"><svg aria-hidden="true" class="MSS8xf" fill="currentColor" height="18px" viewBox="0 -960 960 960" width="18px"><path d="M360-240q-33 0-56.5-23.5T280-320v-480q0-33 23.5-56.5T360-880h360q33 0 56.5 23.5T800-800v480q0 33-23.5 56.5T720-240H360Zm0-80h360v-480H360v480ZM200-80q-33 0-56.5-23.5T120-160v-560h80v560h440v80H200Zm160-240v-480 480Z"></path></svg></button>');
    var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
         $button.css({
 
            'position': 'absolute',
    $content.find('.mw-highlight').each(function() {
            'top': '5px',
        var $block = $(this);
            'right': '5px',
         if ($block.find('.copy-button').length > 0) return;
             'font-size': '0.8em',
 
             'cursor': 'pointer'
        var $button = $('<button>')
        });
             .addClass('copy-button')
       
             .html(copyIcon) // Inject the SVG here
        // Add button to container
            .attr('title', 'Copy to clipboard')
        $this.css('position', 'relative').append($button);
            .on('click', function() {
       
                var code = $block.find('pre').text();
        // Copy function
                navigator.clipboard.writeText(code).then(function() {
        $button.click(function() {
                    $button.html(checkIcon); // Switch to checkmark on success
            var text = $this.find('pre').text();
                    setTimeout(function() { $button.html(copyIcon); }, 2000);
            navigator.clipboard.writeText(text).then(function() {
                });
                $button.text('Copied!');
                setTimeout(function() { $button.text('Copy'); }, 2000);
             });
             });
         });
 
         $block.css('position', 'relative').prepend($button);
     });
     });
});
});

Revision as of 10:01, 6 May 2026

/* Any JavaScript here will be loaded for all users on every page load. */
/* Add copy ICON to syntaxhighlight blocks */
mw.hook('wikipage.content').add(function($content) {
    // Standard "Clipboard" SVG icon
    var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
    var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';

    $content.find('.mw-highlight').each(function() {
        var $block = $(this);
        if ($block.find('.copy-button').length > 0) return;

        var $button = $('<button>')
            .addClass('copy-button')
            .html(copyIcon) // Inject the SVG here
            .attr('title', 'Copy to clipboard')
            .on('click', function() {
                var code = $block.find('pre').text();
                navigator.clipboard.writeText(code).then(function() {
                    $button.html(checkIcon); // Switch to checkmark on success
                    setTimeout(function() { $button.html(copyIcon); }, 2000);
                });
            });

        $block.css('position', 'relative').prepend($button);
    });
});