Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var rv_script = document.createElement('script');
- rv_script.setAttribute('src', 'https://code.responsivevoice.org/responsivevoice.js');
- document.head.appendChild(rv_script);
- function rv_speak_line_info(line) {
- if (line.startsWith('<')) {
- var el = $(line);
- if (el.hasClass('quotelink')) {
- var postNo = el.text().replace('>>', '');
- if (postNo.indexOf('OP') > 1) {
- postNo = 'opa';
- }
- return ['do posta ' + postNo, {pitch: 1}];
- }
- if (el.hasClass('postlink')) {
- var link = el.text().split('://')[1].split('/')[0];
- return ['link do strony ' + link, {pitch: 1}];
- }
- if (el.hasClass('quote')) {
- var text = el.text().replace('>', '');
- return [text, {pitch: 1.5}];
- }
- if (el[0].tagName == 'IMG') {
- return ['obrazek', {pitch: 1}];
- }
- return [text];
- }
- else {
- return [line, {pitch: 1}];
- }
- }
- function rv_text_normalize(text) {
- return text.replace(/<(?:.|\n)*?>/gm, '').replace('>', '');
- }
- function rv_rec_speak(lines, voice) {
- if (lines.length == 0) {
- return;
- }
- var line = lines[0];
- var lineInfo = rv_speak_line_info(line);
- var text = rv_text_normalize(lineInfo[0]);
- var settings = lineInfo[1];
- if (text.length == 0) {
- rv_rec_speak(lines.slice(1, lines.length));
- }
- var params = Object.assign({}, {
- onend: function() {
- rv_rec_speak(lines.slice(1, lines.length), voice);
- }
- }, settings);
- responsiveVoice.speak(text, voice, params);
- }
- function rv_prepare(content, voice) {
- var lines = content.split('<br>');
- var newLines = [];
- for (var i = 0; i < lines.length; i++) {
- if (lines[i].length > 1) {
- newLines.push(lines[i]);
- }
- }
- rv_rec_speak(newLines.slice(0, newLines.length - 1), voice);
- }
- $('.post').each(function() {
- $(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>');
- });
- $('a[data-rv]').click(function(e) {
- e.preventDefault();
- var rv_content = $(this).parents('.post').find('.postMessage').html();
- var rv_voice = ($(this).attr('data-rv') == 'male') ? 'Polish Male' : 'Polish Female';
- rv_prepare(rv_content, rv_voice);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement