MediaWiki:Gadget-speechSynthewiki.js: различия между версиями

Нет описания правки
Нет описания правки
Строка 1: Строка 1:
/**
/**  
  * Speech Synthewiki — гаджет озвучки статей браузером пользователя
  * Speech Synthewiki — гаджет озвучки статей браузером пользователя
  * Версия 0.3.5 — MediaWiki 1.43; скины Vector 2022 / Vector 2010 / Monobook
  * Версия 0.3 (beta) — надо поработать над озвучкой формул
  * Языки кнопок по языку интерфейса пользователя: ru / en / eo / udm
* Для скинов: Vector-10, Vector-22, Monobook
*/
  * Кнопки по языку интерфейса пользователя, пока поддержка 4 языков
/* global mw, $ */
*/
 
/*****************************************************************/
  // ИСПОЛЬЗОВАНИЕ
  // Возле каждого раздела статьи появляется ссылка «озвучить»
  // Нажимаешь → читает весь текст под этим заголовком
  // Если хочешь остановить — жмёшь «стоп» (кнопка меняется)
  // Когда раздел заканчивается — воспроизведение останавливается
  // Кнопка «озвучить всё» озвучивает всю статью
/*****************************************************************/


(function () {
(function () {
Строка 255: Строка 264:
             if (t) chunks.push(t);
             if (t) chunks.push(t);
             return true;
             return true;
        });
        return chunks.join('\n');
    }
    // ---------- сбор текста всей статьи ----------
    function buildFullText() {
        var $root = $('#mw-content-text .mw-parser-output');
        if (!$root.length) $root = $('#mw-content-text');
        if (!$root.length) return '';
        var chunks = [];
        // преамбула
        var lead = buildLeadText();
        if (lead) chunks.push(lead);
        // все секции по порядку
        $root.find('.mw-heading, h1, h2, h3, h4, h5, h6').each(function () {
            var $h = $(this);
            if ($h.is('#mw-toc-heading, #toctitle, .toctitle')) return;
            if ($h.closest('#toc, .toc, .vector-toc, .mw-toc, #mw-panel-toc').length) return;
            if ($h.find('#toctitle, .toctitle').length) return;
            var $realHeader = $h.is('.mw-heading')
                ? $h.children('h1,h2,h3,h4,h5,h6')
                : $h;
            var $start = $realHeader.next();
            if (!$start.length) $start = $realHeader.parent().next();
            var $stop = $start.nextAll('.mw-heading, h1, h2, h3, h4, h5, h6').first();
            var t = $start.length ? buildSpeechText($start, $stop.length ? $stop : null) : '';
            if (t) chunks.push(t);
         });
         });


Строка 275: Строка 316:
         var text;
         var text;
         if ($(a).data('lead')) {
         if ($(a).data('lead')) {
             text = buildLeadText();
             text = buildFullText();         // «озвучить всё» = преамбула + все секции
         } else {
         } else {
             var $heading = $(a).closest('.mw-heading, h1, h2, h3, h4, h5, h6');
             var $heading = $(a).closest('.mw-heading, h1, h2, h3, h4, h5, h6');
Строка 333: Строка 374:
         if (!$root.length) return;
         if (!$root.length) return;


         // --- кнопка преамбулы ---
         // --- кнопка «озвучить всё» ---
        var $firstHeader = $root.find('h1, h2, h3, h4, h5, h6, .mw-heading').first();
         if (!$root.find('.cyclo-tts-lead').length) {
        var hasLead = false;
        $root.children().each(function () {
            if ($firstHeader.length && this === $firstHeader[0]) return false;
            var $el = $(this);
            if ($el.is('.cyclo-tts-lead, .cyclo-tts-start, #toc, .toc, .vector-toc, .mw-toc, #mw-panel-toc')) return true;
            if ($el.is('p, ul, ol, dl, table, blockquote') && $el.text().trim()) {
                hasLead = true;
                return false;
            }
            return true;
        });
 
         if (hasLead && !$root.find('.cyclo-tts-lead').length) {
             var $leadBar = $('<div class="cyclo-tts-lead" style="margin:.5em 0;"></div>');
             var $leadBar = $('<div class="cyclo-tts-lead" style="margin:.5em 0;"></div>');
             var $leadBtn = $('<a href="#" style="cursor:pointer;"></a>')
             var $leadBtn = $('<a href="#" style="cursor:pointer;"></a>')