Artalk icon indicating copy to clipboard operation
Artalk copied to clipboard

请教下怎么在html文件中调用最新的几条评论

Open koobai opened this issue 1 year ago • 2 comments

请教下怎么在html文件中调用最新的几条评论,看文档中没写,使用的hugo博客系统,感谢

koobai avatar Jan 11 '24 10:01 koobai

可以请求这个接口:{你的Artalk地址}/api/v2/stats/latest_comments?site_name=站点名&limit=5 获取最新的 5 条评论

参考以下案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Latest Comments</title>
</head>
<body>
    <h1>Latest Comments</h1>
    <ul id="commentsList"></ul>

    <script>
        // 构造请求的URL
        const apiUrl = "{你的Artalk地址}/api/v2/stats/latest_comments?site_name=站点名&limit=5";

        // 发起GET请求
        fetch(apiUrl)
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                // 处理响应数据
                const comments = data.data;
                const commentsList = document.getElementById('commentsList');

                // 遍历评论并添加到页面中
                comments.forEach(comment => {
                    const li = document.createElement('li');
                    li.textContent = comment.content;
                    commentsList.appendChild(li);
                });
            })
            .catch(error => {
                console.error('There was a problem with the fetch operation:', error);
            });
    </script>
</body>
</html>

可参考 HTTP 文档:https://artalk.js.org/http-api#tag/Statistic/operation/GetStats


有点麻烦,下一个版本我打算加一个函数到 Artalk,直接调用就能获取最新评论

qwqcode avatar Feb 01 '24 10:02 qwqcode

感谢,下个版本增加的时候,是否可以配置调用的时候,选择是否需要头像/用户名之类的,然后评论的跳转链接是不是可以加上~。还有能不能排除特定用户的评论,譬如排除掉管理员的评论~

koobai avatar Feb 02 '24 07:02 koobai