2

<div class="comments"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    const darkModeMeidaQuery = window.matchMedia('(prefers-color-scheme: dark)');

    function updateForDarkModeChange() {
        if (darkModeMeidaQuery.matches) {
            var colorScheme = 'dark'
        } else {
            var colorScheme = 'light'
        }
        $('.comments').append('<script src="https://utteranc.es/client.js" repo="user/repo-name" issue-term="pathname" theme="github-'+colorScheme+'" crossorigin="anonymous" async><\/script>');
    }

    darkModeMeidaQuery.addListener(updateForDarkModeChange);
    updateForDarkModeChange();
</script>

I'm trying to load utterances to match browser theme with the above script. However, the object is created normally, but it throws: Uncaught Error: "issue-term" or "issue-number" must be specified. I tried escaping characters, but it didn't works. Is there any part that I wrote wrong?

1 Answer 1

1

Nice job! Most new users do not include a minimally reproducible example, which you've done!

When in doubt, always consult the issue tracker. You didn't do anything wrong, but jQuery is wiping out your properties.

Follow the instructions from the issue comment:

<div id="comments"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    const darkModeMeidaQuery = window.matchMedia('(prefers-color-scheme: dark)');
    const comments = document.getElementById('comments');
 
    function updateForDarkModeChange() {
        if (darkModeMeidaQuery.matches) {
            var colorScheme = 'dark'
        } else {
            var colorScheme = 'light'
        }
 
        var script = document.createElement('script');
        script.src = 'https://utteranc.es/client.js';
        script.setAttribute('repo', 'ergo9ine/sfdb_tracker');
        script.setAttribute('issue-term', 1);
        script.setAttribute('theme', 'github-dark');
        script.setAttribute('crossorigin', 'anonymous');
        comments.appendChild(script)
    }

    darkModeMeidaQuery.addListener(updateForDarkModeChange);
    updateForDarkModeChange();
</script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.