AnkiConnect to Add to Anki

Not used to javascript, so wondering if someone would be able to help. I’m trying to convert a Draft into a new Anki card. The callback URL way works on iOS but not on Mac. I’ve previously been able to write a small pop clip extension with AnkiConnect (FooSoft Productions - AnkiConnect) but that was using Curl.

This is the example JS provided in above. Just trying it in Drafts gives me an error for trying to ‘invoke’ the function. Is that not allowed from within Drafts?


function invoke(action, version, params={}) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.addEventListener('error', () => reject('failed to issue request'));
        xhr.addEventListener('load', () => {
            try {
                const response = JSON.parse(xhr.responseText);
                if (Object.getOwnPropertyNames(response).length != 2) {
                    throw 'response has an unexpected number of fields';
                if (!response.hasOwnProperty('error')) {
                    throw 'response is missing required error field';
                if (!response.hasOwnProperty('result')) {
                    throw 'response is missing required result field';
                if (response.error) {
                    throw response.error;
            } catch (e) {
        });'POST', '');
        xhr.send(JSON.stringify({action, version, params}));

await invoke('createDeck', {deck: 'test1'});
const result = await invoke('deckNames', 6);
console.log(`got list of decks: ${result}`);

Drafts does not have XMLHTTPRequest objects. That is an extension to JavaScript added by browsers. Draft can make HTTP requests, but that would have to be re-written using the Drafts’ HTTP object to make the request.

1 Like

Thanks for your response. I’ve tried to go another way - it seems AppleScript is supported and I’ve been able to get the following to work in the editor but not with drafts. Any idea where I’m going wrong?

– AppleScript content

– Editing and testing in Script Editor recommended

on execute(draft)

set theText to title of the draft

set theContent to content of the draft

set theCURL to “curl localhost:8765 -X POST -d " & “”{ \“action\”: \“guiAddCards\”, \“version\”: 6, \“params\”: { \“note\”: { \“deckName\”: \“Untagged\”, \“modelName\”: \“Basic\”, \“fields\”: { \“Front\”: \” " & theText & " \", \“Back\”: \" " & theContent & " \" }, \“options\”: {\“closeAfterAdding\”: true }, \“tags\”: [ \“countries\”]}}}""

do shell script theCURL

end execute

For reference, this is the command as I would type it into Terminal:

curl localhost:8765 -X POST -d "{ “action”: “guiAddCards”, “version”: 6, “params”: { “note”: { “deckName”: “Untagged”, “modelName”: “Basic”, “fields”: { “Front”: " Test1 “, “Back”: " Test2 " }, “options”: {“closeAfterAdding”: true }, “tags”: [ “countries”]}}}”

I’m wondering how feasible it would be to create a shim to simulate common XHR uses - as it might open up some porting possibilities.

(I can’t think what; I’m just musing.) :slight_smile:

(No, I don’t have time right now to try building it.) :frowning:

1 Like