[Solved] Trying to create a userscript to show context sentences (not included in "currentItem")

By looking at other userscripts, I see code like

currentItem = $.jStorage.get(“currentItem”);
and then being used in ways sych as
currentItem.en for English
currentItem.aud for audio
currentItem.voc for the current vocab word
What I want to know is, is there documentation for the different types of data in currentItem?
More specifically, I want the context sentence of the current vocab in reviews.
Thanks

EDIT:
So since currentItem doesn’t contain the context sentences, I’m using autohotkey to open every individual vocab page and compile a
database of all the vocab words and their context sentences to include
in the userscript.

EDIT2:
https://greasyfork.org/en/scripts/26868-wk-context-sentences/code
Thanks for your help, especially @rfindley for the code to grab sentences. Here’s my updated script.

create your own documentation! I mean, just look at the object in the console and you’ll see what properties are in a currentItem.

Btw, currentItem doesn’t seem to have the context sentences. You might have to either query the API or scrape it somehow from the info page after you get a wrong answer.

Context sentences aren’t available anywhere in the API.  They can only be extracted from individual item pages.
When an item is loaded, you could do an ajax request to fetch the corresponding page, then figure out how to extract the context sentences.

There’s no documentation for ‘currentItem’, since it’s behind-the-scenes stuff in the WK code.  But you can simply examine its contents.

Open your Javascript console, and type:
  .jStorage.get('currentItem')<br>Then expand the returned result.<br><br>If you prefer to see it in JSON format, run this command instead:<br>&nbsp; JSON.stringify(.jStorage.get(‘currentItem’))
And you can paste the resulting string into a beautifier if you want better-looking results (e.g. http://jsbeautifier.org/)


Here’s a sample

{
  "id": 81,
  "kan": "北",
  "en": ["North"],
  "emph": "onyomi",
  "kun": ["きた"],
  "on": ["ほく"],
  "nanori": [],
  "srs": 4,
  "syn": [],
  "level": "3"
}



rfindley said... Context sentences aren't available anywhere in the API.  They can only be extracted from individual item pages.
When an item is loaded, you could do an ajax request to fetch the corresponding page, then figure out how to extract the context sentences.

There's no documentation for 'currentItem', since it's behind-the-scenes stuff in the WK code.  But you can simply examine its contents.

Open your Javascript console, and type:
  $.jStorage.get('currentItem')
Then expand the returned result.

If you prefer to see it in JSON format, run this command instead:
  JSON.stringify($.jStorage.get('currentItem'))
And you can paste the resulting string into a beautifier if you want better-looking results (e.g. http://jsbeautifier.org/)

Here's a sample
{
    "id": 81,
    "kan": "北",
    "en": ["North"],
    "emph": "onyomi",
    "kun": ["きた"],
    "on": ["ほく"],
    "nanori": [],
    "srs": 4,
    "syn": [],
    "level": "3"
}

 I just looked at the request over the network that's made after an incorrect answer and it refers to https://www.wanikani.com/json/vocabulary/2351
Wouldn't it be possible to copy/reverse engineer the headers from such a request to your own ajax function? Or would there be some sort of passcode that can't be replicated?
Mempo said... I just looked at the request over the network that's made after an incorrect answer and it refers to https://www.wanikani.com/json/vocabulary/2351
Wouldn't it be possible to copy/reverse engineer the headers from such a request to your own ajax function? Or would there be some sort of passcode that can't be replicated?
I'm not sure what that URL contains or does, since it gives me an error if I just paste it in the Location box.  Does it have the context sentences?

The 'id' field in the 'currentItem' corresponds to the '2351' in your sample URL above.  Then, you just need to determine the item type (rad/kan/voc).
rfindley said...
Mempo said... I just looked at the request over the network that's made after an incorrect answer and it refers to https://www.wanikani.com/json/vocabulary/2351
Wouldn't it be possible to copy/reverse engineer the headers from such a request to your own ajax function? Or would there be some sort of passcode that can't be replicated?
I'm not sure what that URL contains or does, since it gives me an error if I just paste it in the Location box.  Does it have the context sentences?

The 'id' field in the 'currentItem' corresponds to the '2351' in your sample URL above.  Then, you just need to determine the item type (rad/kan/voc).
The URL is how WK native code asks the server the information it needs to display on the info section below a wrong answer. I looked at the response it gets and it contains the context sentences.

Simply entering the URL in the address bar gives me nothing though, so there must be something in the GET headers that gives it safe passage. The problem is I don't know what it is.

This is the request:
GET /json/vocabulary/3557 HTTP/1.1
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
X-CSRF-Token: z77EYm2veuA4aDUFONpg+iAFV+0jkZmBi9/oYpVw60sL9aYQw1CT0akPtkVhb3Irgj+0yEJEGTkca0Xo6t4AoQ==
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.87 Chrome/49.0.2623.87 Safari/537.36
X-Requested-With: XMLHttpRequest
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: _wanikani_session=82e6b27ea93030bfa2e366a4ed53a220; __utmt=1; undefined_message_status=false;
__utma=7847860.2134758551.1481131625.1485525868.1485546131.93; __utmb=7847860.17.10.1485546131; __utmc=7847860; __utmz=7847860.1481131625.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
But I don't know enough about HTTP headers to decipher this and replicate it with a random vocab id.
Mempo said...
rfindley said...
Mempo said... 
The URL is how WK native code asks the server the information it needs to display on the info section below a wrong answer. I looked at the response it gets and it contains the context sentences.

Simply entering the URL in the address bar gives me nothing though, so there must be something in the GET headers that gives it safe passage. The problem is I don't know what it is.

This is the request:

But I don't know enough about HTTP headers to decipher this and replicate it with a random vocab id.
 Ah, got it. You just have to pass the cookie data

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -b  _wanikani_session=xxxhttps://www.wanikani.com/json/vocabulary/6229
works for me. Now it's just a matter of determining your personal cookie data. And translating that curl command to some javascript.
Mempo said... Ah, got it. You just have to pass the cookie data

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -b  _wanikani_session=xxx https://www.wanikani.com/json/vocabulary/6229
works for me. Now it's just a matter of determining your personal cookie data. And translating that curl command to some javascript.
You should probably log out and back in again, so someone doesn't hijack your session using the above session key.

re: query with cookies..
Assuming the OP is doing this in a userscript, the cookie is automatically attached to ajax requests, so he could just do a $.getJSON(....).

Could that person do my reviews ;p

Anyway, did that. Lessons were learned today. You probably shouldn’t have quoted the code either ^^

As a newbie, I didn’t know how to use console and had assumed the info was in some kind of API documentation. Thanks for explaining how to check for myself and confirming that currentItem does not contain the context sentence. I have no idea how to use ajax or cookies to get the context sentence. Instead, I was using autohotkey to open every individual vocab page and compile a database of all the vocab words and their context sentences to include in the userscript.
https://greasyfork.org/en/scripts/26868-wk-context-sentences/code
Here’s what I have so far, displaying the sentences for any vocab words in levels 21-30. I’m open to any suggestions to improve the coding (which is most likely unconventional due to my inexperience).

nelemnaru said... As a newbie, I didn't know how to use console and had assumed the info was in some kind of API documentation. Thanks for explaining how to check for myself and confirming that currentItem does not contain the context sentence. I have no idea how to use ajax or cookies to get the context sentence. Instead, I was using autohotkey to open every individual vocab page and compile a database of all the vocab words and their context sentences to include in the userscript.
https://greasyfork.org/en/scripts/26868-wk-context-sentences/code
Here's what I have so far, displaying the sentences for any vocab words in levels 21-30. I'm open to any suggestions to improve the coding (which is most likely unconventional due to my inexperience).
 Image result for its alive gif

That's a whole lotta work to hard-code all those sentences in the script itself.

But hey, my first scripts were not that great either ;p

The only problem would be when the team changes those sentences, you'd have to update them manually too.

Just experiment a lot if you're willing to create more.

If it's more of a one time thing, I'm willing to implement a solution with less manual work.

But you'd have to wait a few days for it.
Mempo said...That's a whole lotta work to hard-code all those sentences in the script itself.
But hey, my first scripts were not that great either ;p
The only problem would be when the team changes those sentences, you'd have to update them manually too.
Just experiment a lot if you're willing to create more.
If it's more of a one time thing, I'm willing to implement a solution with less manual work.
But you'd have to wait a few days for it.
 There's also the fact that the context sentences are copyrighted content, and posting them permanently on GreasyFork is probably not a good idea.
If I get a moment, I'll post some code for how to fetch the sentences via $.getJSON()

Interesting note that I haven’t noticed before. (perhaps partly due to my absence, which I’m trying to remedy). The context sentences show up under “Show All Information” during reviews, but not under either ‘Meaning’ or ‘Reading’ alone (before hitting show all).
I should think it would go under ‘Meaning’, since there isn’t any ‘Reading’ information encoded into it.

@nelemnaru,
This is rfindley from my test account.
Here's some code to fetch the context sentences from the server.

var current_item = $.jStorage.get('currentItem');

// Only vocab has context sentences.
if (current_item.voc !== undefined) {
    var url = '/json/vocabulary/' + current_item.id;

    // Grab the item info from WK server.  Process result when it arrives.
    $.getJSON(url, function(json) {
        // Extract the sentences from the item info.
        var context_sentences = json.sentences;
        console.log('Sentences for ' + current_item.voc + ':');

        // Output each sentence to the console.
        $.each(context_sentences, function(idx, sentence) {
            var japanese = sentence[0];
            var english = sentence[1];
            console.log('  J: ' + japanese);
            console.log('  E: ' + english);
        });
    });
}
rfindley said...
Mempo said...That's a whole lotta work to hard-code all those sentences in the script itself.
But hey, my first scripts were not that great either ;p
The only problem would be when the team changes those sentences, you'd have to update them manually too.
Just experiment a lot if you're willing to create more.
If it's more of a one time thing, I'm willing to implement a solution with less manual work.
But you'd have to wait a few days for it.
 There's also the fact that the context sentences are copyrighted content, and posting them permanently on GreasyFork is probably not a good idea.
If I get a moment, I'll post some code for how to fetch the sentences via $.getJSON()
Wow, I knew there had to be an easier way. Those are good points about the sentences being changed and copyrighted.
@rfindley Thank you so much for the sample code. It's beautiful.

(I decided against majoring in computer science and chose to pursue a teaching career in Japan. So it's probably better for me to spend more time studying Japanese than experimenting with scripts. It takes me hours of googling just to create simple lines of correct code.)
nelemnaru said...
rfindley said...
Mempo said...That's a whole lotta work to hard-code all those sentences in the script itself.
But hey, my first scripts were not that great either ;p
The only problem would be when the team changes those sentences, you'd have to update them manually too.
Just experiment a lot if you're willing to create more.
If it's more of a one time thing, I'm willing to implement a solution with less manual work.
But you'd have to wait a few days for it.
 There's also the fact that the context sentences are copyrighted content, and posting them permanently on GreasyFork is probably not a good idea.
If I get a moment, I'll post some code for how to fetch the sentences via $.getJSON()
Wow, I knew there had to be an easier way. Those are good points about the sentences being changed and copyrighted.
@rfindley Thank you so much for the sample code. It's beautiful.

(I decided against majoring in computer science and chose to pursue a teaching career in Japan. So it's probably better for me to spend more time studying Japanese than experimenting with scripts. It takes me hours of googling just to create simple lines of correct code.)
 Ow yea of course. If you don't already know how to program or plan on learning I'd advice against scripts too. Better to make a request thread and make it blow up with support ;p
Mempo said... Ow yea of course. If you don't already know how to program or plan on learning I'd advice against scripts too. Better to make a request thread and make it blow up with support ;p
Ironically, I was going to make a request, but then I found an unanswered thread from earlier this month requesting almost the same thing, so I decided to give it a try. I have basic knowledge of programming and enjoy messing around, but usually end up wasting too much time trying to get things to work.