I have just started playing with HTML requests in JavaScript yesterday, but I couldn’t find a way to make use of anything other than WaniKani API. I want to make use of JMDict or other similar things.
Jisho.org has an API that you can use, and it’s based on jmdict.
// We'll use a cross-origin proxy, since jisho.org doesn't have a CORS policy,
// which means browsers will block the query since it's not from the same location
// that our script is running.
var proxy = 'https://crossorigin.me/';
var word = '余計';
var url = proxy + 'http://jisho.org/api/v1/search/words?keyword=' + word;
// Run the query. When the requested document is loaded,
// the callback function "process_result()" will be called,
$.getJSON(url, process_result);
// Here's the callback function that receives the response from our query
function process_result(json) {
// Grab the first kanji orthography from the first search result.
var word = json.data[0].japanese[0].word;
// Grab the first reading from the first search result
var reading = json.data[0].japanese[0].reading;
// Grab the first meaning from the first sense of the word
var meaning = json.data[0].senses[0].english_definitions[0];
console.log(word+';'+reading+';'+meaning);
}
For $.getJSON(url, process_result), I get [object Object].
For $.getJSON(url, process_result).data, I get undefined.
For $.getJSON(url, process_result).data[0].japanese[0].word, I get
TypeError: undefined is not an object (evaluating ‘$.getJSON(url, process_result).data’)
For $.getJSON(url, process_result).word, I get undefined.
What are you using to run your javascript? Node.js? A browser?
The reason I ask: I noticed that the jisho.org API doesn’t have a CORS (i.e. cross-origin) policy, meaning browsers won’t allow the JSON to be accepted unless the script is running while on jisho.org.
EDIT: If you’re in a browser, you can get around this by using a cross-origin proxy, such as crossorigin.me.
$.getJSON() doesn’t return the JSON object. It only starts the query, and returns immediately.
When the query is complete, the callback function process_result() will be called, with the JSON object as the first parameter. Then you can process the JSON from inside the callback.
Yeah… Since $.getJSON() is an asynchronous function, there’s really no way to create a synchronous function like:
var result = fetchJishoInfo();
The closest you can get is to use Promises, but that’s not very intuitive:
code
```javascript
function fetchJishoInfo(word) {
return new Promise(function(resolve, reject){
// We'll use a cross-origin proxy, since jisho.org doesn't have a CORS policy,
// which means browsers will block the query since it's not from the same location
// that our script is running.
var proxy = 'https://crossorigin.me/';
var url = proxy + 'http://jisho.org/api/v1/search/words?keyword=' + word;
// Run the query. When the requested document is loaded,
// the callback function "process_result()" will be called,
$.getJSON(url)
.done(json => {
// Grab the first kanji orthography from the first search result.
var word = json.data[0].japanese[0].word;
// Grab the first reading from the first search result
var reading = json.data[0].japanese[0].reading;
// Grab the first meaning from the first sense of the word
var meaning = json.data[0].senses[0].english_definitions[0];
resolve(word+';'+reading+';'+meaning);
})
.fail(error => {
reject();
});
});
}
// Our list of comma-separated vocabulary.
var vocab = ‘余計,倒す,倒産,妨害,機械,災難’;
// Send out jisho.org requests for ALL of them simultaneously.
var array = vocab.split(‘,’);
Promise.all(array.map(fetchJishoInfo))
.then(values => {
// When all of them are fetched, their values are accessible here in the array ‘values’.
// In this case, we just join them all together with a ‘\n’ between each one, and output to console.
console.log(values.join(‘\n’));
});
But I don’t know how to host an html file, so if you want to use it, you’ll have to download it to your computer. Use this to kill Item Marker / Leeches.
Item Marker: replace all " \t" with ‘,’
Leeches via Stat site: replace all “Level” with ‘,’
Although it doesn’t seem to do anything on there. I know nothing about what you’re trying to do. If you want I can supply you with your own login and ftp access and you can upload your Japanese projects there @polv.
FYI, there are some problems when using the proxy. It occasionally returns an error about “service unavailable”, which means you’ll miss some of your vocab if you’re not paying attention.
Should I specifically host the no proxy version?
Also, I updated to the new one and it’s not working nearly as well. It’s not always displaying and requires many presses on the “show” button.