PageSourceSearch

https://daohang-c5k.pages.dev//assets/js/hot.js?v=1789212342

js daohang-c5k.pages.dev collected 2026-10-03 06:39:39 UTC 4,530 bytes, 139 lines download raw bytes

1// 定义API接口,请自己部署自己的api,不要使用我的
2// 定义API接口
3const primaryEndpoints = (window.CONFIG && window.CONFIG.hotApiEndpoints && Array.isArray(window.CONFIG.hotApiEndpoints))
4  ? window.CONFIG.hotApiEndpoints
5  : ['https://hot.noisework.cn', 'https://hot.noisedh.link'];
6
7const apiEndpoints = {
8  zhihu: '/zhihu',
9  weibo: '/sina',
10  bilibili: '/bilibili',
11  douyin: '/douyin',
12  baidu: '/tieba',
13  toutiao: '/toutiao',
14  v2ex: '/v2ex',
15  hellogithub: '/hellogithub'
16};
17
18function notifyHotlistLayoutUpdated() {
19  document.dispatchEvent(new CustomEvent('hotlist:updated'));
20}
21
22// 使用 fetch API 从不同的API端点请求数据
23function fetchData(target) {
24  const targetElement = document.getElementById(target);
25  if (!targetElement) return;
26  const updateTimeElement = targetElement.querySelector('.update-time');
27  if (!updateTimeElement) return;
28  updateTimeElement.textContent = '数据更新时间: 加载中...';
29
30  const endpointPath = apiEndpoints[target];
31  let currentEndpointIndex = 0; // 默认使用第一个API
32
33  const fetchFromEndpoint = (url) => {
34    fetch(url)
35      .then(response => {
36        if (!response.ok) {
37          throw new Error(`HTTP error! status: ${response.status}`);
38        }
39        return response.json();
40      })
41      .then(data => {
42        console.log('Data received from', url, data);
43        loadData(data.data, target);
44        updateTimeElement.textContent = `数据更新时间: ${new Date().toLocaleString()}`;
45        saveDataToLocalStorage(target, data.data); // 保存数据到本地存储
46        notifyHotlistLayoutUpdated();
47      })
48      .catch(error => {
49        console.error('Error fetching data from', url, error);
50        currentEndpointIndex++;
51        if (currentEndpointIndex < primaryEndpoints.length) {
52          fetchFromEndpoint(`${primaryEndpoints[currentEndpointIndex]}${endpointPath}`);
53        } else {
54          updateTimeElement.textContent = `数据更新时间: 错误`;
55          const list = document.getElementById(target + '-list');
56          const li = document.createElement('li');
57          li.textContent = `Error: ${error.message}`;
58          list.appendChild(li);
59          notifyHotlistLayoutUpdated();
60        }
61      });
62  };
63
64  fetchFromEndpoint(`${primaryEndpoints[currentEndpointIndex]}${endpointPath}`);
65}
66
67function loadData(data, target) {
68  const list = document.getElementById(target + '-list');
69  if (!list) return;
70  list.innerHTML = ''; // 清空列表
71  if (Array.isArray(data)) {
72    data.forEach((item, index) => {
73      const li = document.createElement('li');
74      li.setAttribute('data-index', `${index + 1}.`);
75      const url = (window.innerWidth > 768) ? item.url : item.mobileUrl || '#';
76      const a = document.createElement('a');
77      a.textContent = item.title || 'No title';
78      a.href = url || '#';
79      a.target = '_blank';
80      a.rel = 'noopener noreferrer';
81      li.appendChild(a);
82      list.appendChild(li);
83    });
84  } else {
85    const li = document.createElement('li');
86    li.textContent = 'No data received';
87    list.appendChild(li);
88  }
89  notifyHotlistLayoutUpdated();
90}
91
92function saveDataToLocalStorage(target, data) {
93  localStorage.setItem(target, JSON.stringify(data));
94}
95
96function loadFromLocalStorage(target) {
97  const storedData = localStorage.getItem(target);
98  if (storedData) {
99    const data = JSON.parse(storedData);
100    const updateTimeElement = document.getElementById(target).querySelector('.update-time');
101    updateTimeElement.textContent = `数据更新时间: ${new Date().toLocaleString()}`;
102    loadData(data, target);
103  }
104}
105
106function refreshData(target) {
107  fetchData(target);
108}
109
110// 页面加载时初始化数据
111document.addEventListener('DOMContentLoaded', () => {
112  const targets = Object.keys(apiEndpoints);
113  targets.forEach(target => {
114    loadFromLocalStorage(target);
115  });
116  const scheduleFetch = function (target, delay) {
117    window.setTimeout(function () {
118      if (document.visibilityState !== 'visible') return;
119      if ('requestIdleCallback' in window) {
120        requestIdleCallback(function () {
121          fetchData(target);
122        }, { timeout: 1200 });
123        return;
124      }
125      fetchData(target);
126    }, delay);
127  };
128  targets.forEach((target, index) => {
129    scheduleFetch(target, index * 450);
130  });
131});
132
133// 每小时自动刷新热榜数据
134setInterval(() => {
135  if (document.visibilityState !== 'visible') return;
136  Object.keys(apiEndpoints).forEach(target => {
137    fetchData(target);
138  });
139}, 3600000);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.