WME TTS test

Play TTS

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

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