I currently use the following Python script (adapted from AnkiConnect’s documentation) for this purpose, but I need a Javascript code that does the same job. I’ve tried to adapt the Javascript example as well but I can’t make it work.
import json
import urllib.request
def request(action, **params):
return {'action': action, 'params': params, 'version': 6}
def invoke(action, **params):
requestJson = json.dumps(request(action, **params)).encode('utf-8')
response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson)))
if len(response) != 2:
raise Exception('response has an unexpected number of fields')
if 'error' not in response:
raise Exception('response is missing required error field')
if 'result' not in response:
raise Exception('response is missing required result field')
if response['error'] is not None:
raise Exception(response['error'])
return response['result']
result = invoke('cardsInfo', cards=[1627088233969, 1627088233990])
for elem in result:
print(str(elem['cardId']) + "\n" + elem['fields']['Frente']['value'] + "\n" + elem['fields']['Verso']['value'] + "\n\n")
The use case is that I want to be able to integrate my flashcards to my Obsidian notes. The first approach I thought about was to create a custom URL scheme then use it to generate deep links to open the Previewer, but I guess AnkiConnect already does a similar job.
Then I found a plugin that let Obsidian users run javascript codes inside it as well as a plugin that renders flashcards inside Obsidian. However, the later plug-in can only send information to Anki with the help of AnkiConnect but never get the cards from the database. So I’ll give the first one a try.
Can anyone help me with that?