> For the complete documentation index, see [llms.txt](https://soradocs.gitbook.io/sora-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soradocs.gitbook.io/sora-documentation/async-javascript-mode.md).

# Async JavaScript Mode

{% hint style="info" %}
For more examples, head over to [my repo](https://github.com/50n50/sources)
{% endhint %}

In the async Javascript mode Sora will only provide the search keyword for the the searchResults function and the URL for the other three functions. Aside from that, the response format is required to be the same as normal mode.

{% hint style="info" %}
WARNING:

Do not use .json() or .text() methods as those will not work on iOS!&#x20;

For .json():

{% code title="Wrong method" lineNumbers="true" %}

```javascript
const data = await response.json();
```

{% endcode %}

Instead use:

{% code title="Correct method" lineNumbers="true" %}

```javascript
const data = await JSON.parse(response);
```

{% endcode %}

For .text():

{% code title="Wrong method" lineNumbers="true" %}

```javascript
const data = await response.text();
```

{% endcode %}

Instead assign the value directly:

{% code title="Correct method" lineNumbers="true" %}

```javascript
const data = await response;
```

{% endcode %}

Goes without saying that this applies to StreamAsync mode too. You shouldn't need these methods in normal mode but if you do, use the above mentioned way.
{% endhint %}

**Functions:**&#x20;

{% tabs %}
{% tab title="searchResults" %}
Extracts the search results from the provided keyword.

| Input   | Output |
| ------- | ------ |
| Keyword | JSON   |

{% code title="Output JSON format" lineNumbers="true" %}

```json
{
   "title": "Example Title",
   "image": "https://example.com/image.jpg",
   "href": "https://grani.me/example"
}
```

{% endcode %}
{% endtab %}

{% tab title="extractDetails" %}
Extracts the details from the provided URL.

| Input | Output |
| ----- | ------ |
| URL   | JSON   |

{% code title="Output JSON format" lineNumbers="true" %}

```json
{
   "description": "An exciting anime series about adventures.",
   "aliases": "Alternate Name",
   "airdate": "2022"
}
```

{% endcode %}
{% endtab %}

{% tab title="extractEpisodes" %}
Extracts the expisodes from the provided URL.

| Input | Output |
| ----- | ------ |
| URL   | JSON   |

{% code title="Output JSON format" lineNumbers="true" %}

```json
{
   "href": "https://grani.me/episode/123",
   "number": "1"
}
```

{% endcode %}
{% endtab %}

{% tab title="extractStreamUrl" %}
Extracts the stream url from the provided URL.

| Input | Output |
| ----- | ------ |
| URL   | URL    |

{% code title="Output format" lineNumbers="true" %}

```
https://example.com/stream/video.mp4
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Example:**

{% code lineNumbers="true" %}

```javascript
async function searchResults(keyword) {
    try {
        const encodedKeyword = encodeURIComponent(keyword);
        const responseText = await fetch(`https://api.animemundo.net/api/v2/hianime/search?q=${encodedKeyword}&language=dub`);
        const data = JSON.parse(responseText);

        const filteredAnimes = data.data.animes.filter(anime => anime.episodes.dub !== null); 
        
        const transformedResults = data.data.animes.map(anime => ({
            title: anime.name,
            image: anime.poster,
            href: `https://hianime.to/watch/${anime.id}`
        }));
        
        return JSON.stringify(transformedResults);
        
    } catch (error) {
        console.log('Fetch error:', error);
        return JSON.stringify([{ title: 'Error', image: '', href: '' }]);
    }
}

async function extractDetails(url) {
    try {
        const match = url.match(/https:\/\/hianime\.to\/watch\/(.+)$/);
        const encodedID = match[1];
        const response = await fetch(`https://api.animemundo.net/api/v2/hianime/anime/${encodedID}`);
        const data = JSON.parse(response);
        
        const animeInfo = data.data.anime.info;
        const moreInfo = data.data.anime.moreInfo;

        const transformedResults = [{
            description: animeInfo.description || 'No description available',
            aliases: `Duration: ${animeInfo.stats?.duration || 'Unknown'}`,
            airdate: `Aired: ${moreInfo?.aired || 'Unknown'}`
        }];
        
        return JSON.stringify(transformedResults);
    } catch (error) {
        console.log('Details error:', error);
        return JSON.stringify([{
        description: 'Error loading description',
        aliases: 'Duration: Unknown',
        airdate: 'Aired: Unknown'
        }]);
  }
}

async function extractEpisodes(url) {
    try {
        const match = url.match(/https:\/\/hianime\.to\/watch\/(.+)$/);
        const encodedID = match[1];
        const response = await fetch(`https://api.animemundo.net/api/v2/hianime/anime/${encodedID}/episodes`);
        const data = JSON.parse(response);

        const transformedResults = data.data.episodes.map(episode => ({
            href: `https://hianime.to/watch/${encodedID}?ep=${episode.episodeId.split('?ep=')[1]}`,
            number: episode.number
        }));
        
        return JSON.stringify(transformedResults);
        
    } catch (error) {
        console.log('Fetch error:', error);
    }    
}

async function extractStreamUrl(url) {
    try {
       const match = url.match(/https:\/\/hianime\.to\/watch\/(.+)$/);
       const encodedID = match[1];
       const response = await fetch(`https://api.animemundo.net/api/v2/hianime/episode/sources?animeEpisodeId=${encodedID}&category=dub`);
       const data = JSON.parse(response);
       
       const hlsSource = data.data.sources.find(source => source.type === 'hls');
       
       return hlsSource ? hlsSource.url : null;
    } catch (error) {
       console.log('Fetch error:', error);
       return null;
    }
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soradocs.gitbook.io/sora-documentation/async-javascript-mode.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
