Jump to content

Widget:SelectivePreToClip

From RiskiPedia

<style> .selective-pretoclip-widget {

   position: relative;

} .selective-pretoclip-widget pre {

   position: relative;
   padding-top: 30px;

} .pretoclip-copy-button {

   position: absolute;
   top: 5px;
   right: 5px;

} </style>

<script> ( function ( mw ) {

   'use strict';
   if ( !navigator.clipboard ) {
       console.error( 'Widget:SelectivePreToClip: `navigator.clipboard` not available. HINT: Only works from HTTPS or `localhost` context.' );
       return;
   }
   var widgetContainers = document.querySelectorAll( '.selective-pretoclip-widget' );
   widgetContainers.forEach( function( container ) {
       var preElements = container.querySelectorAll( 'pre' );
       preElements.forEach( function( pre ) {
           if ( !pre.getAttribute( 'tabindex' ) ) {
               pre.setAttribute( 'tabindex', '0' );
           }
           var clipboardText = pre.innerText;
           var tooltip = mw.msg( 'pretoclip-button-tooltip' ) || 'Copy to clipboard';
           var copyButton = new OO.ui.ButtonWidget( {
               icon: 'copy',
               classes: [ 'pretoclip-copy-button' ],
               tabIndexed: true,
               title: tooltip,
               data: {
                   clipboardText: clipboardText
               }
           } );
           copyButton.on( 'click', function () {
               var data = this.getData();
               navigator.clipboard.writeText( data.clipboardText ).then( function() {
                   mw.notify( mw.msg( 'pretoclip-button-notification-text' ) || 'Text copied to clipboard!' );
               }).catch( function( err ) {
                   mw.notify( 'Failed to copy text: ' + err, { type: 'error' } );
               });
           }, [], copyButton );
           pre.prepend( copyButton.$element[ 0 ] );
       });
   });

}( mediaWiki ) ); </script>