Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
let finalTranscript = '';
let recognition = new window.SpeechRecognition();

recognition.interimResults = true;
recognition.maxAlternatives = 10;
recognition.continuous = true;

recognition.onresult = (event) => {
  let interimTranscript = '';
  for (let i = event.resultIndex, len = event.results.length; i < len; i++) {
    let transcript = event.results[i][0].transcript;
    if (event.results[i].isFinal) {
      finalTranscript += transcript;
      //alert(finalTranscript);
      if (finalTranscript == "main page"){
      	window.location.href = "https://en.wikipedia.org/wiki/Main_Page";
      }
      if (finalTranscript == "contents"){
      	window.location.href = "https://en.wikipedia.org/wiki/Wikipedia:Contents";
      }
      if (finalTranscript == "current events"){
      	window.location.href = "https://en.wikipedia.org/wiki/Portal:Current_events";
      }
      if (finalTranscript == "random page" || finalTranscript == "random"){
      	window.location.href = "https://en.wikipedia.org/wiki/Special:Random";
      }
      if (finalTranscript == "about wikipedia" || finalTranscript == "about"){
      	window.location.href = "https://en.wikipedia.org/wiki/Wikipedia:About";
      }
      if (finalTranscript == "contact us" || finalTranscript == "contact"){
      	window.location.href = "https://en.wikipedia.org/wiki/Wikipedia:Contact_Us";
      }
      if (finalTranscript == "donate"){
      	window.location.href = "https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&utm_medium=sidebar&utm_campaign=C13_en.wikipedia.org&uselang=en";
      }
      if (finalTranscript == "pause" || finalTranscript == "stop"){
      	location.reload();
      	speechSynthesis.cancel();
      }
      if (finalTranscript == "read"){
      	funcread();
      }
    } else {
      interimTranscript += transcript;
    }
  }

  document.querySelector('body').innerHTML = finalTranscript + '<i style="color:#ddd;">' + interimTranscript + '</>';
};
recognition.start();
//alert("Unfortunately, your browser does not support speech recognition");// REMEMBER TO CHANGE THIS TO TTS
//alert("Does this work?");
function funcread(){
	if(window.speechSynthesis.getVoices().length === 0) {
		window.speechSynthesis.addEventListener('voiceschanged', function() {
			textToSpeech();
		});
	}
	else {
		// languages list available, no need to wait
		textToSpeech();
}
}
function textToSpeech(){
	// get all voices that browser offers
	var available_voices = window.speechSynthesis.getVoices();

	// this will hold an english voice
	var english_voice = '';

	// find voice by language locale "en-US"
	// if not then select the first voice
	for(var i=0; i<available_voices.length; i++) {
		if(available_voices[i].lang === 'en-US') {
			english_voice = available_voices[i];
			break;
		}
	}
	if(english_voice === '')
		english_voice = available_voices[0];

	// new SpeechSynthesisUtterance object
	var pagecontent = document.getElementById("mw-content-text").textContent;
	var utter = new SpeechSynthesisUtterance();
	utter.rate = 1;
	utter.pitch = 0.5;
	utter.text = pagecontent;
	utter.voice = english_voice;

	// event after text has been spoken
	utter.onend = function() {
		//alert('Speech has finished');
	};

	// speak
	location.reload();
	window.speechSynthesis.speak(utter);
}