WME TTS test

Play TTS

当前为 2017-06-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME TTS test
  3. // @description Play TTS
  4. // @version 0.7.0
  5. // @author Vinkoy
  6. // @include https://www.waze.com/editor/*
  7. // @include https://www.waze.com/*/editor/*
  8. // @include https://editor-beta.waze.com/editor/*
  9. // @include https://editor-beta.waze.com/*/editor/*
  10. // @namespace https://greasyfork.org/en/scripts/23202-wme-tts-test
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. var minutes_value = 20;
  16. var round_exit_value = 1;
  17.  
  18. function TTStest_bootstrap()
  19. {
  20. var oWaze=Window.Waze;
  21. var oI18n=Window.I18n;
  22.  
  23. if (typeof unsafeWindow !== "undefined")
  24. {
  25. oWaze=unsafeWindow.Waze;
  26. oI18n=unsafeWindow.I18n;
  27. }
  28.  
  29. if (typeof oWaze === "undefined")
  30. {
  31. setTimeout(TTStest_bootstrap, 500);
  32. return;
  33. }
  34. if (typeof oWaze.map === "undefined")
  35. {
  36. setTimeout(TTStest_bootstrap, 500);
  37. return;
  38. }
  39. if (typeof oWaze.selectionManager === "undefined")
  40. {
  41. setTimeout(TTStest_bootstrap, 500);
  42. return;
  43. }
  44. if (typeof oI18n === "undefined")
  45. {
  46. setTimeout(TTStest_bootstrap, 500);
  47. return;
  48. }
  49. if (typeof oI18n.translations === "undefined")
  50. {
  51. setTimeout(TTStest_bootstrap, 500);
  52. return;
  53. }
  54.  
  55. Waze.selectionManager.events.register("selectionchanged", null, addBtns);
  56. setTimeout(TTStest_initBindKey, 500);
  57. }
  58.  
  59. function addBtns()
  60. {
  61. if(!document.getElementById("WME-test-tts") && Waze.selectionManager.selectedItems.length > 0 && Waze.selectionManager.selectedItems[0].model.type === 'segment')
  62. {
  63. var btnSection = document.createElement('div');
  64. btnSection.id = "WME-test-tts";
  65. var userTabs = document.getElementById('edit-panel');
  66. if (!(userTabs && getElementsByClassName('nav-tabs', userTabs)))
  67. return;
  68.  
  69. var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
  70. if (typeof navTabs !== "undefined")
  71. {
  72. if (!getElementsByClassName('tab-content', userTabs))
  73. return;
  74.  
  75. var tabContent = getElementsByClassName('tab-content', userTabs)[0];
  76.  
  77. if (typeof tabContent !== "undefined")
  78. {
  79. newtab = document.createElement('li');
  80. newtab.innerHTML = '<a href="#WME-test-tts" id="wmettstest" data-toggle="tab">TTS <i class="fa fa-lock" id="wmetts_lock"></i></a>';
  81. navTabs.appendChild(newtab);
  82.  
  83. var class_style_turn = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #4CC600; cursor: pointer;" ';
  84. var class_style_keep = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #CBFF84; cursor: pointer;" ';
  85. var class_style_exit = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #6CB5FF; cursor: pointer;" ';
  86. var class_style_roundabout = 'class="btn btn-default" style="font-size:24px; width:40px; padding:0px; cursor: pointer;" ';
  87.  
  88. var street = Waze.model.streets.get(Waze.selectionManager.selectedItems[0].model.attributes.primaryStreetID);
  89. var streetName = 'Безымянный сегмент';
  90. if (typeof street !== "undefined")
  91. {
  92. if (street.name !== null)
  93. {
  94. streetName = street.name;
  95. }
  96. }
  97.  
  98. btnSection.innerHTML = '<div class="form-group">'+
  99. '<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>' +
  100. '<div class="controls-container">' +
  101. '</br>' +
  102. '<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;' +
  103. '</br></br>' +
  104. '<button id="wmettsContinue" class="btn btn-default" title="Продолжайте движение около '+minutes_value+' минут"><i class="fa fa-volume-up" style="font-size:16px;"></i>&nbsp;&nbsp;Продолжайте движение около</button>&nbsp;' +
  105. '</br>' +
  106. '<input type="number" id="minutes" value="'+minutes_value+'" style="width: 60px; margin-top: 5px;"/>&nbsp; минут</div>' +
  107. '</br></br>' +
  108. '<label class="control-label">Поверните...</label>' +
  109. '<button id="wmettsTL" '+class_style_turn+' title="Поверните налево">←</button>&nbsp;' +
  110. '<button id="wmettsTR" '+class_style_turn+' title="Поверните направо">→</button>&nbsp;' +
  111. '</br></br>' +
  112. '<label class="control-label">Держитесь...</label>' +
  113. '<button id="wmettsKL" '+class_style_keep+' title="Держитесь левее">↖</button>&nbsp;' +
  114. '<button id="wmettsKR" '+class_style_keep+' title="Держитесь правее">↗</button>&nbsp;' +
  115. '</br></br>' +
  116. '<label class="control-label">Съезд...</label>' +
  117. '<button id="wmettsEL" '+class_style_exit+' title="Съезд слева">↖</button>&nbsp;' +
  118. '<button id="wmettsER" '+class_style_exit+' title="Съезд справа">↗</button>&nbsp;' +
  119. '</br></br>' +
  120. '<label class="control-label">Съезд на кольце...</label>' +
  121. '<button id="wmettsRND" '+class_style_roundabout+' title="На кольце '+round_exit_value+' съезд">☼</button>&nbsp;' +
  122. '<input type="number" id="round_exit" value="'+round_exit_value+'" min="1" max="7" style="width: 60px; margin-top: 5px;"/>&nbsp; съезд</div>' +
  123. '</div></div>';
  124.  
  125. btnSection.className = "tab-pane";
  126. tabContent.appendChild(btnSection);
  127. }
  128. else
  129. btnSection.id='';
  130. }
  131. else
  132. btnSection.id='';
  133.  
  134. if(btnSection.id !== '')
  135. {
  136. document.getElementById('wmettsStreet').onclick = playTTS;
  137. document.getElementById('wmettsContinue').onclick = playTTS;
  138. document.getElementById('wmettsTL').onclick = playTTS;
  139. document.getElementById('wmettsTR').onclick = playTTS;
  140. document.getElementById('wmettsKL').onclick = playTTS;
  141. document.getElementById('wmettsKR').onclick = playTTS;
  142. document.getElementById('wmettsEL').onclick = playTTS;
  143. document.getElementById('wmettsER').onclick = playTTS;
  144. document.getElementById('wmettsRND').onclick = playTTS;
  145. document.getElementById('minutes').onchange = function(){
  146. minutes_value = document.getElementById('minutes').value;
  147. };
  148. document.getElementById('round_exit').onchange = function(){
  149. round_exit_value = document.getElementById('round_exit').value;
  150. };
  151. }
  152. }
  153. else if ( !document.getElementById("WME-test-tts") && Waze.selectionManager.selectedItems.length > 0 && Waze.selectionManager.selectedItems[0].model.type == "venue" )
  154. {
  155. var btnSection = document.createElement('div');
  156. btnSection.id = "WME-test-tts";
  157. var userTabs = document.getElementById('edit-panel');
  158. if (!(userTabs && getElementsByClassName('nav-tabs', userTabs)))
  159. return;
  160.  
  161. var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
  162. if (typeof navTabs !== "undefined")
  163. {
  164. if (!getElementsByClassName('tab-content', userTabs))
  165. return;
  166.  
  167. var tabContent = getElementsByClassName('tab-content', userTabs)[0];
  168.  
  169. if (typeof tabContent !== "undefined")
  170. {
  171. newtab = document.createElement('li');
  172. newtab.innerHTML = '<a href="#WME-test-tts" id="wmettstest" data-toggle="tab">TTS <i class="fa fa-lock" id="wmetts_lock"></i></a>';
  173. navTabs.appendChild(newtab);
  174.  
  175. var venue = Waze.selectionManager.selectedItems[0].model.attributes.name;
  176. var poiName = 'ПОИ БЕЗ ИМЕНИ';
  177. if (Waze.selectionManager.selectedItems[0].model.attributes.name !== "")
  178. {
  179. poiName = Waze.selectionManager.selectedItems[0].model.attributes.name;
  180. }
  181.  
  182.  
  183. btnSection.innerHTML = '<div class="form-group">'+
  184. '<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>' +
  185. '<div class="controls-container">' +
  186. '</br>' +
  187. '<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;' +
  188. '</br></br>' +
  189. '<button id="wmettsContinue" class="btn btn-default" title="Продолжайте движение около '+minutes_value+' минут"><i class="fa fa-volume-up" style="font-size:16px;"></i>&nbsp;&nbsp;Продолжайте движение около</button>&nbsp;' +
  190. '</br>' +
  191. '<input type="number" id="minutes" value="'+minutes_value+'" style="width: 60px; margin-top: 5px;"/>&nbsp; минут</div>' +
  192. '</div></div>';
  193.  
  194. btnSection.className = "tab-pane";
  195. tabContent.appendChild(btnSection);
  196. }
  197. else
  198. btnSection.id='';
  199. }
  200. else
  201. btnSection.id='';
  202.  
  203. if(btnSection.id !== '')
  204. {
  205. if(document.getElementById('wmettsStreet')) document.getElementById('wmettsStreet').onclick = playTTS;
  206. if(document.getElementById('wmettsContinue')) document.getElementById('wmettsContinue').onclick = playTTS;
  207. if(document.getElementById('wmettsTL')) document.getElementById('wmettsTL').onclick = playTTS;
  208. if(document.getElementById('wmettsTR')) document.getElementById('wmettsTR').onclick = playTTS;
  209. if(document.getElementById('wmettsKL')) document.getElementById('wmettsKL').onclick = playTTS;
  210. if(document.getElementById('wmettsKR')) document.getElementById('wmettsKR').onclick = playTTS;
  211. if(document.getElementById('wmettsEL')) document.getElementById('wmettsEL').onclick = playTTS;
  212. if(document.getElementById('wmettsER')) document.getElementById('wmettsER').onclick = playTTS;
  213. if(document.getElementById('wmettsRND')) document.getElementById('wmettsRND').onclick = playTTS;
  214. }
  215. }
  216.  
  217. if(document.getElementById("WME-test-tts"))
  218. {
  219. if (Waze.selectionManager.selectedItems.length > 0)
  220. {
  221. var disabledButton=false;
  222. var model=Waze.selectionManager.selectedItems[0].model;
  223.  
  224. if ((model.type !== 'segment' || !(model.getAddress().hasOwnProperty('street') && model.getAddress().street !== null && model.getAddress().street.name !== null)) && (model.type !== 'venue' || model.attributes.name === ""))
  225. disabledButton=true;
  226.  
  227. var lock=document.getElementById('wmetts_lock');
  228. if (lock)
  229. {
  230.  
  231. if (disabledButton)
  232. {
  233. lock.setAttribute('style','color:red;');
  234. lock.setAttribute('class', "fa fa-lock");
  235. }
  236. else
  237. {
  238. lock.setAttribute('style','color:green;');
  239. lock.setAttribute('class', "fa fa-unlock");
  240. }
  241. }
  242.  
  243. if(document.getElementById('wmettsStreet')) document.getElementById('wmettsStreet').disabled = disabledButton;
  244. if(document.getElementById('wmettsContinue')) document.getElementById('wmettsContinue').disabled = disabledButton;
  245. if(document.getElementById('wmettsTL')) document.getElementById('wmettsTL').disabled = disabledButton;
  246. if(document.getElementById('wmettsTR')) document.getElementById('wmettsTR').disabled = disabledButton;
  247. if(document.getElementById('wmettsKL')) document.getElementById('wmettsKL').disabled = disabledButton;
  248. if(document.getElementById('wmettsKR')) document.getElementById('wmettsKR').disabled = disabledButton;
  249. if(document.getElementById('wmettsEL')) document.getElementById('wmettsEL').disabled = disabledButton;
  250. if(document.getElementById('wmettsER')) document.getElementById('wmettsER').disabled = disabledButton;
  251. if(document.getElementById('wmettsRND')) document.getElementById('wmettsRND').disabled = disabledButton;
  252. }
  253. }
  254. }
  255.  
  256. function playTTS()
  257. {
  258. var ttsName = '';
  259. if (Waze.selectionManager.selectedItems[0].model.type === 'segment' )
  260. {
  261. if (Waze.selectionManager.selectedItems.length != 1)
  262. {
  263. alert('Выберите только один сегмент');
  264. return;
  265. }
  266.  
  267. var street = Waze.model.streets.get(Waze.selectionManager.selectedItems[0].model.attributes.primaryStreetID);
  268. if (typeof street !== "undefined")
  269. {
  270. if (street.name === null)
  271. {
  272. alert('Безымянный сегмент');
  273. return;
  274. }
  275. else
  276. {
  277. ttsName = street.name;
  278. }
  279. }
  280. else
  281. {
  282. console.log('WME_TTS undefined street');
  283. return;
  284. }
  285. }
  286. else if (Waze.selectionManager.selectedItems[0].model.type === 'venue')
  287. {
  288. ttsName = Waze.selectionManager.selectedItems[0].model.attributes.name;
  289. }
  290.  
  291. var preText = '';
  292. switch (this.id)
  293. {
  294. case 'wmettsStreet':
  295. preText = ' ';
  296. break;
  297. case 'wmettsContinue':
  298. preText = 'продолжайте движение около ';
  299. var minutes;
  300. if (document.getElementById('minutes') !== null )
  301. minutes = minutes_value + ' мин. до ';
  302. else
  303. minutes = '20 мин. до ';
  304. ttsName = minutes + ttsName;
  305. break;
  306. case 'wmettsTL':
  307. preText = 'Поверните налево на ';
  308. break;
  309. case 'wmettsTR':
  310. preText = 'Поверните направо на ';
  311. break;
  312. case 'wmettsKL':
  313. preText = 'Держитесь левее на ';
  314. break;
  315. case 'wmettsKR':
  316. preText = 'Держитесь правее на ';
  317. break;
  318. case 'wmettsEL':
  319. preText = 'Съезд слева на ';
  320. break;
  321. case 'wmettsER':
  322. preText = 'Съезд справа на ';
  323. break;
  324. case 'wmettsRND':
  325. var round_exit_text;
  326. switch (round_exit_value)
  327. {
  328. case '1':
  329. round_exit_text = 'первый';
  330. break;
  331. case '2':
  332. round_exit_text = 'второй';
  333. break;
  334. case '3':
  335. round_exit_text = 'третий';
  336. break;
  337. case '4':
  338. round_exit_text = 'четвёртый';
  339. break;
  340. case '5':
  341. round_exit_text = 'пятый';
  342. break;
  343. case '6':
  344. round_exit_text = 'шестой';
  345. break;
  346. case '7':
  347. round_exit_text = 'седьмой';
  348. break;
  349. default:
  350. round_exit_text = 'первый';
  351. }
  352. preText = 'На кольце '+round_exit_text+' съезд на ';
  353. break;
  354. default:
  355. alert("Unknown error");
  356. }
  357.  
  358. if (preText !== '')
  359. {
  360. 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();
  361. }
  362. }
  363.  
  364. function getElementsByClassName(classname, node) {
  365. if(!node)
  366. node = document.getElementsByTagName("body")[0];
  367. var a = [];
  368. var re = new RegExp('\\b' + classname + '\\b');
  369. var els = node.getElementsByTagName("*");
  370. for (var i=0,j=els.length; i<j; i++)
  371. if (re.test(els[i].className)) a.push(els[i]);
  372. return a;
  373. }
  374.  
  375.  
  376. TTStest_bootstrap();
  377.  
  378.  
  379.  
  380. // ------------------------------
  381. function playTTSHand()
  382. {
  383. if ((typeof arguments[0]) === "object")
  384. {
  385. switch(arguments[0].type)
  386. {
  387. case 'click': // click button
  388. {
  389. var e=document.getElementById(arguments[0].id);
  390. if(e)
  391. e.click();
  392.  
  393. break;
  394. }
  395. }
  396. }
  397. }
  398.  
  399. function TTStest_initBindKey()
  400. {
  401. var Config =[
  402. {handler: 'wmettsStreet', title: 'Имя сегмента/ПОИ', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsStreet'}},
  403. {handler: 'wmettsContinue', title: 'Продолжайте движение', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsContinue'}},
  404. {handler: 'wmettsTL', title: 'Поверните налево на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTL'}},
  405. {handler: 'wmettsTR', title: 'Поверните направо на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTR'}},
  406. {handler: 'wmettsKL', title: 'Держитесь левее на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKL'}},
  407. {handler: 'wmettsKR', title: 'Держитесь правее на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKR'}},
  408. {handler: 'wmettsEL', title: 'Съезд слева на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsEL'}},
  409. {handler: 'wmettsER', title: 'Съезд справа на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsER'}},
  410. {handler: 'wmettsRND', title: 'На кольце первый съезд на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsRND'}},
  411. ];
  412.  
  413. for(var i=0; i < Config.length; ++i)
  414. {
  415. WMEKSRegisterKeyboardShortcut('WME TTS test', 'WME TTS test', Config[i].handler, Config[i].title, Config[i].func, Config[i].key, Config[i].arg);
  416. }
  417.  
  418. WMEKSLoadKeyboardShortcuts('WME TTS test');
  419.  
  420. window.addEventListener("beforeunload", function() {
  421. WMEKSSaveKeyboardShortcuts('WME TTS test');
  422. }, false);
  423. }
  424.  
  425. // from: https://greasyfork.org/ru/scripts/16071-wme-keyboard-shortcuts (modify)
  426. /*
  427. when adding shortcuts each shortcut will need a uniuque name
  428. the command to add links is WMERegisterKeyboardShortcut(ScriptName, ShortcutsHeader, NewShortcut, ShortcutDescription, FunctionToCall, ShortcutKeysObj) {
  429. ScriptName: This is the name of your script used to track all of your shortcuts on load and save.
  430. ScriptName: replace 'WMEAwesome' with your scripts name such as 'SomeOtherScript'
  431. ShortcutsHeader: this is the header that will show up in the keyboard editor
  432. NewShortcut: This is the name of the shortcut and needs to be uniuque from all of the other shortcuts, from other scripts, and WME
  433. ShortcutDescription: This wil show up as the text next to your shortcut
  434. FunctionToCall: this is the name of your function that will be called when the keyboard shortcut is presses
  435. ShortcutKeysObj: the is the object representing the keys watched set this to '-1' to let the users specify their own shortcuts.
  436. 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'
  437. */
  438. function WMEKSRegisterKeyboardShortcut(e,r,t,a,o,s,c){try{I18n.translations[I18n.locale].keyboard_shortcuts.groups[e].members.length}catch(n){Waze.accelerators.Groups[e]=[],Waze.accelerators.Groups[e].members=[],I18n.translations[I18n.locale].keyboard_shortcuts.groups[e]=[],I18n.translations[I18n.locale].keyboard_shortcuts.groups[e].description=r,I18n.translations[I18n.locale].keyboard_shortcuts.groups[e].members=[]}if(o&&"function"==typeof o){I18n.translations[I18n.locale].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)}
  439. /* ********************************************************** */
  440.  
  441. })();