Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1.  
  2.  
  3. var rv_script = document.createElement('script');
  4. rv_script.setAttribute('src', 'https://code.responsivevoice.org/responsivevoice.js');
  5. document.head.appendChild(rv_script);
  6.  
  7. function rv_speak_line_info(line) {
  8. if (line.startsWith('<')) {
  9. var el = $(line);
  10.  
  11. if (el.hasClass('quotelink')) {
  12. var postNo = el.text().replace('>>', '');
  13.  
  14. if (postNo.indexOf('OP') > 1) {
  15. postNo = 'opa';
  16. }
  17.  
  18. return ['do posta ' + postNo, {pitch: 1}];
  19. }
  20.  
  21. if (el.hasClass('postlink')) {
  22. var link = el.text().split('://')[1].split('/')[0];
  23. return ['link do strony ' + link, {pitch: 1}];
  24. }
  25.  
  26. if (el.hasClass('quote')) {
  27. var text = el.text().replace('>', '');
  28.  
  29. return [text, {pitch: 1.5}];
  30. }
  31.  
  32. if (el[0].tagName == 'IMG') {
  33. return ['obrazek', {pitch: 1}];
  34. }
  35.  
  36. return [text];
  37. }
  38. else {
  39. return [line, {pitch: 1}];
  40. }
  41. }
  42.  
  43. function rv_text_normalize(text) {
  44. return text.replace(/<(?:.|\n)*?>/gm, '').replace('&gt;', '');
  45. }
  46.  
  47. function rv_rec_speak(lines, voice) {
  48. if (lines.length == 0) {
  49. return;
  50. }
  51.  
  52. var line = lines[0];
  53. var lineInfo = rv_speak_line_info(line);
  54. var text = rv_text_normalize(lineInfo[0]);
  55. var settings = lineInfo[1];
  56.  
  57. if (text.length == 0) {
  58. rv_rec_speak(lines.slice(1, lines.length));
  59. }
  60.  
  61. var params = Object.assign({}, {
  62. onend: function() {
  63. rv_rec_speak(lines.slice(1, lines.length), voice);
  64. }
  65. }, settings);
  66.  
  67. responsiveVoice.speak(text, voice, params);
  68. }
  69.  
  70. function rv_prepare(content, voice) {
  71. var lines = content.split('<br>');
  72. var newLines = [];
  73.  
  74. for (var i = 0; i < lines.length; i++) {
  75. if (lines[i].length > 1) {
  76. newLines.push(lines[i]);
  77. }
  78. }
  79.  
  80. rv_rec_speak(newLines.slice(0, newLines.length - 1), voice);
  81. }
  82.  
  83. $('.post').each(function() {
  84. $(this).find('.postInfo').first().append('<span> <a href="#" data-rv="male">[Czytaj męskim głosem]</a> <a href="#" data-rv="female">[Czytaj damskim głosem]</a></span>');
  85. });
  86.  
  87. $('a[data-rv]').click(function(e) {
  88. e.preventDefault();
  89. var rv_content = $(this).parents('.post').find('.postMessage').html();
  90. var rv_voice = ($(this).attr('data-rv') == 'male') ? 'Polish Male' : 'Polish Female';
  91.  
  92. rv_prepare(rv_content, rv_voice);
  93. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement