AnkiHubSoftware / ankihub_addon

3 stars 0 forks source link

Use python-js bridge from iframe POC #973

Closed RisingOrange closed 4 months ago

RisingOrange commented 5 months ago

See https://github.com/ankipalace/ankihub/pull/2138

This script needs to be included on a note type template:

<script>
    // Replace with your own token
    const knox_token = "9c253b471e41a6a213fb52e3f72d4ac2c4d510f5fa7da1e0acb5b7bcb24c7ac4";

    // Replace this with your own app url
    const appUrl = "http://localhost:8000"

    // Replace the deck id with your own deck id
    const targetUrl = (
        `${appUrl}/ai/js-python-bridge-demo/`
    )

    const iframe = document.createElement("iframe");
    iframe.id = "ankihub"
    iframe.src = `${appUrl}/ai/embed_auth?next=${encodeURIComponent(targetUrl)}`
    iframe.width = "100%"
    iframe.height = "500px"
    iframe.style.border = "none"

    // Hide scrollbar of iframe
    iframe.style.overflow = "hidden"
    iframe.scrolling = "no"

    iframe.onload = function () {
        iframe.contentWindow.postMessage(knox_token, appUrl);
        console.log("Message sent to iframe");
    }

    window.addEventListener("message", function (event) {
        if (event.origin === appUrl) {
            const message = event.data;
            console.log(`Message received from iframe: ${message}`);

            // Send the message to Python
            pycmd(message)
        }
    });

    document.body.appendChild(iframe)
</script>

Related issues

Proposed changes

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.40%. Comparing base (f71d055) to head (96ede48). Report is 3 commits behind head on main.

Files Patch % Lines
ankihub/gui/reviewer.py 66.66% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #973 +/- ## ========================================== - Coverage 87.66% 87.40% -0.26% ========================================== Files 66 66 Lines 6631 6645 +14 ========================================== - Hits 5813 5808 -5 - Misses 818 837 +19 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.