{"name":"Select to Read","key":"selecttoread","version":"1.0.0","instructions":"Select the text to be read aloud, and press the button.","showatto":"1","showplayers":"0","requirecss":"","requirejs":"","shim":"","defaults":"format=\"text\",language=\"English(US)|English(GB)|English(AU)|English(In)|English(Welsh)|Danish|Dutch|French(FR)|French(CA)|German|Icelandic|Italian|Japanese|Korean|Norwegian|Polish|Portugese(BR)|Portugese(PT)|Romanian|Russian|Spanish(ES)|Spanish(US)|Swedish|Turkish|Welsh\",\nspeaker=\"Male|Female\"","amd":"1","body":"<button id=\"@@AUTOID@@_button\" type=\"button\" style=\"width: 100%; max-width: 350px\">Select Text and Tap Here</button>\n<audio id=\"@@AUTOID@@_audioplayer\" class=\"nomediaplugin nopoodll\" crossorigin=\"anonymous\" controls=\"true\" style=\"width: 100%; max-width: 350px\"></audio>\n<!-- @@CLOUDPOODLLTOKEN@@ -->\n","bodyend":"","script":"//FUNCTION fetch polly url\n var fetch_polly_url = function(speaktext, voice, callback) {\n\n                //The REST API we are calling\n                var functionname = 'local_cpapi_fetch_polly_url';\n\n                //fetch the Posturl. We need this.\n                //set up our ajax request\n                var xhr = new XMLHttpRequest();\n                var that = this;\n\n                //set up our handler for the response\n                xhr.onreadystatechange = function (e) {\n                    if (this.readyState === 4) {\n                        if (xhr.status == 200) {\n\n                            //get a yes or forgetit or tryagain\n                            var payload = xhr.responseText;\n                            var payloadobject = JSON.parse(payload);\n                            if (payloadobject) {\n                                //returnCode > 0  indicates an error\n                                if (payloadobject.returnCode > 0) {\n                                    console.log(payloadobject.returnMessage);\n                                    return false;\n                                    //if all good, then lets do the embed\n                                } else if (payloadobject.returnCode === 0){\n                                    var pollyurl = payloadobject.returnMessage;\n                                    callback(pollyurl);\n                                } else {\n                                    console.log('Polly Signed URL Request failed:');\n                                    console.log(payloadobject);\n                                }\n                            } else {\n                                console.log('Polly Signed URL Request something bad happened');\n                            }\n                        } else {\n                            console.log('Polly Signed URL Request Not 200 response:' + xhr.status);\n                        }\n                    }\n                };\n\n                //make our request\n                var xhrparams = \"wstoken=\" + @@CLOUDPOODLLTOKEN@@\n                + \"&wsfunction=\" + functionname\n                + \"&moodlewsrestformat=\" + 'json'\n                + \"&text=\" + encodeURIComponent(speaktext)\n                + '&texttype=text'\n                + '&voice=' + voice\n                + '&appid=' + 'filter_poodll'\n                + '&owner=poodll'\n                + '&region=useast1';\n\n                var serverurl = 'https://cloud.poodll.com' + \"/webservice/rest/server.php\";\n                xhr.open(\"POST\", serverurl, true);\n                xhr.setRequestHeader(\"Cache-Control\", \"no-cache\");\n                xhr.setRequestHeader(\"Content-Type\", \"application/x-www-form-urlencoded\");\n                xhr.send(xhrparams);\n        };\n\n\nvar fetch_voice=function(language,mf){\n  switch(language){\n      case \"English(US)\": voice = mf=='Male'?'Joey':'Kendra';break;\n      case \"English(GB)\": voice = mf=='Male'?'Brian':'Amy';break;\n      case \"English(AU)\": voice = mf=='Male'?'Russell':'Nicole';break;\n      case \"English(IN)\": voice = mf=='Male'?'Aditi':'Raveena';break;\n      case \"English(WELSH)\": voice = mf=='Male'? 'Geraint':'Geraint';break;\n      case \"Danish\": voice = mf=='Male'?'Mads':'Naja';break;\n      case \"Dutch\": voice = mf=='Male'?'Ruben':'Lotte';break;\n      case \"French(FR)\": voice = mf=='Male'?'Mathieu':'Celine';break;\n      case \"French(CA)\": voice = mf=='Male'?'Chantal':'Chantal';break;\n      case \"German\": voice = mf=='Male'?'Hans':'Marlene';break;\n      case \"Icelandic\": voice = mf=='Male'?'Karl':'Dora';break;\n      case \"Italian\": voice = mf=='Male'?'Carla':'Giorgio';break;\n      case \"Japanese\": voice = mf=='Male'?'Takumi':'Mizuki';break;\n      case \"Korean\": voice = mf=='Male'?'Seoyan':'Seoyan';break;\n      case \"Norwegian\": voice = mf=='Male'?'Liv':'Liv';break;\n      case \"Polish\": voice = mf=='Male'?'Jacek':'Ewa';break;\n      case \"Portugese(BR)\": voice = mf=='Male'?'Ricardo':'Vitoria';break;\n      case \"Portugese(PT)\": voice = mf=='Male'?'Cristiano':'Ines';break;\n      case \"Romanian\": voice = mf=='Male'?'Carmen':'Carmen';break;\n      case \"Russian\": voice = mf=='Male'?'Maxim':'Tatyana';break;\n      case \"Spanish(ES)\": voice = mf=='Male'?'Enrique':'Conchita';break;\n      case \"Spanish(US)\": voice = mf=='Male'?'Miguel':'Penelope';break;\n      case \"Swedish\": voice = mf=='Male'?'Astrid':'Astrid';break;\n      case \"Turkish\": voice = mf=='Male'?'Filiz':'Filiz';break;\n      case \"Welsh\": voice = mf=='Male'?'Gwyneth':'Gwyneth';break;\n      default: voice = mf=='Male'?'Brian':'Amy';\n}\nreturn voice;\n}\n\nvar  getSelectionText = function() {\n    var text = \"\";\n    var activeEl = document.activeElement;\n    var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;\n    if (\n      (activeElTagName == \"textarea\") || (activeElTagName == \"input\" &&\n      /^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&\n      (typeof activeEl.selectionStart == \"number\")\n    ) {\n        text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);\n    } else if (window.getSelection) {\n        text = window.getSelection().toString();\n    }\n    return text;\n};\n\n//On click load text into the player and play it\n$('#' + @@AUTOID@@ + '_button').on('click',function(){\n   var usetext = getSelectionText();\n   //no super long readings or empty ones\n   if(!usetext || usetext.trim()=='') return;\n    if(usetext.length > 1500) return;\n\n   var theplayer = $('#' + @@AUTOID@@ + '_audioplayer');\n   var voice = fetch_voice(@@language@@,@@speaker@@);\n   fetch_polly_url(usetext,voice,function(audiourl){\n         theplayer.attr('src',audiourl);\n         theplayer[0].play();\n     });\n});\n\n\n\n","style":"","dataset":"","datasetvars":"","alternate":"","alternateend":""}