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.