MediaWiki:Common.js

Версия от 01:42, 24 сентября 2026; Admin (обсуждение | вклад) (Кнопка копирования документов)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.

  • Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
  • Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
  • Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
  • Opera: Нажмите Ctrl+F5.
/* Кнопка «Скопировать» у документов (страница «Бумажная работа») */
( function () {
    'use strict';

    function copyText( text, onDone ) {
        if ( navigator.clipboard && navigator.clipboard.writeText ) {
            navigator.clipboard.writeText( text ).then( onDone, function () { fallback( text, onDone ); } );
            return;
        }
        fallback( text, onDone );
    }

    function fallback( text, onDone ) {
        var area = document.createElement( 'textarea' );
        area.value = text;
        area.setAttribute( 'readonly', '' );
        area.style.position = 'fixed';
        area.style.top = '-1000px';
        document.body.appendChild( area );
        area.select();
        try { document.execCommand( 'copy' ); onDone(); } catch ( e ) { /* ничего */ }
        document.body.removeChild( area );
    }

    function init() {
        var items = document.querySelectorAll( 'table.doc-item' );
        Array.prototype.forEach.call( items, function ( item ) {
            var paper = item.querySelector( 'pre.doc-paper' );
            var head = item.querySelector( 'th' );
            if ( !paper || !head || item.querySelector( '.doc-copy' ) ) { return; }

            var button = document.createElement( 'button' );
            button.type = 'button';
            button.className = 'doc-copy';
            button.textContent = 'Скопировать';
            button.title = 'Скопировать текст документа';
            button.addEventListener( 'click', function () {
                copyText( paper.textContent, function () {
                    button.textContent = 'Скопировано';
                    button.classList.add( 'is-done' );
                    window.setTimeout( function () {
                        button.textContent = 'Скопировать';
                        button.classList.remove( 'is-done' );
                    }, 1500 );
                } );
            } );
            head.appendChild( button );
        } );
    }

    if ( document.readyState === 'loading' ) {
        document.addEventListener( 'DOMContentLoaded', init );
    } else {
        init();
    }
}() );