[Help] looking for help with EventListener

hi,

i’m trying to make a script that first loads all id’s of the subjects that are up for review, and then updates the subjects that are left as you go through your reviews.

the first time the subjects get loaded with the api, but since that takes a while, i want it to be done only once. so instead of fetching all the subjects that are for review again, when a review gets done i want to get the id of that subject and directly remove it from the list of saved id’s. at the moment i have this code:

  window.addEventListener('didChangeSRS', (event) => {
       dothething();
  });

i would like to use something like

let id = event.details.subject_id;

but that is not available. can someone tell me how i can get the subject id?

3 Likes

This is probably not the intended usage of it but you could use another script called WK Item Info Injector to get that information easily:

It’s meant for adding extra sections to the item info (for example in reviews) but you can also just get the item ID from it. Here is an example of how that would work (for setup and further documentation see the thread I linked to):

window.wkItemInfo.notify({id} => doSomethingWith(id))

Basically you have to first ‘require’ the script at the top of your own script and then you can use it in your code (the notify function is what you want to not invoke the info section injection). Your code could look like this (although this way might not be the prettiest):

window.addEventListener('didCompleteSubject', 
    (event) => window.wkItemInfo.notify({id} => {
         dothething(event, id); // can use both id and event here
    })
);

@Sinyaven Tell me if I did something wrong :sweat_smile:

Edit: Changed event as suggested by Kumirei

1 Like

Use didCompleteSubject instead

3 Likes

i assume otherwise it also gets triggered when the review was entered wrong and the srs becomes lower?

1 Like

thanks, that helped a lot! :slight_smile:

1 Like

Well yes, but whether that matters depends on your application. The reason why didCompleteSubject is more suitable is because it contains the subject data, including ID, in its details

Your code snippet registers a new action every time didCompleteSubject is fired, so the number of registered actions increases over time, which will not have the desired effect. Furthermore, the notify action is supposed to only trigger when the targeted item info section gets displayed, so if the user does not open the item info section, it should do nothing.

As @Kumirei suggested, it would probably be better to just use the event arguments from didCompleteSubject:

window.addEventListener('didCompleteSubject', e => dothething(e.detail.subjectWithStats.subject.id));

(e.detail.subjectWithStats.subject also contains other information about the completed item, if needed)

And if you need the current item’s id at a different time than when didCompleteSubject gets triggered, you can use

parseInt(document.querySelector(`.quiz-input__question-type-container`).dataset.subjectId)
3 Likes