<% if (theme.comments.use === 'gitment') { %>
    <script>
        function loadComment() {
            let e, i;
            (e = document.createElement("script")).src = 'https://imsun.github.io/gitment/dist/gitment.browser.js',
            document.body.appendChild(e);
            e.onload = () => {
                var gitment = new Gitment({
                    id: window.location.pathname,
                    owner: '<%= theme.comments.gitment.owner %>',
                    repo: '<%= theme.comments.gitment.repo %>',
                    oauth: {
                        client_id: '<%= theme.comments.gitment.client_id %>',
                        client_secret: '<%= theme.comments.gitment.client_secret %>',
                    },
                })
                gitment.render('gitment-container')
            };
            (i = document.createElement("link")).rel = "stylesheet",
            i.href = 'https://imsun.github.io/gitment/style/default.css',
            document.head.appendChild(i);
        }
    
        var runningOnBrowser = typeof window !== "undefined";
        var isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
        var supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window;
    
        setTimeout(function () {
            if (!isBot && supportsIntersectionObserver) {
                var comment_observer = new IntersectionObserver(function(entries) {
                    if (entries[0].isIntersecting) {
                        loadComment();
                        comment_observer.disconnect();
                    }
                }, { threshold: [0] });
                comment_observer.observe(document.getElementById('comment'));
            } else {
                loadComment();
            }
        }, 1);
    </script>
<% } %>