Artalk icon indicating copy to clipboard operation
Artalk copied to clipboard

请求增加 整个站点的总评论数调用

Open echs-top opened this issue 10 months ago • 2 comments

如标题

echs-top avatar Apr 03 '24 14:04 echs-top

我也需要这个功能。

不过我是问了gpt,写了个脚本

sqlite3 /artalk/artalk.db "SELECT COUNT(*) FROM comments;"

然后调用这个数字

dianso avatar Apr 04 '24 06:04 dianso

你好,这个功能已经存在了,GET 请求 https://artalk.xxx.com/api/v2/stats/site_comment?site_name=站点名 可以得到 {"data":957}

const siteName = '站点名'; // 替换为实际的站点名
const url = `https://artalk.xxx.com/api/v2/stats/site_comment?site_name=${encodeURIComponent(siteName)}`;

fetch(url)
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json();
  })
  .then(({ data }) => {
    console.log(data); // 输出获取的数据
  })
  .catch(error => {
    console.error('There was a problem with the fetch operation:', error);
  });

https://artalk.js.org/http-api.html#tag/Statistic/operation/GetStats

qwqcode avatar May 02 '24 03:05 qwqcode

没看懂如何引用,我自己只是兴趣,有没有示例

echs-top avatar May 21 '24 14:05 echs-top

Screenshot_2024-05-21-22-55-13-937_bin mt plus 比如图中的部分

echs-top avatar May 21 '24 14:05 echs-top

From ChatGPT:

要将数据输出到 HTML 中,你可以使用 JavaScript 来操作 DOM,并将获取的数据插入到 HTML 元素中。以下是一个简单的例子,假设你有一个 <div> 元素用于显示数据:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>显示数据</title>
</head>
<body>
  <div id="dataContainer"></div>

  <script>
    const siteName = '站点名'; // 替换为实际的站点名
    const url = `https://artalk.xxx.com/api/v2/stats/site_comment?site_name=${encodeURIComponent(siteName)}`;

    fetch(url)
      .then(response => {
        if (!response.ok) {
          throw new Error('Network response was not ok');
        }
        return response.json();
      })
      .then(({ data }) => {
        // 将数据插入到 HTML 元素中
        const dataContainer = document.getElementById('dataContainer');
        dataContainer.innerHTML = JSON.stringify(data); // 这里使用 JSON.stringify() 将数据转换为字符串输出
      })
      .catch(error => {
        console.error('There was a problem with the fetch operation:', error);
      });
  </script>
</body>
</html>

在这个例子中,我们创建了一个 <div> 元素并为其设置了一个唯一的 ID("dataContainer")。在 JavaScript 部分,我们通过 document.getElementById('dataContainer') 获取了这个元素的引用,然后将获取的数据插入到这个元素的 innerHTML 中。这里使用了 JSON.stringify() 将 JavaScript 对象转换为字符串,以便直接显示在 HTML 中。

你可以根据你的需求修改这个例子,比如更改数据展示的方式、样式等。

qwqcode avatar May 26 '24 09:05 qwqcode