html.py 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  1. # coding=utf-8
  2. """
  3. HTML 报告渲染模块
  4. 提供 HTML 格式的热点新闻报告生成功能
  5. """
  6. from datetime import datetime
  7. from typing import Any, Dict, List, Optional, Callable
  8. from trendradar.report.helpers import html_escape
  9. from trendradar.utils.time import convert_time_for_display
  10. from trendradar.ai.formatter import render_ai_analysis_html_rich
  11. def render_html_content(
  12. report_data: Dict,
  13. total_titles: int,
  14. mode: str = "daily",
  15. update_info: Optional[Dict] = None,
  16. *,
  17. region_order: Optional[List[str]] = None,
  18. get_time_func: Optional[Callable[[], datetime]] = None,
  19. rss_items: Optional[List[Dict]] = None,
  20. rss_new_items: Optional[List[Dict]] = None,
  21. display_mode: str = "keyword",
  22. standalone_data: Optional[Dict] = None,
  23. ai_analysis: Optional[Any] = None,
  24. show_new_section: bool = True,
  25. ) -> str:
  26. """渲染HTML内容
  27. Args:
  28. report_data: 报告数据字典,包含 stats, new_titles, failed_ids, total_new_count
  29. total_titles: 新闻总数
  30. mode: 报告模式 ("daily", "current", "incremental")
  31. update_info: 更新信息(可选)
  32. region_order: 区域显示顺序列表
  33. get_time_func: 获取当前时间的函数(可选,默认使用 datetime.now)
  34. rss_items: RSS 统计条目列表(可选)
  35. rss_new_items: RSS 新增条目列表(可选)
  36. display_mode: 显示模式 ("keyword"=按关键词分组, "platform"=按平台分组)
  37. standalone_data: 独立展示区数据(可选),包含 platforms 和 rss_feeds
  38. ai_analysis: AI 分析结果对象(可选),AIAnalysisResult 实例
  39. show_new_section: 是否显示新增热点区域
  40. Returns:
  41. 渲染后的 HTML 字符串
  42. """
  43. # 默认区域顺序
  44. default_region_order = ["hotlist", "rss", "new_items", "standalone", "ai_analysis"]
  45. if region_order is None:
  46. region_order = default_region_order
  47. html = """
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <meta charset="UTF-8">
  52. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  53. <title>热点新闻分析</title>
  54. <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js" integrity="sha512-BNaRQnYJYiPSqHHDb58B0yaPfCu+Wgds8Gp/gU33kqBtgNS4tSPHuGibyoeqMV/TJlSKda6FXzoEyYGjTe+vXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  55. <style>
  56. * { box-sizing: border-box; }
  57. body {
  58. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  59. margin: 0;
  60. padding: 16px;
  61. background: #fafafa;
  62. color: #333;
  63. line-height: 1.5;
  64. }
  65. .container {
  66. max-width: 600px;
  67. margin: 0 auto;
  68. background: white;
  69. border-radius: 12px;
  70. overflow: hidden;
  71. box-shadow: 0 2px 16px rgba(0,0,0,0.06);
  72. }
  73. .header {
  74. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  75. color: white;
  76. padding: 32px 24px;
  77. text-align: center;
  78. position: relative;
  79. overflow: visible;
  80. }
  81. .header-watermark {
  82. position: absolute;
  83. top: 50%;
  84. left: 50%;
  85. transform: translate(-50%, -50%);
  86. font-size: clamp(40px, 8vw, 80px);
  87. font-weight: 900;
  88. letter-spacing: 0.05em;
  89. color: rgba(255, 255, 255, 0.15);
  90. pointer-events: none;
  91. z-index: 1;
  92. white-space: nowrap;
  93. -webkit-mask-image: radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%);
  94. mask-image: radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%);
  95. transition: -webkit-mask-image 0.3s ease, mask-image 0.3s ease;
  96. user-select: none;
  97. }
  98. .save-buttons {
  99. position: absolute;
  100. top: 16px;
  101. right: 16px;
  102. display: flex;
  103. gap: 8px;
  104. z-index: 10;
  105. }
  106. .save-btn-group {
  107. position: relative;
  108. display: flex;
  109. }
  110. .save-btn {
  111. background: rgba(255, 255, 255, 0.2);
  112. border: 1px solid rgba(255, 255, 255, 0.3);
  113. color: white;
  114. padding: 10px 18px;
  115. border-radius: 6px 0 0 6px;
  116. cursor: pointer;
  117. font-size: 13px;
  118. font-weight: 500;
  119. transition: all 0.2s ease;
  120. backdrop-filter: blur(10px);
  121. white-space: nowrap;
  122. min-height: 38px;
  123. border-right: none;
  124. }
  125. .save-btn:hover {
  126. background: rgba(255, 255, 255, 0.3);
  127. }
  128. .save-btn:active {
  129. transform: translateY(0);
  130. }
  131. .save-btn:disabled {
  132. opacity: 0.6;
  133. cursor: not-allowed;
  134. }
  135. .save-dropdown-trigger {
  136. background: rgba(255, 255, 255, 0.2);
  137. border: 1px solid rgba(255, 255, 255, 0.3);
  138. color: white;
  139. padding: 10px 10px;
  140. border-radius: 0 6px 6px 0;
  141. cursor: pointer;
  142. font-size: 11px;
  143. transition: all 0.2s ease;
  144. backdrop-filter: blur(10px);
  145. min-height: 38px;
  146. display: flex;
  147. align-items: center;
  148. }
  149. .save-dropdown-trigger:hover {
  150. background: rgba(255, 255, 255, 0.35);
  151. }
  152. .save-dropdown-menu {
  153. position: absolute;
  154. top: 100%;
  155. right: 0;
  156. margin-top: 4px;
  157. background: rgba(30, 30, 50, 0.92);
  158. backdrop-filter: blur(16px);
  159. border: 1px solid rgba(255, 255, 255, 0.15);
  160. border-radius: 8px;
  161. padding: 4px;
  162. min-width: 140px;
  163. opacity: 0;
  164. visibility: hidden;
  165. transform: translateY(-4px);
  166. transition: all 0.2s ease;
  167. box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  168. }
  169. .save-btn-group:hover .save-dropdown-menu,
  170. .save-dropdown-menu:hover {
  171. opacity: 1;
  172. visibility: visible;
  173. transform: translateY(0);
  174. }
  175. .save-dropdown-item {
  176. display: block;
  177. width: 100%;
  178. padding: 9px 14px;
  179. background: none;
  180. border: none;
  181. color: white;
  182. font-size: 13px;
  183. cursor: pointer;
  184. border-radius: 5px;
  185. text-align: left;
  186. transition: background 0.15s;
  187. white-space: nowrap;
  188. }
  189. .save-dropdown-item:hover {
  190. background: rgba(255, 255, 255, 0.15);
  191. }
  192. .dropdown-icon {
  193. width: 14px;
  194. height: 14px;
  195. margin-right: 8px;
  196. vertical-align: -2px;
  197. flex-shrink: 0;
  198. }
  199. .header-title {
  200. font-size: 22px;
  201. font-weight: 700;
  202. margin: 0 0 20px 0;
  203. position: relative;
  204. z-index: 2;
  205. }
  206. .header-info {
  207. position: relative;
  208. z-index: 2;
  209. display: grid;
  210. grid-template-columns: 1fr 1fr;
  211. gap: 16px;
  212. font-size: 14px;
  213. opacity: 0.95;
  214. }
  215. .info-item {
  216. text-align: center;
  217. }
  218. .info-label {
  219. display: block;
  220. font-size: 12px;
  221. opacity: 0.8;
  222. margin-bottom: 4px;
  223. }
  224. .info-value {
  225. font-weight: 600;
  226. font-size: 16px;
  227. }
  228. .content {
  229. padding: 24px;
  230. }
  231. .word-group {
  232. margin-bottom: 40px;
  233. }
  234. .word-group:first-child {
  235. margin-top: 0;
  236. }
  237. .word-header {
  238. display: flex;
  239. align-items: center;
  240. justify-content: space-between;
  241. margin-bottom: 20px;
  242. padding-bottom: 8px;
  243. border-bottom: 1px solid #f0f0f0;
  244. }
  245. .word-info {
  246. display: flex;
  247. align-items: center;
  248. gap: 12px;
  249. }
  250. .word-name {
  251. font-size: 17px;
  252. font-weight: 600;
  253. color: #1a1a1a;
  254. }
  255. .word-count {
  256. color: #666;
  257. font-size: 13px;
  258. font-weight: 500;
  259. }
  260. .word-count.hot { color: #dc2626; font-weight: 600; }
  261. .word-count.warm { color: #ea580c; font-weight: 600; }
  262. .word-index {
  263. color: #999;
  264. font-size: 12px;
  265. }
  266. .news-item {
  267. margin-bottom: 20px;
  268. padding: 16px 0;
  269. border-bottom: 1px solid #f5f5f5;
  270. position: relative;
  271. display: flex;
  272. gap: 12px;
  273. align-items: center;
  274. }
  275. .news-item:last-child {
  276. border-bottom: none;
  277. }
  278. .news-item.new::after {
  279. content: "NEW";
  280. position: absolute;
  281. top: 12px;
  282. right: 0;
  283. background: #fbbf24;
  284. color: #92400e;
  285. font-size: 9px;
  286. font-weight: 700;
  287. padding: 3px 6px;
  288. border-radius: 4px;
  289. letter-spacing: 0.5px;
  290. }
  291. .news-number {
  292. color: #999;
  293. font-size: 13px;
  294. font-weight: 600;
  295. min-width: 20px;
  296. text-align: center;
  297. flex-shrink: 0;
  298. background: #f8f9fa;
  299. border-radius: 50%;
  300. width: 24px;
  301. height: 24px;
  302. display: flex;
  303. align-items: center;
  304. justify-content: center;
  305. align-self: flex-start;
  306. margin-top: 8px;
  307. position: relative;
  308. cursor: pointer;
  309. transition: background 0.15s, color 0.15s;
  310. }
  311. .news-number .num-text { transition: opacity 0.15s; }
  312. .news-number .copy-icon {
  313. position: absolute;
  314. opacity: 0;
  315. transition: opacity 0.15s;
  316. }
  317. .news-item:hover .news-number .num-text { opacity: 0; }
  318. .news-item:hover .news-number .copy-icon { opacity: 1; }
  319. .news-item:hover .news-number {
  320. background: #eef2ff;
  321. color: #4f46e5;
  322. }
  323. .news-number.copied {
  324. background: #dcfce7 !important;
  325. }
  326. .news-number.copied .num-text { opacity: 0 !important; }
  327. .news-number.copied .copy-icon { opacity: 1 !important; }
  328. body.dark-mode .news-item:hover .news-number {
  329. background: #2a2a5a;
  330. color: #8ab4f8;
  331. }
  332. body.dark-mode .news-number.copied {
  333. background: #14532d !important;
  334. }
  335. .news-content {
  336. flex: 1;
  337. min-width: 0;
  338. padding-right: 40px;
  339. }
  340. .news-item.new .news-content {
  341. padding-right: 50px;
  342. }
  343. .news-header {
  344. display: flex;
  345. align-items: center;
  346. gap: 8px;
  347. margin-bottom: 8px;
  348. flex-wrap: wrap;
  349. }
  350. .source-name {
  351. color: #666;
  352. font-size: 12px;
  353. font-weight: 500;
  354. }
  355. .keyword-tag {
  356. color: #2563eb;
  357. font-size: 12px;
  358. font-weight: 500;
  359. background: #eff6ff;
  360. padding: 2px 6px;
  361. border-radius: 4px;
  362. }
  363. .rank-num {
  364. color: #fff;
  365. background: #6b7280;
  366. font-size: 10px;
  367. font-weight: 700;
  368. padding: 2px 6px;
  369. border-radius: 10px;
  370. min-width: 18px;
  371. text-align: center;
  372. }
  373. .rank-num.top { background: #dc2626; }
  374. .rank-num.high { background: #ea580c; }
  375. .time-info {
  376. color: #999;
  377. font-size: 11px;
  378. }
  379. .count-info {
  380. color: #059669;
  381. font-size: 11px;
  382. font-weight: 500;
  383. }
  384. .news-title {
  385. font-size: 15px;
  386. line-height: 1.4;
  387. color: #1a1a1a;
  388. margin: 0;
  389. }
  390. .news-link {
  391. color: #2563eb;
  392. text-decoration: none;
  393. }
  394. .news-link:hover {
  395. text-decoration: underline;
  396. }
  397. .news-link:visited {
  398. color: #7c3aed;
  399. }
  400. /* 通用区域分割线样式 */
  401. .section-divider {
  402. margin-top: 32px;
  403. padding-top: 24px;
  404. border-top: 2px solid #e5e7eb;
  405. }
  406. /* 热榜统计区样式 */
  407. .hotlist-section {
  408. /* 默认无边框,由 section-divider 动态添加 */
  409. }
  410. .new-section {
  411. margin-top: 40px;
  412. padding-top: 24px;
  413. }
  414. .new-section-title {
  415. color: #1a1a1a;
  416. font-size: 16px;
  417. font-weight: 600;
  418. margin: 0 0 20px 0;
  419. }
  420. .new-source-group {
  421. margin-bottom: 24px;
  422. }
  423. .new-source-title {
  424. color: #666;
  425. font-size: 13px;
  426. font-weight: 500;
  427. margin: 0 0 12px 0;
  428. padding-bottom: 6px;
  429. border-bottom: 1px solid #f5f5f5;
  430. }
  431. .new-item {
  432. display: flex;
  433. align-items: center;
  434. gap: 12px;
  435. padding: 8px 0;
  436. border-bottom: 1px solid #f9f9f9;
  437. }
  438. .new-item:last-child {
  439. border-bottom: none;
  440. }
  441. .new-item-number {
  442. color: #999;
  443. font-size: 12px;
  444. font-weight: 600;
  445. min-width: 18px;
  446. text-align: center;
  447. flex-shrink: 0;
  448. background: #f8f9fa;
  449. border-radius: 50%;
  450. width: 20px;
  451. height: 20px;
  452. display: flex;
  453. align-items: center;
  454. justify-content: center;
  455. }
  456. .new-item-rank {
  457. color: #fff;
  458. background: #6b7280;
  459. font-size: 10px;
  460. font-weight: 700;
  461. padding: 3px 6px;
  462. border-radius: 8px;
  463. min-width: 20px;
  464. text-align: center;
  465. flex-shrink: 0;
  466. }
  467. .new-item-rank.top { background: #dc2626; }
  468. .new-item-rank.high { background: #ea580c; }
  469. .new-item-content {
  470. flex: 1;
  471. min-width: 0;
  472. }
  473. .new-item-title {
  474. font-size: 14px;
  475. line-height: 1.4;
  476. color: #1a1a1a;
  477. margin: 0;
  478. }
  479. .error-section {
  480. background: #fef2f2;
  481. border: 1px solid #fecaca;
  482. border-radius: 8px;
  483. padding: 16px;
  484. margin-bottom: 24px;
  485. }
  486. .error-title {
  487. color: #dc2626;
  488. font-size: 14px;
  489. font-weight: 600;
  490. margin: 0 0 8px 0;
  491. }
  492. .error-list {
  493. list-style: none;
  494. padding: 0;
  495. margin: 0;
  496. }
  497. .error-item {
  498. color: #991b1b;
  499. font-size: 13px;
  500. padding: 2px 0;
  501. font-family: 'SF Mono', Consolas, monospace;
  502. }
  503. .footer {
  504. margin-top: 32px;
  505. padding: 20px 24px;
  506. background: #f8f9fa;
  507. border-top: 1px solid #e5e7eb;
  508. text-align: center;
  509. }
  510. .footer-content {
  511. font-size: 13px;
  512. color: #6b7280;
  513. line-height: 1.6;
  514. }
  515. .footer-link {
  516. color: #4f46e5;
  517. text-decoration: none;
  518. font-weight: 500;
  519. transition: color 0.2s ease;
  520. }
  521. .footer-link:hover {
  522. color: #7c3aed;
  523. text-decoration: underline;
  524. }
  525. .project-name {
  526. font-weight: 600;
  527. color: #374151;
  528. }
  529. @media (max-width: 480px) {
  530. body { padding: 12px; }
  531. .header { padding: 24px 20px; }
  532. .content { padding: 20px; }
  533. .footer { padding: 16px 20px; }
  534. .header-info { grid-template-columns: 1fr; gap: 12px; }
  535. .news-header { gap: 6px; }
  536. .news-content { padding-right: 45px; }
  537. .news-item { gap: 8px; }
  538. .new-item { gap: 8px; }
  539. .news-number { width: 20px; height: 20px; font-size: 12px; }
  540. .save-buttons {
  541. position: static;
  542. margin-bottom: 16px;
  543. display: flex;
  544. gap: 8px;
  545. justify-content: center;
  546. width: 100%;
  547. }
  548. .save-btn-group {
  549. flex: 1;
  550. }
  551. .save-btn {
  552. width: 100%;
  553. border-radius: 6px 0 0 6px;
  554. }
  555. }
  556. /* RSS 订阅内容样式 */
  557. .rss-section {
  558. margin-top: 32px;
  559. padding-top: 24px;
  560. }
  561. .rss-section-header {
  562. display: flex;
  563. align-items: center;
  564. justify-content: space-between;
  565. margin-bottom: 20px;
  566. }
  567. .rss-section-title {
  568. font-size: 18px;
  569. font-weight: 600;
  570. color: #059669;
  571. }
  572. .rss-section-count {
  573. color: #6b7280;
  574. font-size: 14px;
  575. }
  576. .feed-group {
  577. margin-bottom: 24px;
  578. }
  579. .feed-group:last-child {
  580. margin-bottom: 0;
  581. }
  582. .feed-header {
  583. display: flex;
  584. align-items: center;
  585. justify-content: space-between;
  586. margin-bottom: 12px;
  587. padding-bottom: 8px;
  588. border-bottom: 2px solid #10b981;
  589. }
  590. .feed-name {
  591. font-size: 15px;
  592. font-weight: 600;
  593. color: #059669;
  594. }
  595. .feed-count {
  596. color: #666;
  597. font-size: 13px;
  598. font-weight: 500;
  599. }
  600. .rss-item {
  601. margin-bottom: 12px;
  602. padding: 14px;
  603. background: #f0fdf4;
  604. border-radius: 8px;
  605. border-left: 3px solid #10b981;
  606. }
  607. .rss-item:last-child {
  608. margin-bottom: 0;
  609. }
  610. .rss-meta {
  611. display: flex;
  612. align-items: center;
  613. gap: 12px;
  614. margin-bottom: 6px;
  615. flex-wrap: wrap;
  616. }
  617. .rss-time {
  618. color: #6b7280;
  619. font-size: 12px;
  620. }
  621. .rss-author {
  622. color: #059669;
  623. font-size: 12px;
  624. font-weight: 500;
  625. }
  626. .rss-title {
  627. font-size: 14px;
  628. line-height: 1.5;
  629. margin-bottom: 6px;
  630. }
  631. .rss-link {
  632. color: #1f2937;
  633. text-decoration: none;
  634. font-weight: 500;
  635. }
  636. .rss-link:hover {
  637. color: #059669;
  638. text-decoration: underline;
  639. }
  640. .rss-summary {
  641. font-size: 13px;
  642. color: #6b7280;
  643. line-height: 1.5;
  644. margin: 0;
  645. display: -webkit-box;
  646. -webkit-line-clamp: 2;
  647. -webkit-box-orient: vertical;
  648. overflow: hidden;
  649. }
  650. /* 独立展示区样式 - 复用热点词汇统计区样式 */
  651. .standalone-section {
  652. margin-top: 32px;
  653. padding-top: 24px;
  654. }
  655. .standalone-section-header {
  656. display: flex;
  657. align-items: center;
  658. justify-content: space-between;
  659. margin-bottom: 20px;
  660. }
  661. .standalone-section-title {
  662. font-size: 18px;
  663. font-weight: 600;
  664. color: #059669;
  665. }
  666. .standalone-section-count {
  667. color: #6b7280;
  668. font-size: 14px;
  669. }
  670. .standalone-group {
  671. margin-bottom: 40px;
  672. }
  673. .standalone-group:last-child {
  674. margin-bottom: 0;
  675. }
  676. .standalone-header {
  677. display: flex;
  678. align-items: center;
  679. justify-content: space-between;
  680. margin-bottom: 20px;
  681. padding-bottom: 8px;
  682. border-bottom: 1px solid #f0f0f0;
  683. }
  684. .standalone-name {
  685. font-size: 17px;
  686. font-weight: 600;
  687. color: #1a1a1a;
  688. }
  689. .standalone-count {
  690. color: #666;
  691. font-size: 13px;
  692. font-weight: 500;
  693. }
  694. /* AI 分析区块样式 */
  695. .ai-section {
  696. margin-top: 32px;
  697. padding: 24px;
  698. background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  699. border-radius: 12px;
  700. border: 1px solid #bae6fd;
  701. }
  702. .ai-section-header {
  703. display: flex;
  704. align-items: center;
  705. gap: 10px;
  706. margin-bottom: 20px;
  707. }
  708. .ai-section-title {
  709. font-size: 18px;
  710. font-weight: 600;
  711. color: #0369a1;
  712. }
  713. .ai-section-badge {
  714. background: #0ea5e9;
  715. color: white;
  716. font-size: 11px;
  717. font-weight: 600;
  718. padding: 3px 8px;
  719. border-radius: 4px;
  720. }
  721. .ai-block {
  722. margin-bottom: 16px;
  723. padding: 16px;
  724. background: white;
  725. border-radius: 8px;
  726. box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  727. }
  728. .ai-block:last-child {
  729. margin-bottom: 0;
  730. }
  731. .ai-block-title {
  732. font-size: 14px;
  733. font-weight: 600;
  734. color: #0369a1;
  735. margin-bottom: 8px;
  736. }
  737. .ai-block-content {
  738. font-size: 14px;
  739. line-height: 1.6;
  740. color: #334155;
  741. white-space: pre-wrap;
  742. }
  743. .ai-error {
  744. padding: 16px;
  745. background: #fef2f2;
  746. border: 1px solid #fecaca;
  747. border-radius: 8px;
  748. color: #991b1b;
  749. font-size: 14px;
  750. }
  751. .ai-info {
  752. padding: 16px;
  753. background: #f0f9ff;
  754. border: 1px solid #bae6fd;
  755. border-radius: 8px;
  756. color: #0369a1;
  757. font-size: 14px;
  758. }
  759. /* ===== 浏览器增强样式(渐进增强,邮件客户端无影响) ===== */
  760. /* 宽屏模式 - 基础 */
  761. body.wide-mode .container { max-width: 1200px; }
  762. body.wide-mode .header-info { grid-template-columns: repeat(4, 1fr); }
  763. body.wide-mode .content { padding: 32px 40px; }
  764. /* 宽屏模式 - RSS feed-group 两列 */
  765. body.wide-mode .rss-feeds-grid {
  766. display: grid;
  767. grid-template-columns: 1fr 1fr;
  768. gap: 24px;
  769. }
  770. body.wide-mode .feed-group { margin-bottom: 0; }
  771. /* 宽屏模式 - AI 分析区两列网格 */
  772. body.wide-mode .ai-section .ai-blocks-grid {
  773. display: grid;
  774. grid-template-columns: 1fr 1fr;
  775. gap: 16px;
  776. }
  777. body.wide-mode .ai-block { margin-bottom: 0; }
  778. /* 宽屏模式 - 新增热点多列 */
  779. body.wide-mode .new-section .new-sources-grid {
  780. display: grid;
  781. grid-template-columns: 1fr 1fr;
  782. gap: 24px;
  783. }
  784. body.wide-mode .new-source-group { margin-bottom: 0; }
  785. /* 宽屏模式 - 独立展示区多列 */
  786. body.wide-mode .standalone-section .standalone-groups-grid {
  787. display: grid;
  788. grid-template-columns: 1fr 1fr;
  789. gap: 24px;
  790. }
  791. body.wide-mode .standalone-group { margin-bottom: 0; }
  792. /* Tab 栏 */
  793. .tab-bar {
  794. display: none;
  795. overflow-x: auto;
  796. white-space: nowrap;
  797. padding: 8px 0 12px 0;
  798. margin-bottom: 20px;
  799. border-bottom: 2px solid #e5e7eb;
  800. -webkit-overflow-scrolling: touch;
  801. scrollbar-width: thin;
  802. position: sticky;
  803. top: 0;
  804. background: white;
  805. z-index: 10;
  806. gap: 4px;
  807. }
  808. body.wide-mode .tab-bar { display: flex; }
  809. body.wide-mode .tab-bar.tab-hidden { display: none; }
  810. .tab-btn {
  811. display: inline-flex;
  812. align-items: center;
  813. gap: 6px;
  814. padding: 8px 16px;
  815. border: none;
  816. background: #f3f4f6;
  817. color: #6b7280;
  818. border-radius: 8px 8px 0 0;
  819. cursor: pointer;
  820. font-size: 13px;
  821. font-weight: 500;
  822. white-space: nowrap;
  823. transition: all 0.2s ease;
  824. flex-shrink: 0;
  825. }
  826. .tab-btn:hover { background: #e5e7eb; color: #374151; }
  827. .tab-btn.active { background: #4f46e5; color: white; }
  828. .tab-count {
  829. font-size: 11px;
  830. background: rgba(0,0,0,0.1);
  831. padding: 1px 6px;
  832. border-radius: 10px;
  833. }
  834. .tab-btn.active .tab-count { background: rgba(255,255,255,0.3); }
  835. .tab-bar::-webkit-scrollbar { height: 4px; }
  836. .tab-bar::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 2px; }
  837. .tab-bar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 2px; }
  838. /* 搜索栏 */
  839. .search-bar { display: none; padding: 0 0 16px 0; }
  840. .search-input {
  841. width: 100%;
  842. padding: 10px 16px;
  843. border: 1px solid #e5e7eb;
  844. border-radius: 8px;
  845. font-size: 14px;
  846. outline: none;
  847. transition: border-color 0.2s;
  848. box-sizing: border-box;
  849. }
  850. .search-input:focus { border-color: #4f46e5; box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }
  851. .search-input::placeholder { color: #9ca3af; }
  852. /* 右下角悬浮工具栏 */
  853. .fab-bar {
  854. position: fixed;
  855. bottom: 24px;
  856. right: 24px;
  857. display: flex;
  858. flex-direction: column;
  859. gap: 8px;
  860. z-index: 100;
  861. opacity: 0;
  862. transform: translateY(10px);
  863. transition: opacity 0.3s, transform 0.3s;
  864. pointer-events: none;
  865. }
  866. .fab-bar.visible {
  867. opacity: 1;
  868. transform: translateY(0);
  869. pointer-events: auto;
  870. }
  871. .fab-btn {
  872. width: 40px;
  873. height: 40px;
  874. border-radius: 50%;
  875. background: #4f46e5;
  876. color: white;
  877. border: none;
  878. cursor: pointer;
  879. font-size: 16px;
  880. box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  881. transition: transform 0.2s, background 0.2s;
  882. display: flex;
  883. align-items: center;
  884. justify-content: center;
  885. position: relative;
  886. }
  887. .fab-btn:hover { transform: scale(1.1); background: #4338ca; }
  888. body.dark-mode .fab-btn { background: #533483; }
  889. body.dark-mode .fab-btn:hover { background: #6d28d9; }
  890. /* 快捷键 tooltip */
  891. .fab-tooltip {
  892. position: absolute;
  893. bottom: 0;
  894. right: 52px;
  895. background: rgba(30, 30, 50, 0.92);
  896. backdrop-filter: blur(12px);
  897. color: white;
  898. border-radius: 10px;
  899. padding: 12px 16px;
  900. white-space: nowrap;
  901. font-size: 12px;
  902. line-height: 1.8;
  903. box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  904. border: 1px solid rgba(255,255,255,0.1);
  905. opacity: 0;
  906. visibility: hidden;
  907. transform: translateY(6px);
  908. transition: all 0.2s ease;
  909. pointer-events: none;
  910. }
  911. .fab-btn:hover .fab-tooltip,
  912. .fab-btn.show-tip .fab-tooltip {
  913. opacity: 1;
  914. visibility: visible;
  915. transform: translateY(0);
  916. pointer-events: auto;
  917. }
  918. .fab-tooltip .tip-row {
  919. display: flex;
  920. justify-content: space-between;
  921. gap: 16px;
  922. align-items: center;
  923. }
  924. .fab-tooltip .tip-key {
  925. background: rgba(255,255,255,0.15);
  926. border-radius: 3px;
  927. padding: 1px 6px;
  928. font-family: monospace;
  929. font-size: 11px;
  930. margin-left: 8px;
  931. }
  932. /* 折叠/展开 */
  933. .collapse-icon {
  934. display: none;
  935. margin-right: 6px;
  936. font-size: 12px;
  937. color: #9ca3af;
  938. transition: transform 0.2s;
  939. user-select: none;
  940. }
  941. .word-header.collapsible { cursor: pointer; }
  942. .word-header.collapsible .collapse-icon { display: inline; }
  943. .word-header.collapsible:hover {
  944. background: #f9fafb;
  945. border-radius: 6px;
  946. margin: 0 -8px 20px -8px;
  947. padding: 8px;
  948. }
  949. .word-group.collapsed .news-item { display: none; }
  950. .word-group.collapsed .collapse-icon { transform: rotate(-90deg); }
  951. /* Tab 切换动画 */
  952. body.wide-mode .word-group[data-tab-index] { animation: tabFadeIn 0.2s ease; }
  953. @keyframes tabFadeIn {
  954. from { opacity: 0; transform: translateY(8px); }
  955. to { opacity: 1; transform: translateY(0); }
  956. }
  957. /* 宽屏切换按钮 */
  958. .toggle-wide-btn {
  959. background: rgba(255, 255, 255, 0.2);
  960. border: 1px solid rgba(255, 255, 255, 0.3);
  961. color: white;
  962. padding: 10px 14px;
  963. border-radius: 6px;
  964. cursor: pointer;
  965. font-size: 15px;
  966. transition: all 0.2s ease;
  967. backdrop-filter: blur(10px);
  968. line-height: 1;
  969. min-height: 38px;
  970. }
  971. .toggle-wide-btn:hover {
  972. background: rgba(255, 255, 255, 0.3);
  973. border-color: rgba(255, 255, 255, 0.5);
  974. transform: translateY(-1px);
  975. }
  976. /* 暗色模式 */
  977. body.dark-mode {
  978. background: #1a1a2e;
  979. color: #e0e0e0;
  980. }
  981. body.dark-mode .container {
  982. background: #16213e;
  983. box-shadow: 0 2px 16px rgba(0,0,0,0.3);
  984. }
  985. body.dark-mode .header {
  986. background: linear-gradient(135deg, #0f3460 0%, #533483 100%);
  987. }
  988. body.dark-mode .content {
  989. background: #16213e;
  990. }
  991. body.dark-mode .word-header {
  992. border-bottom-color: #2a2a4a;
  993. }
  994. body.dark-mode .word-header.collapsible:hover {
  995. background: #1a1a3e;
  996. }
  997. body.dark-mode .news-item {
  998. border-bottom-color: #2a2a4a;
  999. }
  1000. body.dark-mode .news-title a {
  1001. color: #8ab4f8;
  1002. }
  1003. body.dark-mode .news-title a:visited {
  1004. color: #c58af9;
  1005. }
  1006. body.dark-mode .news-meta {
  1007. color: #888;
  1008. }
  1009. body.dark-mode .tab-bar {
  1010. background: #16213e;
  1011. border-bottom-color: #2a2a4a;
  1012. }
  1013. body.dark-mode .tab-btn {
  1014. color: #aaa;
  1015. }
  1016. body.dark-mode .tab-btn.active {
  1017. color: #8ab4f8;
  1018. border-bottom-color: #8ab4f8;
  1019. }
  1020. body.dark-mode .tab-btn:hover {
  1021. color: #ccc;
  1022. background: rgba(255,255,255,0.05);
  1023. }
  1024. body.dark-mode .search-input {
  1025. background: #1a1a3e;
  1026. border-color: #2a2a4a;
  1027. color: #e0e0e0;
  1028. }
  1029. body.dark-mode .search-input:focus {
  1030. border-color: #8ab4f8;
  1031. }
  1032. /* dark fab-btn 已在 .fab-btn 中处理 */
  1033. body.dark-mode .footer {
  1034. background: #0f3460;
  1035. color: rgba(255,255,255,0.7);
  1036. }
  1037. body.dark-mode .rss-item,
  1038. body.dark-mode .new-item,
  1039. body.dark-mode .standalone-item {
  1040. border-bottom-color: #2a2a4a;
  1041. }
  1042. body.dark-mode .rss-title a,
  1043. body.dark-mode .new-item a,
  1044. body.dark-mode .standalone-item a {
  1045. color: #8ab4f8;
  1046. }
  1047. body.dark-mode .ai-block {
  1048. background: #1a1a3e;
  1049. border-color: #2a2a4a;
  1050. }
  1051. body.dark-mode .info-value {
  1052. color: white;
  1053. }
  1054. /* 暗色模式切换按钮 */
  1055. .toggle-dark-btn {
  1056. background: rgba(255, 255, 255, 0.2);
  1057. border: 1px solid rgba(255, 255, 255, 0.3);
  1058. color: white;
  1059. padding: 10px 14px;
  1060. border-radius: 6px;
  1061. cursor: pointer;
  1062. font-size: 15px;
  1063. transition: all 0.2s ease;
  1064. backdrop-filter: blur(10px);
  1065. line-height: 1;
  1066. min-height: 38px;
  1067. }
  1068. .toggle-dark-btn:hover {
  1069. background: rgba(255, 255, 255, 0.3);
  1070. border-color: rgba(255, 255, 255, 0.5);
  1071. transform: translateY(-1px);
  1072. }
  1073. /* 快捷键面板已集成到 fab-tooltip */
  1074. /* 阅读进度条 */
  1075. .reading-progress {
  1076. position: fixed;
  1077. top: 0; left: 0;
  1078. width: 0;
  1079. height: 3px;
  1080. background: linear-gradient(90deg, #4f46e5, #7c3aed);
  1081. z-index: 9999;
  1082. transition: width 0.1s linear;
  1083. }
  1084. body.dark-mode .reading-progress {
  1085. background: linear-gradient(90deg, #8ab4f8, #c58af9);
  1086. }
  1087. /* 复制按钮样式已集成到 .news-number */
  1088. /* 新上榜标记 */
  1089. .badge-new {
  1090. display: inline-block;
  1091. background: linear-gradient(135deg, #f43f5e, #ec4899);
  1092. color: white;
  1093. font-size: 10px;
  1094. font-weight: 600;
  1095. padding: 1px 6px;
  1096. border-radius: 3px;
  1097. margin-left: 6px;
  1098. vertical-align: middle;
  1099. letter-spacing: 0.5px;
  1100. }
  1101. body.dark-mode .badge-new {
  1102. background: linear-gradient(135deg, #be185d, #9333ea);
  1103. }
  1104. </style>
  1105. </head>
  1106. <body>
  1107. <div class="reading-progress"></div>
  1108. <div class="container">
  1109. <div class="header">
  1110. <div class="header-watermark">TrendRadar</div>
  1111. <div class="save-buttons">
  1112. <button class="toggle-wide-btn" onclick="toggleWideMode()" title="切换宽屏/窄屏">⛶</button>
  1113. <button class="toggle-dark-btn" onclick="toggleDarkMode()" title="切换暗色/亮色">☽</button>
  1114. <div class="save-btn-group">
  1115. <button class="save-btn" onclick="saveAsImage()">导出</button>
  1116. <button class="save-dropdown-trigger">▾</button>
  1117. <div class="save-dropdown-menu">
  1118. <button class="save-dropdown-item" onclick="saveAsImage()"><svg class="dropdown-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="2" y="2" width="12" height="12" rx="2"/><circle cx="8" cy="7.5" r="2.5"/><path d="M12 4h.01"/></svg>整页截图</button>
  1119. <button class="save-dropdown-item" onclick="saveAsMultipleImages()"><svg class="dropdown-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="1" y="4" width="10" height="10" rx="1.5"/><path d="M5 4V2.5A1.5 1.5 0 016.5 1h7A1.5 1.5 0 0115 2.5v7a1.5 1.5 0 01-1.5 1.5H12"/></svg>分段截图</button>
  1120. <button class="save-dropdown-item" onclick="saveAsMarkdown()"><svg class="dropdown-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2.5 2h11A1.5 1.5 0 0115 3.5v9a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12.5v-9A1.5 1.5 0 012.5 2z"/><path d="M4 11V5l2.5 3L9 5v6"/><path d="M11.5 8v3m0 0l-1.5-2m1.5 2l1.5-2"/></svg>Markdown</button>
  1121. </div>
  1122. </div>
  1123. </div>
  1124. <div class="header-title">热点新闻分析</div>
  1125. <div class="header-info">
  1126. <div class="info-item">
  1127. <span class="info-label">报告类型</span>
  1128. <span class="info-value">"""
  1129. # 处理报告类型显示(根据 mode 直接显示)
  1130. if mode == "current":
  1131. html += "当前榜单"
  1132. elif mode == "incremental":
  1133. html += "增量分析"
  1134. else:
  1135. html += "全天汇总"
  1136. html += """</span>
  1137. </div>
  1138. <div class="info-item">
  1139. <span class="info-label">新闻总数</span>
  1140. <span class="info-value">"""
  1141. html += f"{total_titles} 条"
  1142. # 计算筛选后的热点新闻数量
  1143. hot_news_count = sum(len(stat["titles"]) for stat in report_data["stats"])
  1144. html += """</span>
  1145. </div>
  1146. <div class="info-item">
  1147. <span class="info-label">热点新闻</span>
  1148. <span class="info-value">"""
  1149. html += f"{hot_news_count} 条"
  1150. html += """</span>
  1151. </div>
  1152. <div class="info-item">
  1153. <span class="info-label">生成时间</span>
  1154. <span class="info-value">"""
  1155. # 使用提供的时间函数或默认 datetime.now
  1156. if get_time_func:
  1157. now = get_time_func()
  1158. else:
  1159. now = datetime.now()
  1160. html += now.strftime("%m-%d %H:%M")
  1161. html += """</span>
  1162. </div>
  1163. </div>
  1164. </div>
  1165. <div class="content">
  1166. <div class="search-bar">
  1167. <input type="text" class="search-input" placeholder="搜索新闻标题..." oninput="handleSearch(this.value)">
  1168. </div>"""
  1169. # 处理失败ID错误信息
  1170. if report_data["failed_ids"]:
  1171. html += """
  1172. <div class="error-section">
  1173. <div class="error-title">⚠️ 请求失败的平台</div>
  1174. <ul class="error-list">"""
  1175. for id_value in report_data["failed_ids"]:
  1176. html += f'<li class="error-item">{html_escape(id_value)}</li>'
  1177. html += """
  1178. </ul>
  1179. </div>"""
  1180. # 生成热点词汇统计部分的HTML
  1181. stats_html = ""
  1182. tab_bar_html = ""
  1183. if report_data["stats"]:
  1184. total_count = len(report_data["stats"])
  1185. # 生成 Tab 栏 HTML
  1186. tab_bar_html = '<div class="tab-bar">'
  1187. for tab_i, tab_stat in enumerate(report_data["stats"]):
  1188. escaped_tab_word = html_escape(tab_stat["word"])
  1189. tab_count = tab_stat["count"]
  1190. tab_bar_html += f'<button class="tab-btn" data-tab-index="{tab_i}">{escaped_tab_word}<span class="tab-count">{tab_count}</span></button>'
  1191. tab_bar_html += '<button class="tab-btn" data-tab-index="all">全部</button>'
  1192. tab_bar_html += '</div>'
  1193. for i, stat in enumerate(report_data["stats"], 1):
  1194. count = stat["count"]
  1195. # 确定热度等级
  1196. if count >= 10:
  1197. count_class = "hot"
  1198. elif count >= 5:
  1199. count_class = "warm"
  1200. else:
  1201. count_class = ""
  1202. escaped_word = html_escape(stat["word"])
  1203. stats_html += f"""
  1204. <div class="word-group" data-tab-index="{i - 1}">
  1205. <div class="word-header">
  1206. <div class="word-info">
  1207. <div class="word-name">{escaped_word}</div>
  1208. <div class="word-count {count_class}">{count} 条</div>
  1209. </div>
  1210. <div class="word-index"><span class="collapse-icon">▼</span>{i}/{total_count}</div>
  1211. </div>"""
  1212. # 处理每个词组下的新闻标题,给每条新闻标上序号
  1213. for j, title_data in enumerate(stat["titles"], 1):
  1214. is_new = title_data.get("is_new", False)
  1215. new_class = "new" if is_new else ""
  1216. stats_html += f"""
  1217. <div class="news-item {new_class}">
  1218. <div class="news-number">{j}</div>
  1219. <div class="news-content">
  1220. <div class="news-header">"""
  1221. # 根据 display_mode 决定显示来源还是关键词
  1222. if display_mode == "keyword":
  1223. # keyword 模式:显示来源
  1224. stats_html += f'<span class="source-name">{html_escape(title_data["source_name"])}</span>'
  1225. else:
  1226. # platform 模式:显示关键词
  1227. matched_keyword = title_data.get("matched_keyword", "")
  1228. if matched_keyword:
  1229. stats_html += f'<span class="keyword-tag">[{html_escape(matched_keyword)}]</span>'
  1230. # 处理排名显示
  1231. ranks = title_data.get("ranks", [])
  1232. if ranks:
  1233. min_rank = min(ranks)
  1234. max_rank = max(ranks)
  1235. rank_threshold = title_data.get("rank_threshold", 10)
  1236. # 确定排名等级
  1237. if min_rank <= 3:
  1238. rank_class = "top"
  1239. elif min_rank <= rank_threshold:
  1240. rank_class = "high"
  1241. else:
  1242. rank_class = ""
  1243. if min_rank == max_rank:
  1244. rank_text = str(min_rank)
  1245. else:
  1246. rank_text = f"{min_rank}-{max_rank}"
  1247. stats_html += f'<span class="rank-num {rank_class}">{rank_text}</span>'
  1248. # 处理时间显示
  1249. time_display = title_data.get("time_display", "")
  1250. if time_display:
  1251. # 简化时间显示格式,将波浪线替换为~
  1252. simplified_time = (
  1253. time_display.replace(" ~ ", "~")
  1254. .replace("[", "")
  1255. .replace("]", "")
  1256. )
  1257. stats_html += (
  1258. f'<span class="time-info">{html_escape(simplified_time)}</span>'
  1259. )
  1260. # 处理出现次数
  1261. count_info = title_data.get("count", 1)
  1262. if count_info > 1:
  1263. stats_html += f'<span class="count-info">{count_info}次</span>'
  1264. stats_html += """
  1265. </div>
  1266. <div class="news-title">"""
  1267. # 处理标题和链接
  1268. escaped_title = html_escape(title_data["title"])
  1269. link_url = title_data.get("mobile_url") or title_data.get("url", "")
  1270. if link_url:
  1271. escaped_url = html_escape(link_url)
  1272. stats_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1273. else:
  1274. stats_html += escaped_title
  1275. stats_html += """
  1276. </div>
  1277. </div>
  1278. </div>"""
  1279. stats_html += """
  1280. </div>"""
  1281. # 给热榜统计添加外层包装
  1282. if stats_html:
  1283. stats_html = f"""
  1284. <div class="hotlist-section">{tab_bar_html}{stats_html}
  1285. </div>"""
  1286. # 生成新增新闻区域的HTML
  1287. new_titles_html = ""
  1288. if show_new_section and report_data["new_titles"]:
  1289. new_titles_html += f"""
  1290. <div class="new-section">
  1291. <div class="new-section-title">本次新增热点 (共 {report_data['total_new_count']} 条)</div>
  1292. <div class="new-sources-grid">"""
  1293. for source_data in report_data["new_titles"]:
  1294. escaped_source = html_escape(source_data["source_name"])
  1295. titles_count = len(source_data["titles"])
  1296. new_titles_html += f"""
  1297. <div class="new-source-group">
  1298. <div class="new-source-title">{escaped_source} · {titles_count}条</div>"""
  1299. # 为新增新闻也添加序号
  1300. for idx, title_data in enumerate(source_data["titles"], 1):
  1301. ranks = title_data.get("ranks", [])
  1302. # 处理新增新闻的排名显示
  1303. rank_class = ""
  1304. if ranks:
  1305. min_rank = min(ranks)
  1306. if min_rank <= 3:
  1307. rank_class = "top"
  1308. elif min_rank <= title_data.get("rank_threshold", 10):
  1309. rank_class = "high"
  1310. if len(ranks) == 1:
  1311. rank_text = str(ranks[0])
  1312. else:
  1313. rank_text = f"{min(ranks)}-{max(ranks)}"
  1314. else:
  1315. rank_text = "?"
  1316. new_titles_html += f"""
  1317. <div class="new-item">
  1318. <div class="new-item-number">{idx}</div>
  1319. <div class="new-item-rank {rank_class}">{rank_text}</div>
  1320. <div class="new-item-content">
  1321. <div class="new-item-title">"""
  1322. # 处理新增新闻的链接
  1323. escaped_title = html_escape(title_data["title"])
  1324. link_url = title_data.get("mobile_url") or title_data.get("url", "")
  1325. if link_url:
  1326. escaped_url = html_escape(link_url)
  1327. new_titles_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1328. else:
  1329. new_titles_html += escaped_title
  1330. new_titles_html += """
  1331. </div>
  1332. </div>
  1333. </div>"""
  1334. new_titles_html += """
  1335. </div>"""
  1336. new_titles_html += """
  1337. </div>
  1338. </div>"""
  1339. # 生成 RSS 统计内容
  1340. def render_rss_stats_html(stats: List[Dict], title: str = "RSS 订阅更新") -> str:
  1341. """渲染 RSS 统计区块 HTML
  1342. Args:
  1343. stats: RSS 分组统计列表,格式与热榜一致:
  1344. [
  1345. {
  1346. "word": "关键词",
  1347. "count": 5,
  1348. "titles": [
  1349. {
  1350. "title": "标题",
  1351. "source_name": "Feed 名称",
  1352. "time_display": "12-29 08:20",
  1353. "url": "...",
  1354. "is_new": True/False
  1355. }
  1356. ]
  1357. }
  1358. ]
  1359. title: 区块标题
  1360. Returns:
  1361. 渲染后的 HTML 字符串
  1362. """
  1363. if not stats:
  1364. return ""
  1365. # 计算总条目数
  1366. total_count = sum(stat.get("count", 0) for stat in stats)
  1367. if total_count == 0:
  1368. return ""
  1369. rss_html = f"""
  1370. <div class="rss-section">
  1371. <div class="rss-section-header">
  1372. <div class="rss-section-title">{title}</div>
  1373. <div class="rss-section-count">{total_count} 条</div>
  1374. </div>
  1375. <div class="rss-feeds-grid">"""
  1376. # 按关键词分组渲染(与热榜格式一致)
  1377. for stat in stats:
  1378. keyword = stat.get("word", "")
  1379. titles = stat.get("titles", [])
  1380. if not titles:
  1381. continue
  1382. keyword_count = len(titles)
  1383. rss_html += f"""
  1384. <div class="feed-group">
  1385. <div class="feed-header">
  1386. <div class="feed-name">{html_escape(keyword)}</div>
  1387. <div class="feed-count">{keyword_count} 条</div>
  1388. </div>"""
  1389. for title_data in titles:
  1390. item_title = title_data.get("title", "")
  1391. url = title_data.get("url", "")
  1392. time_display = title_data.get("time_display", "")
  1393. source_name = title_data.get("source_name", "")
  1394. is_new = title_data.get("is_new", False)
  1395. rss_html += """
  1396. <div class="rss-item">
  1397. <div class="rss-meta">"""
  1398. if time_display:
  1399. rss_html += f'<span class="rss-time">{html_escape(time_display)}</span>'
  1400. if source_name:
  1401. rss_html += f'<span class="rss-author">{html_escape(source_name)}</span>'
  1402. if is_new:
  1403. rss_html += '<span class="rss-author" style="color: #dc2626;">NEW</span>'
  1404. rss_html += """
  1405. </div>
  1406. <div class="rss-title">"""
  1407. escaped_title = html_escape(item_title)
  1408. if url:
  1409. escaped_url = html_escape(url)
  1410. rss_html += f'<a href="{escaped_url}" target="_blank" class="rss-link">{escaped_title}</a>'
  1411. else:
  1412. rss_html += escaped_title
  1413. rss_html += """
  1414. </div>
  1415. </div>"""
  1416. rss_html += """
  1417. </div>"""
  1418. rss_html += """
  1419. </div>
  1420. </div>"""
  1421. return rss_html
  1422. # 生成独立展示区内容
  1423. def render_standalone_html(data: Optional[Dict]) -> str:
  1424. """渲染独立展示区 HTML(复用热点词汇统计区样式)
  1425. Args:
  1426. data: 独立展示数据,格式:
  1427. {
  1428. "platforms": [
  1429. {
  1430. "id": "zhihu",
  1431. "name": "知乎热榜",
  1432. "items": [
  1433. {
  1434. "title": "标题",
  1435. "url": "链接",
  1436. "rank": 1,
  1437. "ranks": [1, 2, 1],
  1438. "first_time": "08:00",
  1439. "last_time": "12:30",
  1440. "count": 3,
  1441. }
  1442. ]
  1443. }
  1444. ],
  1445. "rss_feeds": [
  1446. {
  1447. "id": "hacker-news",
  1448. "name": "Hacker News",
  1449. "items": [
  1450. {
  1451. "title": "标题",
  1452. "url": "链接",
  1453. "published_at": "2025-01-07T08:00:00",
  1454. "author": "作者",
  1455. }
  1456. ]
  1457. }
  1458. ]
  1459. }
  1460. Returns:
  1461. 渲染后的 HTML 字符串
  1462. """
  1463. if not data:
  1464. return ""
  1465. platforms = data.get("platforms", [])
  1466. rss_feeds = data.get("rss_feeds", [])
  1467. if not platforms and not rss_feeds:
  1468. return ""
  1469. # 计算总条目数
  1470. total_platform_items = sum(len(p.get("items", [])) for p in platforms)
  1471. total_rss_items = sum(len(f.get("items", [])) for f in rss_feeds)
  1472. total_count = total_platform_items + total_rss_items
  1473. if total_count == 0:
  1474. return ""
  1475. # 收集所有分组信息用于生成 tab
  1476. all_groups = []
  1477. for p in platforms:
  1478. items = p.get("items", [])
  1479. if items:
  1480. all_groups.append({"name": p.get("name", p.get("id", "")), "count": len(items)})
  1481. for f in rss_feeds:
  1482. items = f.get("items", [])
  1483. if items:
  1484. all_groups.append({"name": f.get("name", f.get("id", "")), "count": len(items)})
  1485. standalone_html = f"""
  1486. <div class="standalone-section">
  1487. <div class="standalone-section-header">
  1488. <div class="standalone-section-title">独立展示区</div>
  1489. <div class="standalone-section-count">{total_count} 条</div>
  1490. </div>"""
  1491. # 生成 tab 栏(2+ 分组时)
  1492. if len(all_groups) >= 2:
  1493. standalone_html += """
  1494. <div class="tab-bar standalone-tab-bar">"""
  1495. for idx, g in enumerate(all_groups):
  1496. active = ' active' if idx == 0 else ''
  1497. standalone_html += f"""
  1498. <button class="tab-btn{active}" data-standalone-tab="{idx}">{html_escape(g["name"])}<span class="tab-count">{g["count"]}</span></button>"""
  1499. standalone_html += f"""
  1500. <button class="tab-btn" data-standalone-tab="all">全部<span class="tab-count">{total_count}</span></button>
  1501. </div>"""
  1502. standalone_html += """
  1503. <div class="standalone-groups-grid">"""
  1504. group_idx = 0
  1505. # 渲染热榜平台(复用 word-group 结构)
  1506. for platform in platforms:
  1507. platform_name = platform.get("name", platform.get("id", ""))
  1508. items = platform.get("items", [])
  1509. if not items:
  1510. continue
  1511. standalone_html += f"""
  1512. <div class="standalone-group" data-standalone-tab="{group_idx}">
  1513. <div class="standalone-header">
  1514. <div class="standalone-name">{html_escape(platform_name)}</div>
  1515. <div class="standalone-count">{len(items)} 条</div>
  1516. </div>"""
  1517. # 渲染每个条目(复用 news-item 结构)
  1518. for j, item in enumerate(items, 1):
  1519. title = item.get("title", "")
  1520. url = item.get("url", "") or item.get("mobileUrl", "")
  1521. rank = item.get("rank", 0)
  1522. ranks = item.get("ranks", [])
  1523. first_time = item.get("first_time", "")
  1524. last_time = item.get("last_time", "")
  1525. count = item.get("count", 1)
  1526. standalone_html += f"""
  1527. <div class="news-item">
  1528. <div class="news-number">{j}</div>
  1529. <div class="news-content">
  1530. <div class="news-header">"""
  1531. # 排名显示(复用 rank-num 样式,无 # 前缀)
  1532. if ranks:
  1533. min_rank = min(ranks)
  1534. max_rank = max(ranks)
  1535. # 确定排名等级
  1536. if min_rank <= 3:
  1537. rank_class = "top"
  1538. elif min_rank <= 10:
  1539. rank_class = "high"
  1540. else:
  1541. rank_class = ""
  1542. if min_rank == max_rank:
  1543. rank_text = str(min_rank)
  1544. else:
  1545. rank_text = f"{min_rank}-{max_rank}"
  1546. standalone_html += f'<span class="rank-num {rank_class}">{rank_text}</span>'
  1547. elif rank > 0:
  1548. if rank <= 3:
  1549. rank_class = "top"
  1550. elif rank <= 10:
  1551. rank_class = "high"
  1552. else:
  1553. rank_class = ""
  1554. standalone_html += f'<span class="rank-num {rank_class}">{rank}</span>'
  1555. # 时间显示(复用 time-info 样式,将 HH-MM 转换为 HH:MM)
  1556. if first_time and last_time and first_time != last_time:
  1557. first_time_display = convert_time_for_display(first_time)
  1558. last_time_display = convert_time_for_display(last_time)
  1559. standalone_html += f'<span class="time-info">{html_escape(first_time_display)}~{html_escape(last_time_display)}</span>'
  1560. elif first_time:
  1561. first_time_display = convert_time_for_display(first_time)
  1562. standalone_html += f'<span class="time-info">{html_escape(first_time_display)}</span>'
  1563. # 出现次数(复用 count-info 样式)
  1564. if count > 1:
  1565. standalone_html += f'<span class="count-info">{count}次</span>'
  1566. standalone_html += """
  1567. </div>
  1568. <div class="news-title">"""
  1569. # 标题和链接(复用 news-link 样式)
  1570. escaped_title = html_escape(title)
  1571. if url:
  1572. escaped_url = html_escape(url)
  1573. standalone_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1574. else:
  1575. standalone_html += escaped_title
  1576. standalone_html += """
  1577. </div>
  1578. </div>
  1579. </div>"""
  1580. standalone_html += """
  1581. </div>"""
  1582. group_idx += 1
  1583. # 渲染 RSS 源(复用相同结构)
  1584. for feed in rss_feeds:
  1585. feed_name = feed.get("name", feed.get("id", ""))
  1586. items = feed.get("items", [])
  1587. if not items:
  1588. continue
  1589. standalone_html += f"""
  1590. <div class="standalone-group" data-standalone-tab="{group_idx}">
  1591. <div class="standalone-header">
  1592. <div class="standalone-name">{html_escape(feed_name)}</div>
  1593. <div class="standalone-count">{len(items)} 条</div>
  1594. </div>"""
  1595. for j, item in enumerate(items, 1):
  1596. title = item.get("title", "")
  1597. url = item.get("url", "")
  1598. published_at = item.get("published_at", "")
  1599. author = item.get("author", "")
  1600. standalone_html += f"""
  1601. <div class="news-item">
  1602. <div class="news-number">{j}</div>
  1603. <div class="news-content">
  1604. <div class="news-header">"""
  1605. # 时间显示(格式化 ISO 时间)
  1606. if published_at:
  1607. try:
  1608. from datetime import datetime as dt
  1609. if "T" in published_at:
  1610. dt_obj = dt.fromisoformat(published_at.replace("Z", "+00:00"))
  1611. time_display = dt_obj.strftime("%m-%d %H:%M")
  1612. else:
  1613. time_display = published_at
  1614. except:
  1615. time_display = published_at
  1616. standalone_html += f'<span class="time-info">{html_escape(time_display)}</span>'
  1617. # 作者显示
  1618. if author:
  1619. standalone_html += f'<span class="source-name">{html_escape(author)}</span>'
  1620. standalone_html += """
  1621. </div>
  1622. <div class="news-title">"""
  1623. escaped_title = html_escape(title)
  1624. if url:
  1625. escaped_url = html_escape(url)
  1626. standalone_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1627. else:
  1628. standalone_html += escaped_title
  1629. standalone_html += """
  1630. </div>
  1631. </div>
  1632. </div>"""
  1633. standalone_html += """
  1634. </div>"""
  1635. group_idx += 1
  1636. standalone_html += """
  1637. </div>
  1638. </div>"""
  1639. return standalone_html
  1640. # 生成 RSS 统计和新增 HTML
  1641. rss_stats_html = render_rss_stats_html(rss_items, "RSS 订阅更新") if rss_items else ""
  1642. rss_new_html = render_rss_stats_html(rss_new_items, "RSS 新增更新") if rss_new_items else ""
  1643. # 生成独立展示区 HTML
  1644. standalone_html = render_standalone_html(standalone_data)
  1645. # 生成 AI 分析 HTML
  1646. ai_html = render_ai_analysis_html_rich(ai_analysis) if ai_analysis else ""
  1647. # 准备各区域内容映射
  1648. region_contents = {
  1649. "hotlist": stats_html,
  1650. "rss": rss_stats_html,
  1651. "new_items": (new_titles_html, rss_new_html), # 元组,分别处理
  1652. "standalone": standalone_html,
  1653. "ai_analysis": ai_html,
  1654. }
  1655. def add_section_divider(content: str) -> str:
  1656. """为内容的外层 div 添加 section-divider 类"""
  1657. if not content or 'class="' not in content:
  1658. return content
  1659. first_class_pos = content.find('class="')
  1660. if first_class_pos != -1:
  1661. insert_pos = first_class_pos + len('class="')
  1662. return content[:insert_pos] + "section-divider " + content[insert_pos:]
  1663. return content
  1664. # 按 region_order 顺序组装内容,动态添加分割线
  1665. has_previous_content = False
  1666. for region in region_order:
  1667. content = region_contents.get(region, "")
  1668. if region == "new_items":
  1669. # 特殊处理 new_items 区域(包含热榜新增和 RSS 新增两部分)
  1670. new_html, rss_new = content
  1671. if new_html:
  1672. if has_previous_content:
  1673. new_html = add_section_divider(new_html)
  1674. html += new_html
  1675. has_previous_content = True
  1676. if rss_new:
  1677. if has_previous_content:
  1678. rss_new = add_section_divider(rss_new)
  1679. html += rss_new
  1680. has_previous_content = True
  1681. elif content:
  1682. if has_previous_content:
  1683. content = add_section_divider(content)
  1684. html += content
  1685. has_previous_content = True
  1686. html += """
  1687. </div>
  1688. <div class="footer">
  1689. <div class="footer-content">
  1690. 由 <span class="project-name">TrendRadar</span> 生成 ·
  1691. <a href="https://github.com/sansan0/TrendRadar" target="_blank" class="footer-link">
  1692. GitHub 开源项目
  1693. </a>"""
  1694. if update_info:
  1695. html += f"""
  1696. <br>
  1697. <span style="color: #ea580c; font-weight: 500;">
  1698. 发现新版本 {update_info['remote_version']},当前版本 {update_info['current_version']}
  1699. </span>"""
  1700. html += """
  1701. </div>
  1702. </div>
  1703. </div>
  1704. <div class="fab-bar">
  1705. <button class="fab-btn" onclick="window.scrollTo({top:0,behavior:'smooth'})" title="返回顶部">↑</button>
  1706. <button class="fab-btn fab-help">
  1707. <span>?</span>
  1708. <div class="fab-tooltip">
  1709. <div class="tip-row"><span>切换宽屏</span><span class="tip-key">W</span></div>
  1710. <div class="tip-row"><span>暗色模式</span><span class="tip-key">D</span></div>
  1711. <div class="tip-row"><span>搜索</span><span class="tip-key">/</span></div>
  1712. <div class="tip-row"><span>上一个 Tab</span><span class="tip-key">←</span></div>
  1713. <div class="tip-row"><span>下一个 Tab</span><span class="tip-key">→</span></div>
  1714. <div class="tip-row"><span>序号可复制</span><span class="tip-key">点击</span></div>
  1715. </div>
  1716. </button>
  1717. </div>
  1718. <script>
  1719. // ===== 浏览器增强功能 =====
  1720. function toggleWideMode() {
  1721. document.body.classList.toggle('wide-mode');
  1722. var isWide = document.body.classList.contains('wide-mode');
  1723. try { localStorage.setItem('trendradar-wide-mode', isWide ? '1' : '0'); } catch(e) {}
  1724. var btn = document.querySelector('.toggle-wide-btn');
  1725. if (btn) btn.textContent = isWide ? '⊡' : '⛶';
  1726. initTabVisibility();
  1727. initCollapseVisibility();
  1728. initStandaloneTabVisibility();
  1729. }
  1730. function toggleDarkMode() {
  1731. var isDark = document.body.classList.toggle('dark-mode');
  1732. try { localStorage.setItem('trendradar-dark-mode', isDark ? '1' : '0'); } catch(e) {}
  1733. var btn = document.querySelector('.toggle-dark-btn');
  1734. if (btn) btn.textContent = isDark ? '☀' : '☽';
  1735. }
  1736. function initTabs() {
  1737. var tabBar = document.querySelector('.tab-bar');
  1738. if (!tabBar) return;
  1739. var tabs = tabBar.querySelectorAll('.tab-btn');
  1740. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1741. initTabVisibility();
  1742. function activateTab(index) {
  1743. tabs.forEach(function(t) { t.classList.remove('active'); });
  1744. if (index === 'all') {
  1745. var allBtn = tabBar.querySelector('[data-tab-index="all"]');
  1746. if (allBtn) allBtn.classList.add('active');
  1747. groups.forEach(function(g) { g.style.display = ''; });
  1748. try { history.replaceState(null, '', '#all'); } catch(e) {}
  1749. return;
  1750. }
  1751. var idx = parseInt(index);
  1752. tabs.forEach(function(t) {
  1753. if (parseInt(t.dataset.tabIndex) === idx) t.classList.add('active');
  1754. });
  1755. if (document.body.classList.contains('wide-mode') && !tabBar.classList.contains('tab-hidden')) {
  1756. groups.forEach(function(g) {
  1757. g.style.display = (parseInt(g.dataset.tabIndex) === idx) ? '' : 'none';
  1758. });
  1759. }
  1760. try { history.replaceState(null, '', '#tab-' + idx); } catch(e) {}
  1761. }
  1762. tabs.forEach(function(tab) {
  1763. tab.addEventListener('click', function() {
  1764. var idx = tab.dataset.tabIndex;
  1765. activateTab(idx === 'all' ? 'all' : parseInt(idx));
  1766. });
  1767. });
  1768. tabBar.addEventListener('keydown', function(e) {
  1769. if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
  1770. var tabsArr = Array.from(tabs);
  1771. var ci = tabsArr.findIndex(function(t) { return t.classList.contains('active'); });
  1772. var dir = e.key === 'ArrowRight' ? 1 : -1;
  1773. var ni = Math.max(0, Math.min(tabsArr.length - 1, ci + dir));
  1774. var nt = tabsArr[ni];
  1775. activateTab(nt.dataset.tabIndex === 'all' ? 'all' : parseInt(nt.dataset.tabIndex));
  1776. nt.focus();
  1777. e.preventDefault();
  1778. }
  1779. });
  1780. var hash = window.location.hash;
  1781. if (hash === '#all') { activateTab('all'); }
  1782. else if (hash.indexOf('#tab-') === 0) { activateTab(parseInt(hash.replace('#tab-', ''))); }
  1783. else { activateTab(0); }
  1784. }
  1785. function initTabVisibility() {
  1786. var tabBar = document.querySelector('.tab-bar');
  1787. if (!tabBar) return;
  1788. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1789. var isWide = document.body.classList.contains('wide-mode');
  1790. if (!isWide || groups.length <= 2) {
  1791. tabBar.classList.add('tab-hidden');
  1792. groups.forEach(function(g) { g.style.display = ''; });
  1793. } else {
  1794. tabBar.classList.remove('tab-hidden');
  1795. var activeTab = tabBar.querySelector('.tab-btn.active');
  1796. if (activeTab) { activeTab.click(); }
  1797. else {
  1798. var firstTab = tabBar.querySelector('.tab-btn');
  1799. if (firstTab) firstTab.click();
  1800. }
  1801. }
  1802. }
  1803. function handleSearch(query) {
  1804. query = query.toLowerCase();
  1805. document.querySelectorAll('.news-item').forEach(function(item) {
  1806. var title = (item.querySelector('.news-title') || {}).textContent || '';
  1807. item.style.display = (!query || title.toLowerCase().indexOf(query) !== -1) ? '' : 'none';
  1808. });
  1809. document.querySelectorAll('.rss-item').forEach(function(item) {
  1810. var title = (item.querySelector('.rss-title') || {}).textContent || '';
  1811. item.style.display = (!query || title.toLowerCase().indexOf(query) !== -1) ? '' : 'none';
  1812. });
  1813. }
  1814. function initBackToTop() {
  1815. var fabBar = document.querySelector('.fab-bar');
  1816. if (!fabBar) return;
  1817. window.addEventListener('scroll', function() {
  1818. fabBar.classList.toggle('visible', window.scrollY > 300);
  1819. });
  1820. }
  1821. function initCollapse() {
  1822. document.querySelectorAll('.word-header').forEach(function(header) {
  1823. header.addEventListener('click', function() {
  1824. var tabBar = document.querySelector('.tab-bar');
  1825. if (document.body.classList.contains('wide-mode') && tabBar && !tabBar.classList.contains('tab-hidden')) return;
  1826. var group = header.closest('.word-group');
  1827. if (group) group.classList.toggle('collapsed');
  1828. });
  1829. });
  1830. initCollapseVisibility();
  1831. }
  1832. function initCollapseVisibility() {
  1833. var headers = document.querySelectorAll('.word-header');
  1834. var tabBar = document.querySelector('.tab-bar');
  1835. var isTabMode = document.body.classList.contains('wide-mode') && tabBar && !tabBar.classList.contains('tab-hidden');
  1836. headers.forEach(function(h) {
  1837. if (isTabMode) { h.classList.remove('collapsible'); }
  1838. else { h.classList.add('collapsible'); }
  1839. });
  1840. if (isTabMode) {
  1841. document.querySelectorAll('.word-group.collapsed').forEach(function(g) {
  1842. g.classList.remove('collapsed');
  1843. });
  1844. }
  1845. }
  1846. // 独立展示区 Tab 切换
  1847. function initStandaloneTabs() {
  1848. var tabBar = document.querySelector('.standalone-tab-bar');
  1849. if (!tabBar) return;
  1850. var groups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1851. var btns = tabBar.querySelectorAll('.tab-btn[data-standalone-tab]');
  1852. function activateStandaloneTab(val) {
  1853. btns.forEach(function(b) {
  1854. var bVal = b.getAttribute('data-standalone-tab');
  1855. b.classList.toggle('active', bVal === String(val));
  1856. });
  1857. groups.forEach(function(g) {
  1858. var gVal = g.getAttribute('data-standalone-tab');
  1859. g.style.display = (val === 'all' || gVal === String(val)) ? '' : 'none';
  1860. });
  1861. }
  1862. btns.forEach(function(btn) {
  1863. btn.addEventListener('click', function() {
  1864. activateStandaloneTab(btn.getAttribute('data-standalone-tab'));
  1865. });
  1866. });
  1867. // 初始状态
  1868. initStandaloneTabVisibility();
  1869. }
  1870. function initStandaloneTabVisibility() {
  1871. var tabBar = document.querySelector('.standalone-tab-bar');
  1872. if (!tabBar) return;
  1873. var groups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1874. var isWide = document.body.classList.contains('wide-mode');
  1875. if (!isWide || groups.length <= 1) {
  1876. tabBar.classList.add('tab-hidden');
  1877. groups.forEach(function(g) { g.style.display = ''; });
  1878. } else {
  1879. tabBar.classList.remove('tab-hidden');
  1880. var activeBtn = tabBar.querySelector('.tab-btn.active');
  1881. if (activeBtn) activeBtn.click();
  1882. else { var first = tabBar.querySelector('.tab-btn'); if (first) first.click(); }
  1883. }
  1884. }
  1885. function prepareForScreenshot() {
  1886. var state = {
  1887. wasWide: document.body.classList.contains('wide-mode'),
  1888. hiddenGroups: []
  1889. };
  1890. document.body.classList.remove('wide-mode');
  1891. state.wasDark = document.body.classList.contains('dark-mode');
  1892. document.body.classList.remove('dark-mode');
  1893. document.querySelectorAll('.word-group[data-tab-index]').forEach(function(g, i) {
  1894. if (g.style.display === 'none') {
  1895. state.hiddenGroups.push(i);
  1896. g.style.display = '';
  1897. }
  1898. });
  1899. state.hiddenStandaloneGroups = [];
  1900. document.querySelectorAll('.standalone-group[data-standalone-tab]').forEach(function(g, i) {
  1901. if (g.style.display === 'none') {
  1902. state.hiddenStandaloneGroups.push(i);
  1903. g.style.display = '';
  1904. }
  1905. });
  1906. document.querySelectorAll('.tab-bar, .standalone-tab-bar, .search-bar, .fab-bar, .toggle-wide-btn').forEach(function(el) {
  1907. el.dataset.prevDisplay = el.style.display || '';
  1908. el.style.display = 'none';
  1909. });
  1910. document.querySelectorAll('.toggle-dark-btn').forEach(function(el) {
  1911. el.dataset.prevDisplay = el.style.display || ''; el.style.display = 'none';
  1912. });
  1913. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = 'none'; });
  1914. document.querySelectorAll('.header-watermark').forEach(function(el) { el.style.display = 'none'; });
  1915. return state;
  1916. }
  1917. function restoreAfterScreenshot(state) {
  1918. if (state.wasWide) document.body.classList.add('wide-mode');
  1919. if (state.wasDark) document.body.classList.add('dark-mode');
  1920. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1921. state.hiddenGroups.forEach(function(i) {
  1922. if (groups[i]) groups[i].style.display = 'none';
  1923. });
  1924. var standaloneGroups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1925. if (state.hiddenStandaloneGroups) {
  1926. state.hiddenStandaloneGroups.forEach(function(i) {
  1927. if (standaloneGroups[i]) standaloneGroups[i].style.display = 'none';
  1928. });
  1929. }
  1930. document.querySelectorAll('.tab-bar, .standalone-tab-bar, .search-bar, .fab-bar, .toggle-wide-btn').forEach(function(el) {
  1931. el.style.display = el.dataset.prevDisplay || '';
  1932. delete el.dataset.prevDisplay;
  1933. });
  1934. document.querySelectorAll('.toggle-dark-btn').forEach(function(el) {
  1935. el.style.display = el.dataset.prevDisplay || ''; delete el.dataset.prevDisplay;
  1936. });
  1937. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = ''; });
  1938. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = ''; });
  1939. document.querySelectorAll('.header-watermark').forEach(function(el) { el.style.display = ''; });
  1940. initTabVisibility();
  1941. initCollapseVisibility();
  1942. initStandaloneTabVisibility();
  1943. var fabBar = document.querySelector('.fab-bar');
  1944. if (fabBar && window.scrollY > 300) fabBar.classList.add('visible');
  1945. }
  1946. // ===== 截图功能 =====
  1947. async function saveAsImage() {
  1948. const button = event.target;
  1949. const originalText = button.textContent;
  1950. try {
  1951. button.textContent = '生成中...';
  1952. button.disabled = true;
  1953. window.scrollTo(0, 0);
  1954. // 等待页面稳定
  1955. await new Promise(resolve => setTimeout(resolve, 200));
  1956. // 截图前准备:切回窄屏布局
  1957. var screenshotState = prepareForScreenshot();
  1958. // 截图前隐藏按钮
  1959. const buttons = document.querySelector('.save-buttons');
  1960. buttons.style.visibility = 'hidden';
  1961. // 再次等待确保按钮完全隐藏
  1962. await new Promise(resolve => setTimeout(resolve, 100));
  1963. const container = document.querySelector('.container');
  1964. const canvas = await html2canvas(container, {
  1965. backgroundColor: '#ffffff',
  1966. scale: 1.5,
  1967. useCORS: true,
  1968. allowTaint: false,
  1969. imageTimeout: 10000,
  1970. removeContainer: false,
  1971. foreignObjectRendering: false,
  1972. logging: false,
  1973. width: container.offsetWidth,
  1974. height: container.offsetHeight,
  1975. x: 0,
  1976. y: 0,
  1977. scrollX: 0,
  1978. scrollY: 0,
  1979. windowWidth: window.innerWidth,
  1980. windowHeight: window.innerHeight
  1981. });
  1982. buttons.style.visibility = 'visible';
  1983. restoreAfterScreenshot(screenshotState);
  1984. const link = document.createElement('a');
  1985. const now = new Date();
  1986. const filename = `TrendRadar_热点新闻分析_${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}.png`;
  1987. link.download = filename;
  1988. link.href = canvas.toDataURL('image/png', 1.0);
  1989. // 触发下载
  1990. document.body.appendChild(link);
  1991. link.click();
  1992. document.body.removeChild(link);
  1993. button.textContent = '保存成功!';
  1994. setTimeout(() => {
  1995. button.textContent = originalText;
  1996. button.disabled = false;
  1997. }, 2000);
  1998. } catch (error) {
  1999. const buttons = document.querySelector('.save-buttons');
  2000. buttons.style.visibility = 'visible';
  2001. restoreAfterScreenshot(screenshotState);
  2002. button.textContent = '保存失败';
  2003. setTimeout(() => {
  2004. button.textContent = originalText;
  2005. button.disabled = false;
  2006. }, 2000);
  2007. }
  2008. }
  2009. async function saveAsMultipleImages() {
  2010. const button = event.target;
  2011. const originalText = button.textContent;
  2012. const container = document.querySelector('.container');
  2013. const scale = 1.5;
  2014. const maxHeight = 5000 / scale;
  2015. var screenshotState2 = prepareForScreenshot();
  2016. try {
  2017. button.textContent = '分析中...';
  2018. button.disabled = true;
  2019. // 获取所有可能的分割元素
  2020. const newsItems = Array.from(container.querySelectorAll('.news-item'));
  2021. const wordGroups = Array.from(container.querySelectorAll('.word-group'));
  2022. const newSection = container.querySelector('.new-section');
  2023. const errorSection = container.querySelector('.error-section');
  2024. const header = container.querySelector('.header');
  2025. const footer = container.querySelector('.footer');
  2026. // 计算元素位置和高度
  2027. const containerRect = container.getBoundingClientRect();
  2028. const elements = [];
  2029. // 添加header作为必须包含的元素
  2030. elements.push({
  2031. type: 'header',
  2032. element: header,
  2033. top: 0,
  2034. bottom: header.offsetHeight,
  2035. height: header.offsetHeight
  2036. });
  2037. // 添加错误信息(如果存在)
  2038. if (errorSection) {
  2039. const rect = errorSection.getBoundingClientRect();
  2040. elements.push({
  2041. type: 'error',
  2042. element: errorSection,
  2043. top: rect.top - containerRect.top,
  2044. bottom: rect.bottom - containerRect.top,
  2045. height: rect.height
  2046. });
  2047. }
  2048. // 按word-group分组处理news-item
  2049. wordGroups.forEach(group => {
  2050. const groupRect = group.getBoundingClientRect();
  2051. const groupNewsItems = group.querySelectorAll('.news-item');
  2052. // 添加word-group的header部分
  2053. const wordHeader = group.querySelector('.word-header');
  2054. if (wordHeader) {
  2055. const headerRect = wordHeader.getBoundingClientRect();
  2056. elements.push({
  2057. type: 'word-header',
  2058. element: wordHeader,
  2059. parent: group,
  2060. top: groupRect.top - containerRect.top,
  2061. bottom: headerRect.bottom - containerRect.top,
  2062. height: headerRect.height
  2063. });
  2064. }
  2065. // 添加每个news-item
  2066. groupNewsItems.forEach(item => {
  2067. const rect = item.getBoundingClientRect();
  2068. elements.push({
  2069. type: 'news-item',
  2070. element: item,
  2071. parent: group,
  2072. top: rect.top - containerRect.top,
  2073. bottom: rect.bottom - containerRect.top,
  2074. height: rect.height
  2075. });
  2076. });
  2077. });
  2078. // 添加新增新闻部分
  2079. if (newSection) {
  2080. const rect = newSection.getBoundingClientRect();
  2081. elements.push({
  2082. type: 'new-section',
  2083. element: newSection,
  2084. top: rect.top - containerRect.top,
  2085. bottom: rect.bottom - containerRect.top,
  2086. height: rect.height
  2087. });
  2088. }
  2089. // 添加footer
  2090. const footerRect = footer.getBoundingClientRect();
  2091. elements.push({
  2092. type: 'footer',
  2093. element: footer,
  2094. top: footerRect.top - containerRect.top,
  2095. bottom: footerRect.bottom - containerRect.top,
  2096. height: footer.offsetHeight
  2097. });
  2098. // 计算分割点
  2099. const segments = [];
  2100. let currentSegment = { start: 0, end: 0, height: 0, includeHeader: true };
  2101. let headerHeight = header.offsetHeight;
  2102. currentSegment.height = headerHeight;
  2103. for (let i = 1; i < elements.length; i++) {
  2104. const element = elements[i];
  2105. const potentialHeight = element.bottom - currentSegment.start;
  2106. // 检查是否需要创建新分段
  2107. if (potentialHeight > maxHeight && currentSegment.height > headerHeight) {
  2108. // 在前一个元素结束处分割
  2109. currentSegment.end = elements[i - 1].bottom;
  2110. segments.push(currentSegment);
  2111. // 开始新分段
  2112. currentSegment = {
  2113. start: currentSegment.end,
  2114. end: 0,
  2115. height: element.bottom - currentSegment.end,
  2116. includeHeader: false
  2117. };
  2118. } else {
  2119. currentSegment.height = potentialHeight;
  2120. currentSegment.end = element.bottom;
  2121. }
  2122. }
  2123. // 添加最后一个分段
  2124. if (currentSegment.height > 0) {
  2125. currentSegment.end = container.offsetHeight;
  2126. segments.push(currentSegment);
  2127. }
  2128. button.textContent = `生成中 (0/${segments.length})...`;
  2129. // 隐藏保存按钮
  2130. const buttons = document.querySelector('.save-buttons');
  2131. buttons.style.visibility = 'hidden';
  2132. // 为每个分段生成图片
  2133. const images = [];
  2134. for (let i = 0; i < segments.length; i++) {
  2135. const segment = segments[i];
  2136. button.textContent = `生成中 (${i + 1}/${segments.length})...`;
  2137. // 创建临时容器用于截图
  2138. const tempContainer = document.createElement('div');
  2139. tempContainer.style.cssText = `
  2140. position: absolute;
  2141. left: -9999px;
  2142. top: 0;
  2143. width: ${container.offsetWidth}px;
  2144. background: white;
  2145. `;
  2146. tempContainer.className = 'container';
  2147. // 克隆容器内容
  2148. const clonedContainer = container.cloneNode(true);
  2149. // 移除克隆内容中的保存按钮
  2150. const clonedButtons = clonedContainer.querySelector('.save-buttons');
  2151. if (clonedButtons) {
  2152. clonedButtons.style.display = 'none';
  2153. }
  2154. tempContainer.appendChild(clonedContainer);
  2155. document.body.appendChild(tempContainer);
  2156. // 等待DOM更新
  2157. await new Promise(resolve => setTimeout(resolve, 100));
  2158. // 使用html2canvas截取特定区域
  2159. const canvas = await html2canvas(clonedContainer, {
  2160. backgroundColor: '#ffffff',
  2161. scale: scale,
  2162. useCORS: true,
  2163. allowTaint: false,
  2164. imageTimeout: 10000,
  2165. logging: false,
  2166. width: container.offsetWidth,
  2167. height: segment.end - segment.start,
  2168. x: 0,
  2169. y: segment.start,
  2170. windowWidth: window.innerWidth,
  2171. windowHeight: window.innerHeight
  2172. });
  2173. images.push(canvas.toDataURL('image/png', 1.0));
  2174. // 清理临时容器
  2175. document.body.removeChild(tempContainer);
  2176. }
  2177. // 恢复按钮显示
  2178. buttons.style.visibility = 'visible';
  2179. // 下载所有图片
  2180. const now = new Date();
  2181. const baseFilename = `TrendRadar_热点新闻分析_${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}`;
  2182. for (let i = 0; i < images.length; i++) {
  2183. const link = document.createElement('a');
  2184. link.download = `${baseFilename}_part${i + 1}.png`;
  2185. link.href = images[i];
  2186. document.body.appendChild(link);
  2187. link.click();
  2188. document.body.removeChild(link);
  2189. // 延迟一下避免浏览器阻止多个下载
  2190. await new Promise(resolve => setTimeout(resolve, 100));
  2191. }
  2192. button.textContent = `已保存 ${segments.length} 张图片!`;
  2193. restoreAfterScreenshot(screenshotState2);
  2194. setTimeout(() => {
  2195. button.textContent = originalText;
  2196. button.disabled = false;
  2197. }, 2000);
  2198. } catch (error) {
  2199. console.error('分段保存失败:', error);
  2200. const buttons = document.querySelector('.save-buttons');
  2201. buttons.style.visibility = 'visible';
  2202. restoreAfterScreenshot(screenshotState2);
  2203. button.textContent = '保存失败';
  2204. setTimeout(() => {
  2205. button.textContent = originalText;
  2206. button.disabled = false;
  2207. }, 2000);
  2208. }
  2209. }
  2210. function saveAsMarkdown() {
  2211. var lines = [];
  2212. var now = new Date();
  2213. var dateStr = now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0') + '-' + String(now.getDate()).padStart(2, '0');
  2214. var timeStr = String(now.getHours()).padStart(2, '0') + ':' + String(now.getMinutes()).padStart(2, '0');
  2215. // 标题
  2216. var headerTitle = document.querySelector('.header-title');
  2217. lines.push('# ' + (headerTitle ? headerTitle.textContent.trim() : 'TrendRadar'));
  2218. lines.push('');
  2219. // 报告元信息
  2220. var infoItems = document.querySelectorAll('.header-info .info-item');
  2221. if (infoItems.length) {
  2222. infoItems.forEach(function(item) {
  2223. var label = item.querySelector('.info-label');
  2224. var value = item.querySelector('.info-value');
  2225. if (label && value) {
  2226. lines.push('- **' + label.textContent.trim() + '**: ' + value.textContent.trim());
  2227. }
  2228. });
  2229. lines.push('');
  2230. }
  2231. // 提取 news-item 通用函数
  2232. function extractItem(item, idx) {
  2233. var titleEl = item.querySelector('.news-title a');
  2234. var titleText = '';
  2235. var url = '';
  2236. if (titleEl) {
  2237. titleText = titleEl.textContent.trim();
  2238. url = titleEl.href || '';
  2239. } else {
  2240. var titleDiv = item.querySelector('.news-title') || item.querySelector('.new-item-title');
  2241. if (titleDiv) titleText = titleDiv.textContent.trim();
  2242. }
  2243. if (!titleText) return '';
  2244. var meta = [];
  2245. var rank = item.querySelector('.rank-num, .new-item-rank');
  2246. if (rank && rank.textContent.trim() && rank.textContent.trim() !== '?') meta.push('#' + rank.textContent.trim());
  2247. var source = item.querySelector('.source-name');
  2248. if (source) meta.push(source.textContent.trim());
  2249. var keyword = item.querySelector('.keyword-tag');
  2250. if (keyword) meta.push(keyword.textContent.trim());
  2251. var time = item.querySelector('.time-info');
  2252. if (time) meta.push(time.textContent.trim());
  2253. var count = item.querySelector('.count-info');
  2254. if (count) meta.push(count.textContent.trim());
  2255. var line = idx + '. ';
  2256. if (url) {
  2257. line += '[' + titleText.replace(/[[\]]/g, '') + '](' + url + ')';
  2258. } else {
  2259. line += titleText;
  2260. }
  2261. if (meta.length) line += ' `' + meta.join(' | ') + '`';
  2262. return line;
  2263. }
  2264. // 热点关键词区
  2265. var wordGroups = document.querySelectorAll('.hotlist-section > .word-group');
  2266. if (wordGroups.length) {
  2267. lines.push('## 热点新闻');
  2268. lines.push('');
  2269. wordGroups.forEach(function(group) {
  2270. var wordName = group.querySelector('.word-name');
  2271. var wordCount = group.querySelector('.word-count');
  2272. if (wordName) {
  2273. lines.push('### ' + wordName.textContent.trim() + (wordCount ? ' (' + wordCount.textContent.trim() + ')' : ''));
  2274. lines.push('');
  2275. }
  2276. var items = group.querySelectorAll('.news-item');
  2277. items.forEach(function(item, i) {
  2278. var line = extractItem(item, i + 1);
  2279. if (line) lines.push(line);
  2280. });
  2281. lines.push('');
  2282. });
  2283. }
  2284. // 新增热点区
  2285. var newSection = document.querySelector('.new-section');
  2286. if (newSection) {
  2287. var newTitle = newSection.querySelector('.new-section-title');
  2288. lines.push('## ' + (newTitle ? newTitle.textContent.trim() : '本次新增热点'));
  2289. lines.push('');
  2290. var sourceGroups = newSection.querySelectorAll('.new-source-group');
  2291. sourceGroups.forEach(function(sg) {
  2292. var srcTitle = sg.querySelector('.new-source-title');
  2293. if (srcTitle) {
  2294. lines.push('### ' + srcTitle.textContent.trim());
  2295. lines.push('');
  2296. }
  2297. var items = sg.querySelectorAll('.new-item');
  2298. items.forEach(function(item, i) {
  2299. var line = extractItem(item, i + 1);
  2300. if (line) lines.push(line);
  2301. });
  2302. lines.push('');
  2303. });
  2304. }
  2305. // 独立展示区(热榜平台 + RSS)
  2306. var standaloneSection = document.querySelector('.standalone-section');
  2307. if (standaloneSection) {
  2308. var standaloneTitle = standaloneSection.querySelector('.standalone-section-title');
  2309. lines.push('## ' + (standaloneTitle ? standaloneTitle.textContent.trim() : '独立展示区'));
  2310. lines.push('');
  2311. var groups = standaloneSection.querySelectorAll('.standalone-group');
  2312. groups.forEach(function(group) {
  2313. var name = group.querySelector('.standalone-name');
  2314. var cnt = group.querySelector('.standalone-count');
  2315. if (name) {
  2316. lines.push('### ' + name.textContent.trim() + (cnt ? ' (' + cnt.textContent.trim() + ')' : ''));
  2317. lines.push('');
  2318. }
  2319. var items = group.querySelectorAll('.news-item');
  2320. items.forEach(function(item, i) {
  2321. var line = extractItem(item, i + 1);
  2322. if (line) lines.push(line);
  2323. });
  2324. lines.push('');
  2325. });
  2326. }
  2327. // 错误区
  2328. var errorSection = document.querySelector('.error-section');
  2329. if (errorSection) {
  2330. var errorItems = errorSection.querySelectorAll('.error-item');
  2331. if (errorItems.length) {
  2332. lines.push('## 抓取异常');
  2333. lines.push('');
  2334. errorItems.forEach(function(item) {
  2335. lines.push('- ' + item.textContent.trim());
  2336. });
  2337. lines.push('');
  2338. }
  2339. }
  2340. // 页脚
  2341. lines.push('---');
  2342. lines.push('*Generated by TrendRadar*');
  2343. // 下载
  2344. var md = lines.join('\n');
  2345. var blob = new Blob([md], { type: 'text/markdown;charset=utf-8' });
  2346. var link = document.createElement('a');
  2347. var filename = 'TrendRadar_' + dateStr + '_' + timeStr.replace(':', '') + '.md';
  2348. link.download = filename;
  2349. link.href = URL.createObjectURL(blob);
  2350. document.body.appendChild(link);
  2351. link.click();
  2352. document.body.removeChild(link);
  2353. URL.revokeObjectURL(link.href);
  2354. }
  2355. document.addEventListener('DOMContentLoaded', function() {
  2356. window.scrollTo(0, 0);
  2357. // 自动检测宽屏模式
  2358. var savedMode = null;
  2359. try { savedMode = localStorage.getItem('trendradar-wide-mode'); } catch(e) {}
  2360. if (savedMode === '1' || (savedMode === null && window.innerWidth > 768)) {
  2361. document.body.classList.add('wide-mode');
  2362. var btn = document.querySelector('.toggle-wide-btn');
  2363. if (btn) btn.textContent = '⊡';
  2364. }
  2365. // 暗色模式恢复
  2366. var savedDark = null;
  2367. try { savedDark = localStorage.getItem('trendradar-dark-mode'); } catch(e) {}
  2368. if (savedDark === '1') {
  2369. document.body.classList.add('dark-mode');
  2370. var darkBtn = document.querySelector('.toggle-dark-btn');
  2371. if (darkBtn) darkBtn.textContent = '☀';
  2372. }
  2373. // 启用搜索栏
  2374. var searchBar = document.querySelector('.search-bar');
  2375. if (searchBar) searchBar.style.display = 'block';
  2376. // 初始化增强功能
  2377. initTabs();
  2378. initBackToTop();
  2379. initCollapse();
  2380. initStandaloneTabs();
  2381. // 键盘快捷键
  2382. document.addEventListener('keydown', function(e) {
  2383. if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
  2384. var helpBtn = document.querySelector('.fab-help');
  2385. switch(e.key) {
  2386. case '?':
  2387. if (helpBtn) {
  2388. helpBtn.classList.toggle('show-tip');
  2389. var fabBar = document.querySelector('.fab-bar');
  2390. if (fabBar) fabBar.classList.add('visible');
  2391. }
  2392. break;
  2393. case 'Escape':
  2394. if (helpBtn) helpBtn.classList.remove('show-tip');
  2395. break;
  2396. case 'w': case 'W': toggleWideMode(); break;
  2397. case 'd': case 'D': toggleDarkMode(); break;
  2398. case '/': e.preventDefault(); var si = document.querySelector('.search-input'); if (si) si.focus(); break;
  2399. }
  2400. });
  2401. // 阅读进度条
  2402. var progressBar = document.querySelector('.reading-progress');
  2403. if (progressBar) {
  2404. window.addEventListener('scroll', function() {
  2405. var h = document.documentElement.scrollHeight - window.innerHeight;
  2406. progressBar.style.width = (h > 0 ? (window.scrollY / h * 100) : 0) + '%';
  2407. });
  2408. }
  2409. // 一键复制:hover 时数字变复制图标
  2410. var copySvg = '<svg width="12" height="12" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="5" y="5" width="9" height="9" rx="1.5"/><path d="M5 11H3.5A1.5 1.5 0 012 9.5v-7A1.5 1.5 0 013.5 1h7A1.5 1.5 0 0112 2.5V5"/></svg>';
  2411. var checkSvg = '<svg width="12" height="12" viewBox="0 0 16 16" fill="none" stroke="#22c55e" stroke-width="2"><path d="M3 8.5l3.5 3.5 7-7"/></svg>';
  2412. document.querySelectorAll('.news-item .news-number').forEach(function(numEl) {
  2413. var item = numEl.closest('.news-item');
  2414. var titleEl = item ? item.querySelector('.news-title a') : null;
  2415. if (!titleEl) return;
  2416. var numText = numEl.textContent.trim();
  2417. numEl.innerHTML = '<span class="num-text">' + numText + '</span><span class="copy-icon">' + copySvg + '</span>';
  2418. numEl.title = '点击复制标题和链接';
  2419. numEl.addEventListener('click', function(e) {
  2420. e.stopPropagation();
  2421. var text = titleEl.textContent.trim() + ' ' + titleEl.href;
  2422. navigator.clipboard.writeText(text).then(function() {
  2423. numEl.classList.add('copied');
  2424. numEl.querySelector('.copy-icon').innerHTML = checkSvg;
  2425. setTimeout(function() {
  2426. numEl.classList.remove('copied');
  2427. numEl.querySelector('.copy-icon').innerHTML = copySvg;
  2428. }, 1500);
  2429. });
  2430. });
  2431. });
  2432. // Header watermark 鼠标跟随揭示
  2433. (function() {
  2434. var header = document.querySelector('.header');
  2435. var watermark = document.querySelector('.header-watermark');
  2436. if (!header || !watermark) return;
  2437. var radius = 100;
  2438. header.addEventListener('mousemove', function(e) {
  2439. var rect = watermark.getBoundingClientRect();
  2440. var x = e.clientX - rect.left;
  2441. var y = e.clientY - rect.top;
  2442. var maskVal = 'radial-gradient(circle ' + radius + 'px at ' + x + 'px ' + y + 'px, rgba(0,0,0,1) 0%, rgba(0,0,0,0.3) 50%, rgba(0,0,0,0) 100%)';
  2443. watermark.style.webkitMaskImage = maskVal;
  2444. watermark.style.maskImage = maskVal;
  2445. watermark.style.color = 'rgba(255, 255, 255, 0.25)';
  2446. });
  2447. header.addEventListener('mouseleave', function() {
  2448. watermark.style.webkitMaskImage = 'radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%)';
  2449. watermark.style.maskImage = 'radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%)';
  2450. watermark.style.color = 'rgba(255, 255, 255, 0.15)';
  2451. });
  2452. })();
  2453. });
  2454. </script>
  2455. </body>
  2456. </html>
  2457. """
  2458. return html