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?
Thanks!
M
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;
}
resolve(response.result);
} catch (e) {
reject(e);
}
});
xhr.open('POST', 'http://127.0.0.1:8765');
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}`);