I am kind of new to javascript and building websites, I program c# most of the times.
I am trying to build something and I need to use google translate api, the problem that is cost money so I prefer use Free API so I found this.
https://ctrlq.org/code/19909-google-translate-api
so I changed it a bit and tried alone, because I wasn't sure what e type ment.
so this is my code:
function doGet(text) {
var sourceText = text;
var translatedText = LanguageApp.translate('en', 'iw', sourceText);
var urllog = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
+ "en" + "&tl=" + "iw" + "&dt=t&q=" + encodeURI(text);
var result = JSON.parse(UrlFetchApp.fetch(urllog).getContentText());
translatedText = result[0][0][0];
console.log(translatedText);
}
so the url is downloading me a text file called "f.txt" that include the translate code the problem is that I doesnt want it to download File,
I just need the translate inside the txt file its gives me,
also the problem is I am not sure how to get that info inside a javascript variable, And I doesnt want it to give me that file as well..
So how Can I read it?
how can I use the file without download it, and How can I push it to a string variable?
And How I can cancel the download and get only the translate?
THANKS!
By the way
and if anyone know the function doGet(e) that I showed on the link, what is "e"? what does the function wants?
I know I'm a year late but I came to same problem and fixed it using PHP. I have created this simple PHP function:
function translate($text, $from, $to) {
if($text == null)
echo "Please enter a text to translate.";
if($from == null)
$from = "auto";
if($to == null)
$to = "en";
$NEW_TEXT = preg_replace('/\s+/', '+', $text);
$API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" . $from . "&tl=" . $to . "&dt=t&q=" . $NEW_TEXT;
$OUTPUT = get_remote_data($API_URL);
$json = json_decode($OUTPUT, true); // decode the JSON into an associative array
$TRANSLATED_OUTPUT = $json[0][0][0];
echo $TRANSLATED_OUTPUT;
}
Example usage (English to Spanish):
translate("Hello", "en", "es"); //Output: Hola
/*
sourceLanguage: the 2-3 letter language code of the source language (English = "en")
targetLanguage: the 2-3 letter language code of the target language (Hebrew is "iw")
text: the text to translate
callback: the function to call once the request finishes*
* Javascript is much different from C# in that it is an asynchronous language, which
means it works on a system of events, where anything may happen at any time
(which makes sense when dealing with things on the web like sending requests to a
server). Because of this, Javascript allows you to pass entire
functions as parameters to other functions (called callbacks) that trigger when some
time-based event triggers. In this case, as seen below,
we use our callback function when the request to google translate finishes.
*/
const translate = function(sourceLanguage,targetLanguage,text,callback) {
// make a new HTTP request
const request = new XMLHttpRequest();
/*
when the request finishes, call the specified callback function with the
response data
*/
request.onload = function() {
// using JSON.parse to turn response text into a JSON object
callback(JSON.parse(request.responseText));
}
/*
set up HTTP GET request to translate.googleapis.com with the specified language
and translation text parameters
*/
request.open(
"GET",
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=" +
sourceLanguage + "&tl=" + targetLanguage + "&dt=t&q=" + text,
true
);
// send the request
request.send();
}
/*
translate "This shouldn't download anything" from English to Hebrew
(when the request finishes, it will follow request.onload (specified above) and
call the anonymous
function we use below with the request response text)
*/
translate("en","iw","This shouldn't download anything!",function(translation) {
// output google's JSON object with the translation to the console
console.log(translation);
});
Related
So now that Yahoo shut down query.yahooapis.com as the following message indicates, does anyone know of a free replacement?
"Important EOL Notice: As of Thursday, Jan. 3, 2019, the YQL service
at query.yahooapis.com will be retired. This will impact users of
datatables.org as well as developers who creates features using this
YQL service. To continue using our free Yahoo Weather APIs, use
https://weather-ydn-yql.media.yahoo.com/forecastrss as your new API
endpoint. Contact yahoo-weather-ydn-api#oath.com for credentials to
onboard to this free Yahoo Weather API service. Other YQL based
services that use query.yahooapis.com will no longer operate."
Need to replace "//query.yahooapis.com/v1/public/yql?q=" for my rss scraper to work.
function yql(a, b) {
return (
"**//query.yahooapis.com/v1/public/yql?q=**" +
encodeURIComponent(
"select * from " +
b +
' where url="' +
a +
'" limit ' +
params.feedcount
) +
"&format=json"
);
}
I found this and it worked great for me.
https://api.rss2json.com
There's a free layer and it's way more straightforward than YQL was for RSS to JSONP conversion.
I build CloudQuery which is able to turn most websites to API and it has a simple to use web interface to create the API. And it is open sourced on github
Here is a possible solution for you.
a) You need some kind of proxy to allow loading content with ajax from different sources. It is advisable to whitelist and add CORS Headers etc. to prevent exploiting your proxy. Create for example a php-file on one of your servers with this functionality:
$valid_url_regex = '/.*(rss|feed|atom).*/';
$url = $_GET['url'];
if ( !preg_match( $valid_url_regex, $url ) ) exit;
$feeds = file_get_contents($url);
//this is some workaround to get special namespaces into the json
$feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
$feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
$feeds = str_replace("<media:content ","<mediaContent ",$feeds);
$feeds = str_replace("</media:content>","</mediaContent>",$feeds);
$simpleXml = simplexml_load_string($feeds, "SimpleXMLElement", LIBXML_NOCDATA);//this is for CDATA
$json = json_encode($simpleXml);
header("Access-Control-Allow-Origin: http://yourdomainnamehere");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
print $json;
b) Perform an async ajax-call to the proxy-script and handle the data:
function loadRss(url)
{
$.ajax({
url: 'yourserverurl/rssproxy.php?url='+url,
type: 'GET',
success: function(response) {
handleResponse(JSON.parse(response));
}
});
}
function handleResponse(response) {
var entries;
if(response.entry) //ATOM
entries = response.entry;
else if(response.channel.item) //RSS 1/2
entries = response.channel.item;
var feedTitle="";
if(response.title)
feedTitle = response.title;
else if(response.channel.title)
feedTitle = response.channel.title;
//iterate all news entries
$.each(entries, function (i, e) {
console.log("Entry #"+i);
console.log(e);
//access the data as necessary like e.content, e.summary, e.contentEncoded etc....
}
);
}
I changed my google rss api a few years ago to YQL, now i had to do it again today, took a few hours, but this time you wont be dependent on some 3rd-party vendor and hopefully you can use your new reader code until rss vanishes in preference of mankind for the famous filter bubble ;)
Above code is just a hint and of course you will have to invest some time if you want to map the response to the generalized YQL Structure. I didnt go that way and accessed the properties of the response as necessary.
I am writing an Angular 2 app (built with angular cli), and trying to use AWS Polly text-to-speech API.
According to the API you can request audio output as well as "Speech Marks" which can describe word timing, visemes, etc. The audio is delivered as "mp3" format, and the speech marks as "application/x-json-stream", which I understand as a "new line" delimited JSON. It cannot be parsed with JSON.parse() due to the new lines. I have yet been unable to read/parse this data. I have looked at several libs that are for "json streaming" but they are all built for node.js and won't work with Angular 2. My code is as follows...
onClick() {
AWS.config.region = 'us-west-2';
AWS.config.accessKeyId = 'xxxxx';
AWS.config.secretAccessKey = 'yyyyy';
let polly = new AWS.Polly();
var params = {
OutputFormat: 'json',
Text: 'Hello world',
VoiceId: 'Joanna',
SpeechMarkTypes:['viseme']
};
polly.synthesizeSpeech(params, (err, data) => {
if (err) {
console.log(err, err.stack);
} else {
var uInt8Array = new Uint8Array(data.AudioStream);
var arrayBuffer = uInt8Array.buffer;
var blob = new Blob([arrayBuffer]);
var url = URL.createObjectURL(blob);
this.audio.src = url;
this.audio.play(); // works fine
// speech marks info displays "application/x-json-stream"
console.log(data.ContentType);
}
});
Strangely enough Chrome browser knows how to read this data and displays it in the response.
Any help would be greatly appreciated.
I had the same problem. I saved the file so I could then read it line by line, accessing the JSON objects when I need to highlight words being read. Mind you this is probably not the most effective way, but an easy way to move on and get working on the fun stuff.
I am trying out different ways to work with Polly, will update answer if I find a better way
You can do it with:
https://www.npmjs.com/package/ndjson-parse
That worked for me.
But I can't play audio, I tried your code it says
DOMException: Failed to load because no supported source was found.
I am working on a WP8 application, containing the WebBrowser control in which I open a html page, containing javascript. The javascript contains the following function:
function send(data) {
windows.external.notify(data);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'getresponse', false);
xhr.send(null);
var result = xhr.responseText;
if (result) {
return JSON.parse(result);
}
}
Basically this function calls the native C# side of the app, where I run some functions and I need to be able to return some data from the native side to the send function. I wanted to use an XMLHttpRequest for this, where my idea was to "intercept" the request url (in this case 'getresponse') and return the data I want by including it in the response.
Is this please possible on Windows Phone 8 using the WebBrowser control?
Once again, all I need to do is this:
Have a javascript function (in this case called "send") which connects to the native app (using windows.external.notify) and pass data back to this "send" function so that it can return it (and so that other JS function can use it).
Is this please possible? If not using the XMLHttpRequest, maybe using another technique?
Thank you all for your help!
You are looking for InvokeScript.
If you have full control over the page that is displayed inside the WebBrowser (e.g. the server is your's), you can define the JS-function to be called:
webBrowser.InvokeScript("yourJSFunction", "param1", "param2");
If you display a website from a foreign webserver you can inject JS like this (this uses jQuery):
webBrowser.InvokeScript("eval", "window.youInjectedFunction = function() {" +
"window.external.notify('and_notify_back');" +
"}; " +
"window.readyStateCheckInterval = setInterval(function() {" +
"window.external.notify('timer');" +
"if (document.readyState === 'complete') {" +
"clearInterval(window.readyStateCheckInterval);window.yourInjectedFuntion();" +
"}}, 100);" +
"");
I used the timer, as you can not be certain if the InvokeScript is called after the page is completely loaded.
If you can control the source, you should definitely go for option 1.
I have a small HTML5 (using jQuery mobile) web app that caches its files to use them offline, however some parts don't seem to work once it's offline.
The files are cached OK (I can see them in the web inspector) but when I try to visit a page that uses jQuery to load a JSON file it doesn't load.
I tried creating an empty function to load the JSON files (when the index page is loaded) to see if that would help but it doesn't seem to make a difference.
Here's the function that doesn't want to work offline.
My question is: should it work offline or am I missing something?
// events page listing start
function listEvents(data){
$.getJSON('/files/events.json', {type: "json"},function (data) {
var output = '';
for (i in data)
{
var headline = data[i].headline;
var excerpt = data[i].rawtext;
output += '<div id="eventsList">';
output += '<h3>'+headline+'</h3>';
output += '<p>'+ excerpt +'<p>';
output += '</div>';
}
$("#eventsPageList").html(output).trigger("create");
});
}
I'm not really sure, if i'm right about this. But i think an ajax request will always fail when you are offline. It won't use the locally cached file. What you should try is, to cache the data in localStorage. When the ajax request fails, fallback to localStorage.
OK here's a version which seems to work, I read the json file and place it in localstorage then use the localstorage in the listEvents function.
When the page loads I call this function to add the json to localstorage
function cacheJson(data){
$.getJSON('/files/events.json',
{type: "json", cache: true},function (data) {
localStorage['events'] = JSON.stringify(data); });
}
Then this function to output the json (from localstorage) to the page, with an if else incase the localstorage doesn't contain the json.
function listEvents(data){
if (localStorage.getItem("events") === null) {
var output = '';
output += 'Sorry we have an error';
$("#eventsPageList").html(output).trigger("create");
}
else {
data = JSON.parse(localStorage['events']);
var output = '';
for (i in data)
{
var headline = data[i].headline;
var excerpt = data[i].rawtext;
output += '<div id="eventsList">';
output += '<h3>'+headline+'</h3>';
output += '<p>'+ excerpt +'<p>';
output += '</div>';
}
$("#eventsPageList").html(output).trigger("create");
}
}
It seems to work ok but am I missing something that could cause issues?
Is there a more efficient way of doing this?
I'm trying to get the Yahoo Weather with JavaScript. I originally made a proxy, but found that clumsy.
So can get the JSON response from http://weather.yahooapis.com/forecastjson?w=9807, and I know that the script tag can avoid the same-domain restrictions, but I'm getting a syntax error.
Yahoo's JSON response isn't padded; I've got the callback working but the browser isn't interpreting the JSON properly.
I've seen many examples like How to read yahoo weather JSON data with Jquery ajax but it's so weird because all those give me the cross-domain error.
Can anyone help me with this? Cross domain, yahoo weather, without special servers or YQL or anything like that. Something that just works out of the box.
If you're expecting JSON-P then you need to add a callback function name to the query. With jQuery, this is always ?. jQuery will substitute it with a randomly generated function name:
var query = escape('select item from weather.forecast where location="CAXX0518"'),
url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=?";
$.getJSON(url, function(data) {
console.log( data );
});
If you want to use yql, this is the link:
http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json
When you call it just pass that as the parameter in your jquery. So, in other using STeve's code you can simply replace the url passed into the getJSON function call with the yql link and of course replace the zip code you want to use for the location. So, here's the code:
$(document).ready(DocReady);
function DocReady()
{
var Result = $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20location%3D%2233015%22&format=json", "",
function (data)
{
$("body").append("Sunrise: " + data.query.results.channel.astronomy.sunrise + "<br />");
$("body").append("SuntSet: " + data.query.results.channel.astronomy.sunset + "<br />");
});
}
Here is the section you need to replace to get the proper location:
Enter zip code between both %22's
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20location%3D%22
33333
%22&format=json
Let me know if you have any questions.
Here is some code
$(document).ready(DocReady);
function DocReady()
{
jQuery.support.cors = true;
var Result = $.getJSON("http://weather.yahooapis.com/forecastjson?w=9807", "",
function (data)
{
$("body").append("Sunrise: " + data.astronomy.sunrise + "<br />");
$("body").append("SuntSet: " + data.astronomy.sunset + "<br />");
});
}