JS API discussion

Hi there! Anybody else using the JS API?

If anybody is interested, I could share more about my learnings.

E.g.: There is a bug in 2.18. I posted about it here: AnkiDroid Javascript API · ankidroid/Anki-Android Wiki · GitHub

It would be helpful if we could have a category for the JS API in this forum. Right now the only options are Help and Suggestions. This doesn’t fit in these.

1 Like

For my own project I wrote for the AnkiDroid JS API:

If you are interested in this, please write so here. Because as long as nobody is using this anyway, I will be pretty careless with breaking changes and probably forget to push my updates to GitHub.

I learned that localStorage can now be used. I tested it, it persists between cards. Good for storing settings.

Outdated Anki (NOT AnkiDroid!) Addon that makes the AnkiDroid JS API work in Anki Desktop. If I find time, I will fork it and update it to work again. Would be highly useful. https://ankiweb.net/shared/info/1490471827

Posting in the hope that somebody will find time earlier than me. :slight_smile:

Which parts are broken now? I use this addon myself but I haven’t tested each and every function it’s adding. It doesn’t implement every function the AnkiDroid JS API has either. I made my own fork to implement the ankiSearchCard function since I wanted to use it my own cards: GitHub - jhhr/AnkiJS-API: Anki JavaScript API to get cards informations in reviewer window

This still leaves the vast majority of functions to implement, basically all the ones that edit things.

Thank you for your answer! Well, it cannot work with the current v0.3 API because all API functions were just revamped to a different (async) return types and the plugin was not updated after that change.

I recall that already happened in v0.2. The addon itself works as before though. And you always needed two sets of code to either call the desktop JS API addon through pycmd or the AnkiDroid JS API.

For example, this is what I’m doing in my code which worked with v0.2 and v0.3 the same (haven’t tested this on AnkiMobile though)

// Function for handling either AnkiDroid JS API calls, which are async or desktop JS API calls which are not
const handlePromise = (val, func) => {
        if (typeof val === 'object' && val !== null && !!val.then) {
            val.then(res => func(res.value));
        } else {
            func(val);
        }
    }

    const setReps = (repsVal) => {
        handlePromise(repsVal, (repsNum = 0) => { setInfoInSpan("reps", "reps: " + repsNum); });
    };
    const setFactor = (fctVal) => {
        handlePromise(fctVal, (fctNum = 0) => { setInfoInSpan("ease", "ease: " + fctNum / 10 + "%"); });
    };
    const setIvl = (ivlVal) => {
        handlePromise(ivlVal, (ivlNum = 0) => { setInfoInSpan("ivl", "ivl: " + ivlNum + "d"); });
    };
    try {
        const jsApiContract = { version: "0.0.3", developer: "https://github.com/jhhr" };

        const api = new AnkiDroidJS(jsApiContract);

        console.log("AnkiDroid API", api);

        setReps(api.ankiGetCardReps());
        setFactor(api.ankiGetCardFactor());
        setIvl(api.ankiGetCardInterval());
    } catch (e) {
        if (globalThis.ankiPlatform !== "desktop") {
            console.log("AnkiDroid API error", e);
        } else {
            pycmd("AnkiJS.ankiGetCardReps()", setReps);
            pycmd("AnkiJS.ankiGetCardFactor()", setFactor);
            pycmd("AnkiJS.ankiGetCardInterval()", setIvl);
        }
    }

Thank you very much for taking the time for clearing that up! A saying here goes “Who can read has the clear advantage”. I thought it was a full emulation.

This is what my code was like before the async update in the AnkiDroid JS API. The change did require adding a bunch more code so I guess I would like the desktop addon to also follow the same async structure as that’d remove the need for the handlePromise helper.

    const setReps = (repsVal) => {
        setInfoInSpan("reps", "reps: " + repsNum);
    };
    const setFactor = (fctVal) => {
        setInfoInSpan("ease", "ease: " + fctNum / 10 + "%");
    };
    const setIvl = (ivlVal) => {
        setInfoInSpan("ivl", "ivl: " + ivlNum + "d");
    };
    try {
        const jsApiContract = { version: "0.0.3", developer: "https://github.com/jhhr" };

        const api = new AnkiDroidJS(jsApiContract);

        console.log("AnkiDroid API", api);

        setReps(api.ankiGetCardReps());
        setFactor(api.ankiGetCardFactor());
        setIvl(api.ankiGetCardInterval());
    } catch (e) {
        if (globalThis.ankiPlatform !== "desktop") {
            console.log("AnkiDroid API error", e);
        } else {
            pycmd("AnkiJS.ankiGetCardReps()", setReps);
            pycmd("AnkiJS.ankiGetCardFactor()", setFactor);
            pycmd("AnkiJS.ankiGetCardInterval()", setIvl);
        }
    }

But what I’d most like is that all the functions available in the AnkiDroid JS API would also be implemented in the addon. I’m not going to implement anything in my cards where the implementation would have to vastly different on desktop vs Android so I’m basically limited to the intersection set of the JS API addon and the AnkiDroid JS API.

I’ve been thinking a little about doing (some of) that work but have only done the one function that’s in my fork so far. Maybe we can work together on, split the work?

At this time I have to say no. Too much on my plate which offers a lot more reward for me. My solution is to hardly work on Desktop at all. Thanks for the offer! But I will put this in my JS API wrapper which will then work on both platforms.

TTS: I also have a wrapper around the TTS API. I have not published it yet, b/c it is currently tightly coupled with code not suitable for publishing. If any developer is interested to do something with this, I will try to find the time to publish it. It has these features:
– Speak only text, all html tags are not spoken.
– Pause after each sentence.
– Correct bad pronunciation by replacing a list of words with phonetic spelling.
– Set the correct language for each note by tags.
– Speak the card forever in a loop.
– Blacken the screen and option lock inputs while speaking.

I update this here: AnkiDroid Javascript API · ankidroid/Anki-Android Wiki · GitHub

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.