一份统计楼层风评操作的 snippet,三种不同写法(CC98 论坛)

用处:① 观察风评战的风向 ② 验证是否已经“平衡”或“归零”

用法:放到浏览器的控制台中运行

id 对应页面元素的 id 属性,通俗地说就是这个页面中的第 n 个楼层(从 1 开始)。热门回复的情况有所不同,读源代码可知const id = item.floor % 10;,所以会出现形如“hot_6”的 id,这类需要手动看开发者工具确定。

版本 1

使用到的有 CSS 选择器、XPath、HTMLElement.click()、CC98 页面上的 jQuery

id = "1";
expandButton = $(`#${id}.reply .awardInfo > button`);
if (expandButton.text() === "显示全部") expandButton.click();
for (currentInspectingOperation of ["风评值 -1", "风评值 +1"]) {
  console.log(
    currentInspectingOperation,
    $x(
      `count(//div[@class="reply" and @id="${id}"]//div[@class="grades" and .="${currentInspectingOperation}"])`
    )
  );
}

版本 2

从与元素相关联的 React 实例中读取数据,依赖其非公开的接口

id = "1";
awardElement = document.querySelector(`#${CSS.escape(id)} .awardInfo`);
reactInstanceName = Object.keys(awardElement).find((name) =>
  name.startsWith("__reactInternalInstance")
);
awardInfo = awardElement[
  reactInstanceName
].return.memoizedProps.awardInfo.reduce(
  (acc, cur) => {
    switch (cur.content) {
      case "风评值 -1":
        acc.deduct++;
        break;
      case "风评值 +1":
        acc.add++;
        break;
    }
    return acc;
  },
  { deduct: 0, add: 0 }
);
console.log(`风评值 -1:${awardInfo.deduct}\n风评值 +1:${awardInfo.add}`);

版本 3

CC98 API请求数据

这个版本暂不支持直接使用形如hot_${id}的 id 获取热评的风评情况

id = "1";
[topicId, pageId] = location.pathname.slice(7).split("/"); // "/topic/"
floorId = ((pageId ?? 1) - 1) * 10;
accessToken = localStorage.getItem("accessToken").slice(4); // "str-"
fetch(`https://api.cc98.org/topic/${topicId}/post?from=${floorId}&size=1`, {
  headers: new Headers({ Authorization: accessToken }),
})
  .then((res) => res.json())
  .then((res) =>
    res[0].awards.reduce((acc, cur) => {
      acc[cur.content] ? acc[cur.content]++ : (acc[cur.content] = 1);
      return acc;
    }, {})
  )
  .then((res) => Object.entries(res).forEach(([k, v]) => console.log(k, v)));