# 自动获取热门帖子并且显示

**URL:** <https://www.isharkfly.com/t/topic/16792>\
**Category:** Discourse\
**Tags:** discourse\
**Created:** [January 14, 2025, 7:41am UTC](https://www.isharkfly.com/t/topic/16792 "2025-01-14T07:41:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 14, 2025, 7:41am UTC](https://www.isharkfly.com/t/topic/16792/1 "2025-01-14T07:41:05Z")

</div>

## [前往我的论坛](https://www.justnainai.com)

 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/7/8/78fe130d9bf9a1c8cbe00bc6e38d22bce1b463b9.jpeg)

## 目前这个只兼容我的论坛 你需要自己修改我的源码来兼容你自己的

**目前不支持 切换黑夜后 标题和背景变成灰色 不知道是什么问题**

> **html+JS**
>
> ```auto
> <script>
> // 确保 DOM 完全加载后再执行代码
> document.addEventListener("DOMContentLoaded", () => {
> // 获取侧边栏容器
> const sidebar = document.querySelector(".tc-right-sidebar");
> 
> // 检查是否找到侧边栏容器
> if (!sidebar) {
> console.error("未找到 .tc-right-sidebar 容器！");
> return;
> }
> 
> // 添加热点推荐内容
> sidebar.innerHTML = `
> <div class="custom-sidebar">
> <div class="recommendation-container">
> <div class="recommendation-header">
> <h2><i class="fa fa-hand-o-right"></i>最新游戏帖</h2> <!-- 添加图标和标题 -->
> </div>
> <ul class="recommendation-list">
> <!-- 默认占位内容 -->
> <li class="recommendation-item">
> <a href="#">
> <img src="https://www.justnainai.com/uploads/default/original/1X/47e2788af99a2cfec29a917364c578757f67d9ef.jpeg" alt="占位图1">
> </a>
> </li>
> </ul>
> </div>
> </div>
> `;
> 
> // 动态加载数据
> fetchTopics();
> 
> // 监听主题切换
> observeThemeChanges();
> });
> 
> /**
> * 从指定 API 获取主题数据
> */
> async function fetchTopics() {
> try {
> // 请求 API 数据
> const response = await fetch("https://www.justnainai.com/c/游戏中心/5/l/latest.json");
> if (!response.ok) {
> throw new Error("数据获取失败");
> }
> 
> const jsonData = await response.json();
> 
> // 提取前4条主题数据
> const topics = jsonData.topic_list.topics.slice(0, 4).map(topic => ({
> id: topic.id,
> title: topic.title,
> slug: topic.slug,
> image_url: topic.image_url || "https://www.justnainai.com/uploads/default/original/1X/47e2788af99a2cfec29a917364c578757f67d9ef.jpeg",
> }));
> 
> // 渲染到页面
> renderTopics(topics);
> } catch (error) {
> console.error("获取数据时出错:", error);
> }
> }
> 
> /**
> * 渲染推荐内容到页面
> * @param {Array} topics - 主题数据
> */
> function renderTopics(topics) {
> const recommendationList = document.querySelector(".recommendation-list");
> recommendationList.innerHTML = ""; // 清空占位内容
> 
> topics.forEach(topic => {
> const link = `https://www.justnainai.com/t/${topic.slug}/${topic.id}`;
> const image = topic.image_url;
> 
> const listItem = document.createElement("li");
> listItem.className = "recommendation-item";
> 
> // 创建推荐项 HTML
> listItem.innerHTML = `
> <a href="${link}" title="${topic.title}" target="_blank">
> <img src="${image}" alt="${topic.title}">
> </a>
> `;
> recommendationList.appendChild(listItem);
> });
> }
> </script>
> 
> ```

> **CSS**
>
> ```auto
> .custom-sidebar {
> padding: 10px;
> background-color: #fff; /* 白色背景 */
> border-radius: 5px;
> box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
> margin-bottom: 20px;
> }
> 
> .recommendation-container .recommendation-header h2 {
> font-size: 23x;
> margin-bottom: 10px;
> color: #333; /* 标题黑色 */
> }
> 
> .recommendation-list {
> list-style-type: none;
> padding: 0;
> margin: 0;
> }
> 
> .recommendation-item {
> margin-bottom: 10px;
> }
> 
> .recommendation-item img {
> width: 300px; /* 固定宽度 */
> height: 156px; /* 固定高度，保持一致 */
> object-fit: cover; /* 保持图片内容的比例 */
> border-radius: 5px;
> box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
> transition: transform 0.2s ease;
> }
> 
> .recommendation-item img:hover {
> transform: scale(1.05);
> box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 14, 2025, 7:42am UTC](https://www.isharkfly.com/t/topic/16792/2 "2025-01-14T07:42:32Z")

</div>

唉 只能说都没人看我的帖子伤心

---

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 14, 2025, 8:15am UTC](https://www.isharkfly.com/t/topic/16792/3 "2025-01-14T08:15:11Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/0/3/033238822824007bf5777a5c800b378f81f9230c.jpeg)  
使用了一个取巧的办法

 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/a/5/a5a1adf9b61166fdaa712c2ee67b64d8a1805e02.jpeg)  
但是不会写论坛的黑夜模式

---

<div class="post-metadata">

**Author:** ![luobo](https://www.isharkfly.com/user_avatar/www.isharkfly.com/luobo/32/15604_2.png) [@luobo](https://www.isharkfly.com/u/luobo)\
**Post date:** [January 14, 2025, 11:10am UTC](https://www.isharkfly.com/t/topic/16792/4 "2025-01-14T11:10:59Z")

</div>

> [@VegaMonika](#):
>
> 标题和背景变成灰色

估计是主题自带的css影响了，可以看看父类是不是有对应的样式

---

<div class="post-metadata">

**Author:** ![summer](https://www.isharkfly.com/user_avatar/www.isharkfly.com/summer/32/16283_2.png) [@summer](https://www.isharkfly.com/u/summer)\
**Post date:** [January 15, 2025, 1:47pm UTC](https://www.isharkfly.com/t/topic/16792/5 "2025-01-15T13:47:15Z")

</div>

谢谢楼主分享，可以写具体点怎么操作吗？比如要修改哪些文件。。。

---

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 17, 2025, 9:54am UTC](https://www.isharkfly.com/t/topic/16792/6 "2025-01-17T09:54:07Z")

</div>

需要一点代码基础

---

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 17, 2025, 9:55am UTC](https://www.isharkfly.com/t/topic/16792/7 "2025-01-17T09:55:12Z")

</div>

> [@VegaMonika](#):
>
> ```auto
> // 获取侧边栏容器
> const sidebar = document.querySelector(".tc-right-sidebar");
> 
> // 检查是否找到侧边栏容器
> if (!sidebar) {
> console.error("未找到 .tc-right-sidebar 容器！");
> return;
> }
> 
> ```

第一个这里

```auto
// 请求 API 数据
            const response = await fetch("https://www.justnainai.com/c/游戏中心/5/l/latest.json");

```

第三个是一个默认图片

```auto
                image_url: topic.image_url || "https://www.justnainai.com/uploads/default/original/1X/47e2788af99a2cfec29a917364c578757f67d9ef.jpeg",

```

你可以改也可以不改

---

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [January 25, 2025, 11:21am UTC](https://www.isharkfly.com/t/topic/16792/8 "2025-01-25T11:21:48Z")

</div>

嗯水一下  
😀
