streamlit-javascript icon indicating copy to clipboard operation
streamlit-javascript copied to clipboard

JavaScript code must start on first line of string

Open BenSchZA opened this issue 1 year ago • 3 comments

For anyone else who may run into this error (haven't dug into why this happens) the JavaScript code to execute must start on the first line of a multi-line string.

Returns 0:

    script = """
    await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """

Returns 10:

    script = """10
    await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """

Returns the expected value of cookies:

    script = """await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """

BenSchZA avatar Aug 25 '23 18:08 BenSchZA

Genius brother, genius. Thanks!

Socvest avatar Sep 15 '23 18:09 Socvest

Not working for me. Even a simple script = 'await (async function () { return "test"; })()' return 0

ghz avatar Oct 29 '23 15:10 ghz

@GHz this is probably related to issue #14 .

BenSchZA avatar Nov 01 '23 07:11 BenSchZA