WME TTS test

Play TTS

当前为 2016-09-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME TTS test
  3. // @description Play TTS
  4. // @version 0.4
  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;" ';
  80. var class_style_keep = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #CBFF84;" ';
  81. var class_style_exit = 'class="btn btn-default" style="font-size:18px; width:40px; padding:0px; background-color: #6CB5FF;" ';
  82. var class_style_roundabout = 'class="btn btn-default" style="font-size:24px; width:40px; padding:0px;" ';
  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.  
  138. if(document.getElementById("WME-test-tts"))
  139. {
  140. if (Waze.selectionManager.selectedItems.length > 0)
  141. {
  142. var disabledButton=false;
  143. var model=Waze.selectionManager.selectedItems[0].model;
  144.  
  145. if (model.type !== 'segment' || !(model.getAddress().hasOwnProperty('street') && model.getAddress().street !== null && model.getAddress().street.name !== null))
  146. disabledButton=true;
  147.  
  148. var lock=document.getElementById('wmetts_lock');
  149. if (lock)
  150. {
  151.  
  152. if (disabledButton)
  153. {
  154. lock.setAttribute('style','color:red;');
  155. lock.setAttribute('class', "fa fa-lock");
  156. }
  157. else
  158. {
  159. lock.setAttribute('style','color:green;');
  160. lock.setAttribute('class', "fa fa-unlock");
  161. }
  162. }
  163.  
  164. document.getElementById('wmettsStreet').disabled = disabledButton;
  165. document.getElementById('wmettsTL').disabled = disabledButton;
  166. document.getElementById('wmettsTR').disabled = disabledButton;
  167. document.getElementById('wmettsKL').disabled = disabledButton;
  168. document.getElementById('wmettsKR').disabled = disabledButton;
  169. document.getElementById('wmettsEL').disabled = disabledButton;
  170. document.getElementById('wmettsER').disabled = disabledButton;
  171. document.getElementById('wmettsRND').disabled = disabledButton;
  172. }
  173. }
  174. }
  175.  
  176. function playTTS()
  177. {
  178. if (Waze.selectionManager.selectedItems.length != 1)
  179. {
  180. alert('Выберите только один сегмент');
  181. return;
  182. }
  183.  
  184. var street = Waze.model.streets.get(Waze.selectionManager.selectedItems[0].model.attributes.primaryStreetID);
  185. if (typeof street !== "undefined")
  186. {
  187. if (street.name === null)
  188. {
  189. alert('Безымянный сегмент');
  190. return;
  191. }
  192. }
  193. else
  194. {
  195. console.log('WME_TTS undefined street');
  196. return;
  197. }
  198.  
  199. var preText = '';
  200. switch (this.id)
  201. {
  202. case 'wmettsStreet':
  203. preText = ' ';
  204. break;
  205. case 'wmettsTL':
  206. preText = 'Поверните налево на ';
  207. break;
  208. case 'wmettsTR':
  209. preText = 'Поверните направо на ';
  210. break;
  211. case 'wmettsKL':
  212. preText = 'Держитесь левее на ';
  213. break;
  214. case 'wmettsKR':
  215. preText = 'Держитесь правее на ';
  216. break;
  217. case 'wmettsEL':
  218. preText = 'Съезд слева на ';
  219. break;
  220. case 'wmettsER':
  221. preText = 'Съезд справа на ';
  222. break;
  223. case 'wmettsRND':
  224. preText = 'На кольце первый съезд на ';
  225. break;
  226. default:
  227. alert("Unknown error");
  228. }
  229.  
  230. if (preText !== '')
  231. {
  232. 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();
  233. }
  234. }
  235.  
  236. function getElementsByClassName(classname, node) {
  237. if(!node)
  238. node = document.getElementsByTagName("body")[0];
  239. var a = [];
  240. var re = new RegExp('\\b' + classname + '\\b');
  241. var els = node.getElementsByTagName("*");
  242. for (var i=0,j=els.length; i<j; i++)
  243. if (re.test(els[i].className)) a.push(els[i]);
  244. return a;
  245. }
  246.  
  247.  
  248. TTStest_bootstrap();
  249.  
  250.  
  251.  
  252. // ------------------------------
  253. function playTTSHand()
  254. {
  255. if ((typeof arguments[0]) === "object")
  256. {
  257. switch(arguments[0].type)
  258. {
  259. case 'click': // click button
  260. {
  261. var e=document.getElementById(arguments[0].id);
  262. if(e)
  263. e.click();
  264.  
  265. break;
  266. }
  267. }
  268. }
  269. }
  270.  
  271. function TTStest_initBindKey()
  272. {
  273. var Config =[
  274. {handler: 'wmettsStreet', title: 'Имя сегмента', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsStreet'}},
  275. {handler: 'wmettsTL', title: 'Поверните налево на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTL'}},
  276. {handler: 'wmettsTR', title: 'Поверните направо на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsTR'}},
  277. {handler: 'wmettsKL', title: 'Держитесь левее на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKL'}},
  278. {handler: 'wmettsKR', title: 'Держитесь правее на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsKR'}},
  279. {handler: 'wmettsEL', title: 'Съезд слева на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsEL'}},
  280. {handler: 'wmettsER', title: 'Съезд справа на', func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsER'}},
  281. {handler: 'wmettsRND', title: 'На кольце первый съезд на',func:playTTSHand, key:-1, arg:{type:'click',id:'wmettsRND'}},
  282. ];
  283.  
  284. for(var i=0; i < Config.length; ++i)
  285. {
  286. WMEKSRegisterKeyboardShortcut('WME TTS test', 'WME TTS test', Config[i].handler, Config[i].title, Config[i].func, Config[i].key, Config[i].arg);
  287. }
  288.  
  289. WMEKSLoadKeyboardShortcuts('WME TTS test');
  290.  
  291. window.addEventListener("beforeunload", function() {
  292. WMEKSSaveKeyboardShortcuts('WME TTS test');
  293. }, false);
  294. }
  295.  
  296. // from: https://greasyfork.org/ru/scripts/16071-wme-keyboard-shortcuts (modify)
  297. /*
  298. when adding shortcuts each shortcut will need a uniuque name
  299. the command to add links is WMERegisterKeyboardShortcut(ScriptName, ShortcutsHeader, NewShortcut, ShortcutDescription, FunctionToCall, ShortcutKeysObj) {
  300. ScriptName: This is the name of your script used to track all of your shortcuts on load and save.
  301. ScriptName: replace 'WMEAwesome' with your scripts name such as 'SomeOtherScript'
  302. ShortcutsHeader: this is the header that will show up in the keyboard editor
  303. NewShortcut: This is the name of the shortcut and needs to be uniuque from all of the other shortcuts, from other scripts, and WME
  304. ShortcutDescription: This wil show up as the text next to your shortcut
  305. FunctionToCall: this is the name of your function that will be called when the keyboard shortcut is presses
  306. ShortcutKeysObj: the is the object representing the keys watched set this to '-1' to let the users specify their own shortcuts.
  307. 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'
  308. */
  309. 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)}
  310. /* ********************************************************** */