WME TTS test

Play TTS

目前為 2016-09-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name         WME TTS test
// @description  Play TTS
// @version      0.1
// @author       Vinkoy
// @include      https://www.waze.com/editor/*
// @include      https://www.waze.com/*/editor/*
// @include      https://editor-beta.waze.com/editor/*
// @include      https://editor-beta.waze.com/*/editor/*
// @namespace    https://greasyfork.org/en/scripts/24162-vinkoy
// @grant        none
// ==/UserScript==

function TTStest_bootstrap()
{
    Waze.selectionManager.events.register("selectionchanged", null, addBtns);
}

function addBtns()
{
    if(!document.getElementById("WME-test-tts"))
	{
		var btnSection = document.createElement('div');
		btnSection.id = "WME-test-tts";
		var userTabs = document.getElementById('edit-panel');
		var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
		if (typeof navTabs !== "undefined")
		{
			var tabContent = getElementsByClassName('tab-content', userTabs)[0];

			if (typeof tabContent !== "undefined")
			{
				newtab = document.createElement('li');
				newtab.innerHTML = '<a href="#WME-test-tts" id="wmettstest" data-toggle="tab">TTS</a>';
				navTabs.appendChild(newtab);

                var class_style_turn = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #4CC600;" ';
                var class_style_keep = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #CBFF84;" ';
                var class_style_exit = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #6CB5FF;" ';
                var class_style_roundabout = 'class="btn btn-default" style="font-size:24px; width:40px; padding:0px;" ';

                
				btnSection.innerHTML = /*'<hr>'+*/
					'<div class="form-group">'+
					'<div class="controls-container">' +
                    '<label class="control-label">Поверните...</label>' +
					'<button id="wmettsTL" '+class_style_turn+' title="Поверните налево">←</button>&nbsp;' +
                    '<button id="wmettsTR" '+class_style_turn+' title="Поверните направо">→</button>&nbsp;' +
                    '</br></br>' +
                    '<label class="control-label">Держитесь...</label>' +
                    '<button id="wmettsKL" '+class_style_keep+' title="Держитесь левее">↖</button>&nbsp;' +
                    '<button id="wmettsKR" '+class_style_keep+' title="Держитесь правее">↗</button>&nbsp;' +
                    '</br></br>' +
                    '<label class="control-label">Съезд...</label>' +
                    '<button id="wmettsEL" '+class_style_exit+' title="Съезд слева на">↖</button>&nbsp;' +
                    '<button id="wmettsER" '+class_style_exit+' title="Съезд справа на">↗</button>&nbsp;' +
                    '</br></br>' +
                    '<label class="control-label">Съезд на кольце...</label>' +
                    '<button id="wmettsRND" '+class_style_roundabout+' title="На кольце первый съезд на">☼</button>&nbsp;' +
					'</div></div>';

				btnSection.className = "tab-pane";
				tabContent.appendChild(btnSection);
			}
			else
				btnSection.id='';
        }
        else
			btnSection.id='';
    }

    document.getElementById('wmettsTL').onclick = playTTS;
	document.getElementById('wmettsTR').onclick = playTTS;
	document.getElementById('wmettsKL').onclick = playTTS;
    document.getElementById('wmettsKR').onclick = playTTS;
    document.getElementById('wmettsEL').onclick = playTTS;
    document.getElementById('wmettsER').onclick = playTTS;
    document.getElementById('wmettsRND').onclick = playTTS;
}

function playTTS()
{
    if (Waze.selectionManager.selectedItems.length != 1)
    {
        alert('Выберите только один сегмент');
		return;
    }

    var street = Waze.model.streets.get(Waze.selectionManager.selectedItems[0].model.attributes.primaryStreetID);
    if (typeof street !== "undefined")
    {
        streetname=street.name;
        if (street.name === null)
        {
            alert('Безымянный сегмент');
            return;
        }
    }
    else
    {
        console.log('WME_TTS undefined street');
        return;
    }

    var preText = '';
    switch (this.id)
    {
    case 'wmettsTL':
        preText = 'Поверните налево на ';
        break;
    case 'wmettsTR':
        preText = 'Поверните направо на ';
        break;
    case 'wmettsKL':
        preText = 'Держитесь левее на ';
        break;
    case 'wmettsKR':
        preText = 'Держитесь правее на ';
        break;
    case 'wmettsEL':
        preText = 'Съезд слева на ';
        break;
    case 'wmettsER':
        preText = 'Съезд справа на ';
        break;
    case 'wmettsRND':
        preText = 'На кольце первый съезд на ';
        break;
    default:
        alert("Unknown error");
    }

    if (preText !== '')
    {
    new Audio('https://ttsgw.world.waze.com/TTSGateway/Text2SpeechServlet?text='+preText+street.name+'&lang=ru-RU&lon=0&lat=0&version=6&protocol=2&sessionid=12345654321&content_type=audio%2Fmpeg&type=street&validate_data=positive&skipCache=true').play();
    }
}

function getElementsByClassName(classname, node) {
	if(!node)
		node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for (var i=0,j=els.length; i<j; i++)
		if (re.test(els[i].className)) a.push(els[i]);
	return a;
}

TTStest_bootstrap();