Detect if on AnkiMobile using javascript

I am making a card deck, and I can detect whether the client is on the desktop version of Anki or AnkiDroid. If they are not on those platforms, I can deduce that the cards are on either AnkiWeb or AnkiMobile. How can I distinguish between them with just a javascript function returning a boolean? I know that iOS can be detected with javascript, but people use AnkiWeb on Safari or Chrome on Apple devices; I am looking for some detectable feature that is specific to the app, so the help of a developer would be greatly appreciated.

1 Like

I think there will be a class mobile in the <html> tag.
You can use js to check it.

Also there is an “Add Mobile Class” option in card template preview feature.

1 Like

Please see Styling & HTML - Anki Manual

1 Like

Thank you, both. I ended up using document.documentElement.classList.contains(''). The classes iphone and ipad are specific to AnkiMobile, and mobile works for either AnkiDroid or AnkiMobile.

I created a function exactly for this:

// Detects where the script is being executed
function detectAnkiPlatform() {
    if (window.anki && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.cb !== undefined) {
        return "AnkiMobile";
    } else if (document.querySelector("span.align-middle") && document.querySelector("span.align-middle").textContent.includes("AnkiWeb")) {
        return "AnkiWeb";
    } else if (typeof AnkiDroidJS !== "undefined") {
        return "AnkiDroid";
    } else if (typeof pycmd !== "undefined") {
        return "AnkiDesktop";
    } else {
        return "Unknown";
    }
}

If you want to see the complete model here.

1 Like

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