MediaWiki:Gadget-Speech.js

Aus Wiktionary, dem freien Wörterbuch
//<nowiki>

// begin namespace: foSpeech
(function( _public, $, undefined ) {

var voices = null;
var toSpeak = null;
var counter = {};  //Positionsmerker für einzelne Abschnitte
var prefs = {idx: 0, rate: 6, pitch: 10, volume: 50 };  // 0.1 - 10.0, 0.0 - 2.0, 0.0 - 1.0


_public.init = function ()
{
  if ('speechSynthesis' in window) {
    voices = speechSynthesis.getVoices();  // lädt asynchron
  }
  addIcons("1","p[title='Phonetik']");
  addIcons("2","p[title='Sinn und Bezeichnetes (Semantik)']");
  addIcons("3","p[title='Verwendungsbeispielsätze']");
}


// Anbringen der Bedienelemente
function addIcons(idx,selector) {
  var select = $(selector);
  var dl = $(select).nextAll("dl");
  if (dl.length > 0) {
    $(select).css('display', 'inline');
    addLinks(select,idx);
  }
}


// Bedienelemente anbringen
function addLinks(element,idx) {
 var serv = "//upload.wikimedia.org/wiktionary/de";
 $(element).each(function(idx2) {
  var id = 'sp' + idx + 'x' + idx2;
  $( this ).attr("id",id);
  $( this ).append(' ',
  '<a class="noprint" href="#" onclick="foSpeech.play('+"'"+id+"'"+');return false;"> &#x1F509;</a>',
  '<span class="noprint panel" style="display:none"> · ' + 
  '<a href="#" onclick="foSpeech.back('+"'"+id+"'"+');return false;">' +
  '<img src="' + serv + '/8/89/foIcon-B-PlayPrev.png" alt="&#x2590;&#x25C0;" height="20" width="20"></a> · ' +
  '<a href="#" onclick="foSpeech.play('+"'"+id+"'"+');return false;">' +
  '<img src="' + serv + '/2/29/foIcon-B-Play.png" alt="&#x25B6;" height="20" width="20"></a> · ' +
  '<a href="#" onclick="foSpeech.forward('+"'"+id+"'"+');return false;">' +
  '<img src="' + serv + '/2/2f/foIcon-B-PlayNext.png" alt="&#x25B6;&#x258C;" height="20" width="20"></a> · ' +
  '<a href="" onclick="foSpeech.help('+"'"+id+"'"+');return false;">' +
  '<img src="' + serv + '/9/9b/foIcon-B-Config.png" alt="&#x2261;" height="20" width="20"</a></span>');
 });
}


_public.config = function ()
{
  if ('speechSynthesis' in window) {
    voices = speechSynthesis.getVoices();
    _public.load();
    $("#spSlider").after('<b>Sprachausgabe:</b>','<br />',
    'Lautstärke: ',
    '<input type="range" min="0" max="100" value="' + prefs.volume +'" id="volume">','<br />',
    'Tonhöhe: ',
    '<input type="range" min="0" max="20" value="' + prefs.pitch +'" id="pitch">','<br />',
    'Geschwindigkeit: ',
    '<input type="range" min="1" max="30" value="' + prefs.rate +'" id="rate">','<br />');

    addVoices();
    createButtonTest();

  } else {
    $("#spSlider").after('<b>Dieser Browser unterstützt keine Sprachausgabe!</b>');
  }
}


function addVoices() {
  if (voices.length > 0) {
    var txt = "";
    for (var i = 0; i < voices.length; i++) {
      txt += '<input type="radio" name="voice" value="' + i;
      if (i == prefs.idx) {
        txt += '" checked="true';
      }
      txt += '">' + voices[i].name + " :: " + voices[i].lang + "<br />";
    }
    $("#spVoices").after('<br />','<b>Stimmen: </b>','<br />',txt);
  } else {
    $("#spVoices").after('<br />','<b>Stimmen: </b>','Auf diesem Gerät ist keine Stimmenauswahl verfügbar.','<br />');
  }
}


_public.back = function (chapter) {
  if (!counter.hasOwnProperty(chapter)) {
    counter[chapter] = 1;
  }
  counter[chapter]--;
  _public.play(chapter);
}


_public.forward = function (chapter) {
  if (!counter.hasOwnProperty(chapter)) {
    counter[chapter] = -1;
  }
  counter[chapter]++;
  _public.play(chapter);
}


_public.help = function (chapter) {
  window.location.href = buildUrl2("Hilfe:Speech", "");
}


_public.play = function (chapter) {

  $(".panel").fadeIn();
  if ('speechSynthesis' in window) {
    voices = speechSynthesis.getVoices();
    if (toSpeak && toSpeak.speaking) {
      toSpeak.cancel();
      return;
    }
    _public.load();
    toSpeak = new SpeechSynthesisUtterance();
    toSpeak.voice = voices[prefs.idx];
    toSpeak.volume = prefs.volume / 100;
    toSpeak.rate = prefs.rate / 10;
    toSpeak.pitch = prefs.pitch / 10;

    toSpeak.lang = "de-DE";

    if (chapter.charAt(2) == 1) {
      toSpeak.text = mw.config.get("wgTitle");
    } else if (chapter.charAt(2) == 2) {
      toSpeak.text = "Bedeutung ";
      var pBeddd = $('#'+chapter).nextAll("dl").first().children("dd");

      if (pBeddd.length > 0) {
        if (!counter.hasOwnProperty(chapter)) {
          counter[chapter] = 0;
        }
        if (counter[chapter] >= pBeddd.length) {
          counter[chapter] = 0;
        }
        if (counter[chapter] < 0) {
          counter[chapter] = pBeddd.length - 1;
        }

        toSpeak.text += removeBrackets($(pBeddd[counter[chapter]]).text()) + "."; // ein Punkt am Ende senkt die Stimme
      }

    } else if (chapter.charAt(2) == 3) {
      toSpeak.text = "Beispiel zu Bedeutung ";
      var pBspdd = $('#'+chapter).nextAll("dl").first().children("dd");

      if (pBspdd.length > 0) {
        if (!counter.hasOwnProperty(chapter)) {
          counter[chapter] = 0;
        }
        if (counter[chapter] >= pBspdd.length) {
          counter[chapter] = 0;
        }
        if (counter[chapter] < 0) {
          counter[chapter] = pBspdd.length - 1;
        }
        toSpeak.text += removeBrackets($(pBspdd[counter[chapter]]).text());
      }
    } else {
      toSpeak.text = chapter;
    }

    speechSynthesis.speak(toSpeak);

  } else {
    alert("Dieser Browser unterstützt die Funktion nicht!");
  }

}


_public.testen = function (chapter) {
  var voice = document.querySelector('input[name="voice"]:checked');
  if (voice) {
    prefs.idx = voice.value;
  }
  prefs.volume = document.getElementById("volume").value;
  prefs.rate = document.getElementById("rate").value;
  prefs.pitch = document.getElementById("pitch").value;

  toSpeak = new SpeechSynthesisUtterance();
  toSpeak.voice = voices[prefs.idx];
  toSpeak.volume = prefs.volume / 100;
  toSpeak.rate = prefs.rate / 10;
  toSpeak.pitch = prefs.pitch / 10;

  toSpeak.lang = "de-DE";
  toSpeak.text = "Aussprache";
  speechSynthesis.speak(toSpeak);

}



_public.load = function () {
  var value = mw.cookie.get('preferencesSpeechJs') || '0|50|10|6';
  var values = value.split("|");
  prefs.idx = values[0];
  prefs.volume = values[1];
  prefs.rate = values[2];
  prefs.pitch = values[3];
}


_public.save = function () {
  var voice = document.querySelector('input[name="voice"]:checked');
  if (voice) {
    prefs.idx = document.querySelector('input[name = "voice"]:checked').value;
  } else {
    prefs.idx = 0;
  }
  prefs.volume = document.getElementById("volume").value;
  prefs.rate = document.getElementById("rate").value;
  prefs.pitch = document.getElementById("pitch").value;
  var value = prefs.idx + "|" + prefs.volume + "|" + prefs.rate + "|" + prefs.pitch;
  mw.cookie.set('preferencesSpeechJs', value, { expires: 86400 * 360 });
  $("#spSave").after(' gespeichert!');
}


// Die Klammern des ersten eckigen Klammerpaares (Bedeutungsangabe) nicht aussprechen
// und Komma hinzufügen, um Bedeutungsnummer vom Rest des Textes abzugrenzen
function removeBrackets(txt){
  return txt.replace(/\[([0-9]+)\]/, "$1,");
}


function createButtonTest(){
  $("#spPlay").after('<br />',
   '<input type="button" title="Ausprobieren" value="Testen" onclick="foSpeech.testen();return false;">',
   '<br />','<br />',
   '<input type="button" id="spSave" title="Einstellungen in einem Cookie speichern" value="Einstellungen speichern" onclick="foSpeech.save();return false;">');
}


function addCORS() {
 if (window.location.hostname.indexOf(".m.") >= 0) {  // mobile site
  return "origin=" + window.location.protocol + "//" + window.location.hostname + "&";
 }
 return "";
}

function getServer() {
 var server = mw.config.get("wgServer");
 if (window.location.hostname.indexOf(".m.") >= 0) {  // mobile site
  if (server.indexOf(".m.") < 0) {
   return server.replace(".", ".m.");
  }
 }
 return server;
}

function buildApi(t,p) {
 return getServer() + mw.config.get("wgScriptPath") + "/api.php?" + addCORS() +
        p + mw.util.wikiUrlencode(t);
}

function buildUrl2(t,p) {
 return getServer() + mw.config.get("wgScript") + "?title=" + mw.util.wikiUrlencode(t) + p;
}


// end namespace: foSpeech
}( window.foSpeech = window.foSpeech || {}, jQuery ));

if (mw.config.get("wgNamespaceNumber") == 0 && mw.config.get("wgAction") == "view") {
  jQuery(document).ready(foSpeech.init);
}
if (mw.config.get("wgPageName") == "Hilfe:Speech" && mw.config.get("wgAction") == "view") {
  jQuery(document).ready(foSpeech.config);
}

//</nowiki>