WME TTS test

Play TTS

当前为 2016-10-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WME TTS test
// @description  Play TTS
// @version      0.5.0
// @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/23202-wme-tts-test
// @grant        none
// ==/UserScript==

function TTStest_bootstrap()
{
	var oWaze=Window.Waze;
	var oI18n=Window.I18n;

	if (typeof unsafeWindow !== "undefined")
	{
		oWaze=unsafeWindow.Waze;
		oI18n=unsafeWindow.I18n;
	}

	if (typeof oWaze === "undefined")
	{
		setTimeout(TTStest_bootstrap, 500);
		return;
	}
	if (typeof oWaze.map === "undefined")
	{
		setTimeout(TTStest_bootstrap, 500);
		return;
	}
	if (typeof oWaze.selectionManager === "undefined")
	{
		setTimeout(TTStest_bootstrap, 500);
		return;
	}
	if (typeof oI18n === "undefined")
	{
		setTimeout(TTStest_bootstrap, 500);
		return;
	}
	if (typeof oI18n.translations === "undefined")
	{
		setTimeout(TTStest_bootstrap, 500);
		return;
	}

    Waze.selectionManager.events.register("selectionchanged", null, addBtns);
	setTimeout(TTStest_initBindKey, 500);
}

function addBtns()
{
    if(!document.getElementById("WME-test-tts") && Waze.selectionManager.selectedItems.length > 0 && Waze.selectionManager.selectedItems[0].model.type === 'segment')
	{
		var btnSection = document.createElement('div');
		btnSection.id = "WME-test-tts";
		var userTabs = document.getElementById('edit-panel');
		if (!(userTabs && getElementsByClassName('nav-tabs', userTabs)))
			return;

		var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
		if (typeof navTabs !== "undefined")
		{
			if (!getElementsByClassName('tab-content', userTabs))
				return;

			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 <i class="fa fa-lock" id="wmetts_lock"></i></a>';
				navTabs.appendChild(newtab);

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

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

				btnSection.innerHTML = '<div class="form-group">'+
                    '<h4>TTS Test <sup>' + GM_info.script.version + '</sup>&nbsp;<sub><a href="https://greasyfork.org/en/scripts/23202-wme-tts-test" title="Link" target="_blank"><span class="fa fa-external-link"></span></a></sub></h4>' +
					'<div class="controls-container">' +
                    '</br>' +
                    '<button id="wmettsStreet" class="btn btn-default" title="'+streetName+'"><i class="fa fa-volume-up" style="font-size:16px;"></i>&nbsp;&nbsp;'+streetName+'</button>&nbsp;' +
                    '</br></br>' +
                    '<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='';

        if(btnSection.id !== '')
        {
            document.getElementById('wmettsStreet').onclick = playTTS;
            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;
        }
    }
    else if ( !document.getElementById("WME-test-tts") && Waze.selectionManager.selectedItems.length > 0 && Waze.selectionManager.selectedItems[0].model.type == "venue" )
    {
		var btnSection = document.createElement('div');
		btnSection.id = "WME-test-tts";
		var userTabs = document.getElementById('edit-panel');
		if (!(userTabs && getElementsByClassName('nav-tabs', userTabs)))
			return;

		var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
		if (typeof navTabs !== "undefined")
		{
			if (!getElementsByClassName('tab-content', userTabs))
				return;

			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 <i class="fa fa-lock" id="wmetts_lock"></i></a>';
				navTabs.appendChild(newtab);

                var venue = Waze.selectionManager.selectedItems[0].model.attributes.name;
                var poiName = 'ПОИ БЕЗ ИМЕНИ';
                if (Waze.selectionManager.selectedItems[0].model.attributes.name !== "")
                {
                    poiName = Waze.selectionManager.selectedItems[0].model.attributes.name;
                }


				btnSection.innerHTML = '<div class="form-group">'+
                    '<h4>TTS Test <sup>' + GM_info.script.version + '</sup>&nbsp;<sub><a href="https://greasyfork.org/en/scripts/23202-wme-tts-test" title="Link" target="_blank"><span class="fa fa-external-link"></span></a></sub></h4>' +
					'<div class="controls-container">' +
                    '</br>' +
                    '<button id="wmettsStreet" class="btn btn-default" title="'+poiName+'"><i class="fa fa-volume-up" style="font-size:16px;"></i>&nbsp;&nbsp;'+poiName+'</button>&nbsp;' +
					'</div></div>';

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

        if(btnSection.id !== '')
        {
            document.getElementById('wmettsStreet').onclick = playTTS;
            if(document.getElementById('wmettsTL')) document.getElementById('wmettsTL').onclick = playTTS;
            if(document.getElementById('wmettsTR')) document.getElementById('wmettsTR').onclick = playTTS;
            if(document.getElementById('wmettsKL')) document.getElementById('wmettsKL').onclick = playTTS;
            if(document.getElementById('wmettsKR')) document.getElementById('wmettsKR').onclick = playTTS;
            if(document.getElementById('wmettsEL')) document.getElementById('wmettsEL').onclick = playTTS;
            if(document.getElementById('wmettsER')) document.getElementById('wmettsER').onclick = playTTS;
            if(document.getElementById('wmettsRND')) document.getElementById('wmettsRND').onclick = playTTS;
        }
    }

    if(document.getElementById("WME-test-tts"))
    {
        if (Waze.selectionManager.selectedItems.length > 0)
        {
            var disabledButton=false;
            var model=Waze.selectionManager.selectedItems[0].model;

            if ((model.type !== 'segment' || !(model.getAddress().hasOwnProperty('street') && model.getAddress().street !== null && model.getAddress().street.name !== null)) && (model.type !== 'venue' || model.attributes.name === ""))
                disabledButton=true;

            var lock=document.getElementById('wmetts_lock');
            if (lock)
            {

                if (disabledButton)
                {
                    lock.setAttribute('style','color:red;');
                    lock.setAttribute('class', "fa fa-lock");
                }
                else
                {
                    lock.setAttribute('style','color:green;');
                    lock.setAttribute('class', "fa fa-unlock");
                }
            }

            document.getElementById('wmettsStreet').disabled = disabledButton;
            if(document.getElementById('wmettsTL')) document.getElementById('wmettsTL').disabled = disabledButton;
            if(document.getElementById('wmettsTR')) document.getElementById('wmettsTR').disabled = disabledButton;
            if(document.getElementById('wmettsKL')) document.getElementById('wmettsKL').disabled = disabledButton;
            if(document.getElementById('wmettsKR')) document.getElementById('wmettsKR').disabled = disabledButton;
            if(document.getElementById('wmettsEL')) document.getElementById('wmettsEL').disabled = disabledButton;
            if(document.getElementById('wmettsER')) document.getElementById('wmettsER').disabled = disabledButton;
            if(document.getElementById('wmettsRND')) document.getElementById('wmettsRND').disabled = disabledButton;
        }
    }
}

function playTTS()
{
    var ttsName = '';
    if (Waze.selectionManager.selectedItems[0].model.type === 'segment' )
    {
        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")
        {
            if (street.name === null)
            {
                alert('Безымянный сегмент');
                return;
            }
            else
            {
                ttsName = street.name;
            }
        }
        else
        {
            console.log('WME_TTS undefined street');
            return;
        }
    }
    else if (Waze.selectionManager.selectedItems[0].model.type === 'venue')
    {
        ttsName = Waze.selectionManager.selectedItems[0].model.attributes.name;
    }

    var preText = '';
    switch (this.id)
    {
    case 'wmettsStreet':
        preText = ' ';
        break;
    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+ttsName+'&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();



// ------------------------------
function playTTSHand()
{
	if ((typeof arguments[0]) === "object")
	{
		switch(arguments[0].type)
		{
			case 'click': // click button
			{
				var e=document.getElementById(arguments[0].id);
				if(e)
					e.click();

				break;
			}
		}
	}
}

function TTStest_initBindKey()
{
	var Config =[
        {handler: 'wmettsStreet', title: 'Имя сегмента/ПОИ',             func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsStreet'}},
		{handler: 'wmettsTL',     title: 'Поверните налево на',      func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTL'}},
		{handler: 'wmettsTR',     title: 'Поверните направо на',     func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTR'}},
		{handler: 'wmettsKL',     title: 'Держитесь левее на',       func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKL'}},
		{handler: 'wmettsKR',     title: 'Держитесь правее на',      func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKR'}},
		{handler: 'wmettsEL',     title: 'Съезд слева на',           func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsEL'}},
		{handler: 'wmettsER',     title: 'Съезд справа на',          func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsER'}},
		{handler: 'wmettsRND',    title: 'На кольце первый съезд на',func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsRND'}},
	];

	for(var i=0; i < Config.length; ++i)
	{
		WMEKSRegisterKeyboardShortcut('WME TTS test', 'WME TTS test', Config[i].handler, Config[i].title, Config[i].func, Config[i].key, Config[i].arg);
	}

    WMEKSLoadKeyboardShortcuts('WME TTS test');

	window.addEventListener("beforeunload", function() {
		WMEKSSaveKeyboardShortcuts('WME TTS test');
	}, false);
}

// from: https://greasyfork.org/ru/scripts/16071-wme-keyboard-shortcuts (modify)
/*
when adding shortcuts each shortcut will need a uniuque name
the command to add links is WMERegisterKeyboardShortcut(ScriptName, ShortcutsHeader, NewShortcut, ShortcutDescription, FunctionToCall, ShortcutKeysObj) {
	ScriptName: This is the name of your script used to track all of your shortcuts on load and save.
	ScriptName: replace 'WMEAwesome' with your scripts name such as 'SomeOtherScript'
	ShortcutsHeader: this is the header that will show up in the keyboard editor
	NewShortcut: This is the name of the shortcut and needs to be uniuque from all of the other shortcuts, from other scripts, and WME
	ShortcutDescription: This wil show up as the text next to your shortcut
	FunctionToCall: this is the name of your function that will be called when the keyboard shortcut is presses
	ShortcutKeysObj: the is the object representing the keys watched set this to '-1' to let the users specify their own shortcuts.
	ShortcutKeysObj: The alt, shift, and ctrl keys are A=alt, S=shift, C=ctrl. for short cut to use "alt shift ctrl and l" the object would be 'ASC+l'
*/
function WMEKSRegisterKeyboardShortcut(e,r,t,a,o,s,c){try{I18n.translations.en.keyboard_shortcuts.groups[e].members.length}catch(n){Waze.accelerators.Groups[e]=[],Waze.accelerators.Groups[e].members=[],I18n.translations.en.keyboard_shortcuts.groups[e]=[],I18n.translations.en.keyboard_shortcuts.groups[e].description=r,I18n.translations.en.keyboard_shortcuts.groups[e].members=[]}if(o&&"function"==typeof o){I18n.translations.en.keyboard_shortcuts.groups[e].members[t]=a,Waze.accelerators.addAction(t,{group:e});var l="-1",i={};i[l]=t,Waze.accelerators._registerShortcuts(i),null!==s&&(i={},i[s]=t,Waze.accelerators._registerShortcuts(i)),W.accelerators.events.register(t,null,function(){o(c)})}else alert("The function "+o+" has not been declared")}function WMEKSLoadKeyboardShortcuts(e){if(localStorage[e+"KBS"])for(var r=JSON.parse(localStorage[e+"KBS"]),t=0;t<r.length;t++)Waze.accelerators._registerShortcuts(r[t])}function WMEKSSaveKeyboardShortcuts(e){var r=[];for(var t in Waze.accelerators.Actions){var a="";if(Waze.accelerators.Actions[t].group==e){Waze.accelerators.Actions[t].shortcut?(Waze.accelerators.Actions[t].shortcut.altKey===!0&&(a+="A"),Waze.accelerators.Actions[t].shortcut.shiftKey===!0&&(a+="S"),Waze.accelerators.Actions[t].shortcut.ctrlKey===!0&&(a+="C"),""!==a&&(a+="+"),Waze.accelerators.Actions[t].shortcut.keyCode&&(a+=Waze.accelerators.Actions[t].shortcut.keyCode)):a="-1";var o={};o[a]=Waze.accelerators.Actions[t].id,r[r.length]=o}}localStorage[e+"KBS"]=JSON.stringify(r)}
/* ********************************************************** */