Does Anki block JavaScript's access to the web?

I am trying to make an Anki card that includes a random example sentence from tatoeba.org for an Italian word. I would like this to be done via their API in Anki, rather than having to download all the sentences and saving them all in the card.

I got some javascript for this working in jsfiddle but, in Anki the placeholders never update.

<strong>Italian Word:</strong> <span id="word">cavallo</span>
<div id="sentence-container">
  <strong>Italian:</strong> <span id="italian-sentence">Loading...</span><br />
  <strong>English:</strong> <span id="english-sentence">Loading...</span>
</div>

<script>
  document.addEventListener('DOMContentLoaded', (event) => {
    async function fetchSentence(word) {
      const italianSentenceElement = document.getElementById('italian-sentence');
      const englishSentenceElement = document.getElementById('english-sentence');
      const url = // had to remove this line to post
      const proxyUrl = // had to remove this line to post

      try {
        const response = await fetch(proxyUrl + url);
        if (!response.ok) {
          throw new Error('Network response was not ok ' + response.statusText);
        }
        const data = await response.json();

        if (data.results && data.results.length > 0) {
          const firstResult = data.results[0];
          italianSentenceElement.innerText = firstResult.text;

          if (firstResult.translations && firstResult.translations.length > 0 && firstResult.translations[0].length > 0) {
            englishSentenceElement.innerText = firstResult.translations[0][0].text;
          } else {
            englishSentenceElement.innerText = 'No English translations found';
          }
        } else {
          italianSentenceElement.innerText = 'No sentences found';
          englishSentenceElement.innerText = 'No sentences found';
        }
      } catch (error) {
        console.error('There has been a problem with your fetch operation:', error);
        italianSentenceElement.innerText = 'Failed to load sentences';
        englishSentenceElement.innerText = 'Failed to load sentences';
      }
    }

    const wordElement = document.getElementById('word');
    const word = wordElement.innerText;
    fetchSentence(word);
  });

</script>

My only guess is that Anki, for security reasons, is blocking fetch calls for security reasons, or has a limited support of Javascript. Can anyone confirm or deny?

As for getting it working: if I can’t pull via API, it looks like my only option is to hard-code every sentence into the field and then write some javascript to pull a random sentence from the field. Not ideal though.

I’m afraid I can not provide support for JS (Styling & HTML - Anki Manual), but one possible issue you have there is that DOMContentLoaded will have already fired by the time that code runs.

Another option is using an addon:

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