html.py 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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: hidden;
  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. </div>
  1121. </div>
  1122. </div>
  1123. <div class="header-title">热点新闻分析</div>
  1124. <div class="header-info">
  1125. <div class="info-item">
  1126. <span class="info-label">报告类型</span>
  1127. <span class="info-value">"""
  1128. # 处理报告类型显示(根据 mode 直接显示)
  1129. if mode == "current":
  1130. html += "当前榜单"
  1131. elif mode == "incremental":
  1132. html += "增量分析"
  1133. else:
  1134. html += "全天汇总"
  1135. html += """</span>
  1136. </div>
  1137. <div class="info-item">
  1138. <span class="info-label">新闻总数</span>
  1139. <span class="info-value">"""
  1140. html += f"{total_titles} 条"
  1141. # 计算筛选后的热点新闻数量
  1142. hot_news_count = sum(len(stat["titles"]) for stat in report_data["stats"])
  1143. html += """</span>
  1144. </div>
  1145. <div class="info-item">
  1146. <span class="info-label">热点新闻</span>
  1147. <span class="info-value">"""
  1148. html += f"{hot_news_count} 条"
  1149. html += """</span>
  1150. </div>
  1151. <div class="info-item">
  1152. <span class="info-label">生成时间</span>
  1153. <span class="info-value">"""
  1154. # 使用提供的时间函数或默认 datetime.now
  1155. if get_time_func:
  1156. now = get_time_func()
  1157. else:
  1158. now = datetime.now()
  1159. html += now.strftime("%m-%d %H:%M")
  1160. html += """</span>
  1161. </div>
  1162. </div>
  1163. </div>
  1164. <div class="content">
  1165. <div class="search-bar">
  1166. <input type="text" class="search-input" placeholder="搜索新闻标题..." oninput="handleSearch(this.value)">
  1167. </div>"""
  1168. # 处理失败ID错误信息
  1169. if report_data["failed_ids"]:
  1170. html += """
  1171. <div class="error-section">
  1172. <div class="error-title">⚠️ 请求失败的平台</div>
  1173. <ul class="error-list">"""
  1174. for id_value in report_data["failed_ids"]:
  1175. html += f'<li class="error-item">{html_escape(id_value)}</li>'
  1176. html += """
  1177. </ul>
  1178. </div>"""
  1179. # 生成热点词汇统计部分的HTML
  1180. stats_html = ""
  1181. tab_bar_html = ""
  1182. if report_data["stats"]:
  1183. total_count = len(report_data["stats"])
  1184. # 生成 Tab 栏 HTML
  1185. tab_bar_html = '<div class="tab-bar">'
  1186. for tab_i, tab_stat in enumerate(report_data["stats"]):
  1187. escaped_tab_word = html_escape(tab_stat["word"])
  1188. tab_count = tab_stat["count"]
  1189. tab_bar_html += f'<button class="tab-btn" data-tab-index="{tab_i}">{escaped_tab_word}<span class="tab-count">{tab_count}</span></button>'
  1190. tab_bar_html += '<button class="tab-btn" data-tab-index="all">全部</button>'
  1191. tab_bar_html += '</div>'
  1192. for i, stat in enumerate(report_data["stats"], 1):
  1193. count = stat["count"]
  1194. # 确定热度等级
  1195. if count >= 10:
  1196. count_class = "hot"
  1197. elif count >= 5:
  1198. count_class = "warm"
  1199. else:
  1200. count_class = ""
  1201. escaped_word = html_escape(stat["word"])
  1202. stats_html += f"""
  1203. <div class="word-group" data-tab-index="{i - 1}">
  1204. <div class="word-header">
  1205. <div class="word-info">
  1206. <div class="word-name">{escaped_word}</div>
  1207. <div class="word-count {count_class}">{count} 条</div>
  1208. </div>
  1209. <div class="word-index"><span class="collapse-icon">▼</span>{i}/{total_count}</div>
  1210. </div>"""
  1211. # 处理每个词组下的新闻标题,给每条新闻标上序号
  1212. for j, title_data in enumerate(stat["titles"], 1):
  1213. is_new = title_data.get("is_new", False)
  1214. new_class = "new" if is_new else ""
  1215. stats_html += f"""
  1216. <div class="news-item {new_class}">
  1217. <div class="news-number">{j}</div>
  1218. <div class="news-content">
  1219. <div class="news-header">"""
  1220. # 根据 display_mode 决定显示来源还是关键词
  1221. if display_mode == "keyword":
  1222. # keyword 模式:显示来源
  1223. stats_html += f'<span class="source-name">{html_escape(title_data["source_name"])}</span>'
  1224. else:
  1225. # platform 模式:显示关键词
  1226. matched_keyword = title_data.get("matched_keyword", "")
  1227. if matched_keyword:
  1228. stats_html += f'<span class="keyword-tag">[{html_escape(matched_keyword)}]</span>'
  1229. # 处理排名显示
  1230. ranks = title_data.get("ranks", [])
  1231. if ranks:
  1232. min_rank = min(ranks)
  1233. max_rank = max(ranks)
  1234. rank_threshold = title_data.get("rank_threshold", 10)
  1235. # 确定排名等级
  1236. if min_rank <= 3:
  1237. rank_class = "top"
  1238. elif min_rank <= rank_threshold:
  1239. rank_class = "high"
  1240. else:
  1241. rank_class = ""
  1242. if min_rank == max_rank:
  1243. rank_text = str(min_rank)
  1244. else:
  1245. rank_text = f"{min_rank}-{max_rank}"
  1246. stats_html += f'<span class="rank-num {rank_class}">{rank_text}</span>'
  1247. # 处理时间显示
  1248. time_display = title_data.get("time_display", "")
  1249. if time_display:
  1250. # 简化时间显示格式,将波浪线替换为~
  1251. simplified_time = (
  1252. time_display.replace(" ~ ", "~")
  1253. .replace("[", "")
  1254. .replace("]", "")
  1255. )
  1256. stats_html += (
  1257. f'<span class="time-info">{html_escape(simplified_time)}</span>'
  1258. )
  1259. # 处理出现次数
  1260. count_info = title_data.get("count", 1)
  1261. if count_info > 1:
  1262. stats_html += f'<span class="count-info">{count_info}次</span>'
  1263. stats_html += """
  1264. </div>
  1265. <div class="news-title">"""
  1266. # 处理标题和链接
  1267. escaped_title = html_escape(title_data["title"])
  1268. link_url = title_data.get("mobile_url") or title_data.get("url", "")
  1269. if link_url:
  1270. escaped_url = html_escape(link_url)
  1271. stats_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1272. else:
  1273. stats_html += escaped_title
  1274. stats_html += """
  1275. </div>
  1276. </div>
  1277. </div>"""
  1278. stats_html += """
  1279. </div>"""
  1280. # 给热榜统计添加外层包装
  1281. if stats_html:
  1282. stats_html = f"""
  1283. <div class="hotlist-section">{tab_bar_html}{stats_html}
  1284. </div>"""
  1285. # 生成新增新闻区域的HTML
  1286. new_titles_html = ""
  1287. if show_new_section and report_data["new_titles"]:
  1288. new_titles_html += f"""
  1289. <div class="new-section">
  1290. <div class="new-section-title">本次新增热点 (共 {report_data['total_new_count']} 条)</div>
  1291. <div class="new-sources-grid">"""
  1292. for source_data in report_data["new_titles"]:
  1293. escaped_source = html_escape(source_data["source_name"])
  1294. titles_count = len(source_data["titles"])
  1295. new_titles_html += f"""
  1296. <div class="new-source-group">
  1297. <div class="new-source-title">{escaped_source} · {titles_count}条</div>"""
  1298. # 为新增新闻也添加序号
  1299. for idx, title_data in enumerate(source_data["titles"], 1):
  1300. ranks = title_data.get("ranks", [])
  1301. # 处理新增新闻的排名显示
  1302. rank_class = ""
  1303. if ranks:
  1304. min_rank = min(ranks)
  1305. if min_rank <= 3:
  1306. rank_class = "top"
  1307. elif min_rank <= title_data.get("rank_threshold", 10):
  1308. rank_class = "high"
  1309. if len(ranks) == 1:
  1310. rank_text = str(ranks[0])
  1311. else:
  1312. rank_text = f"{min(ranks)}-{max(ranks)}"
  1313. else:
  1314. rank_text = "?"
  1315. new_titles_html += f"""
  1316. <div class="new-item">
  1317. <div class="new-item-number">{idx}</div>
  1318. <div class="new-item-rank {rank_class}">{rank_text}</div>
  1319. <div class="new-item-content">
  1320. <div class="new-item-title">"""
  1321. # 处理新增新闻的链接
  1322. escaped_title = html_escape(title_data["title"])
  1323. link_url = title_data.get("mobile_url") or title_data.get("url", "")
  1324. if link_url:
  1325. escaped_url = html_escape(link_url)
  1326. new_titles_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1327. else:
  1328. new_titles_html += escaped_title
  1329. new_titles_html += """
  1330. </div>
  1331. </div>
  1332. </div>"""
  1333. new_titles_html += """
  1334. </div>"""
  1335. new_titles_html += """
  1336. </div>
  1337. </div>"""
  1338. # 生成 RSS 统计内容
  1339. def render_rss_stats_html(stats: List[Dict], title: str = "RSS 订阅更新") -> str:
  1340. """渲染 RSS 统计区块 HTML
  1341. Args:
  1342. stats: RSS 分组统计列表,格式与热榜一致:
  1343. [
  1344. {
  1345. "word": "关键词",
  1346. "count": 5,
  1347. "titles": [
  1348. {
  1349. "title": "标题",
  1350. "source_name": "Feed 名称",
  1351. "time_display": "12-29 08:20",
  1352. "url": "...",
  1353. "is_new": True/False
  1354. }
  1355. ]
  1356. }
  1357. ]
  1358. title: 区块标题
  1359. Returns:
  1360. 渲染后的 HTML 字符串
  1361. """
  1362. if not stats:
  1363. return ""
  1364. # 计算总条目数
  1365. total_count = sum(stat.get("count", 0) for stat in stats)
  1366. if total_count == 0:
  1367. return ""
  1368. rss_html = f"""
  1369. <div class="rss-section">
  1370. <div class="rss-section-header">
  1371. <div class="rss-section-title">{title}</div>
  1372. <div class="rss-section-count">{total_count} 条</div>
  1373. </div>
  1374. <div class="rss-feeds-grid">"""
  1375. # 按关键词分组渲染(与热榜格式一致)
  1376. for stat in stats:
  1377. keyword = stat.get("word", "")
  1378. titles = stat.get("titles", [])
  1379. if not titles:
  1380. continue
  1381. keyword_count = len(titles)
  1382. rss_html += f"""
  1383. <div class="feed-group">
  1384. <div class="feed-header">
  1385. <div class="feed-name">{html_escape(keyword)}</div>
  1386. <div class="feed-count">{keyword_count} 条</div>
  1387. </div>"""
  1388. for title_data in titles:
  1389. item_title = title_data.get("title", "")
  1390. url = title_data.get("url", "")
  1391. time_display = title_data.get("time_display", "")
  1392. source_name = title_data.get("source_name", "")
  1393. is_new = title_data.get("is_new", False)
  1394. rss_html += """
  1395. <div class="rss-item">
  1396. <div class="rss-meta">"""
  1397. if time_display:
  1398. rss_html += f'<span class="rss-time">{html_escape(time_display)}</span>'
  1399. if source_name:
  1400. rss_html += f'<span class="rss-author">{html_escape(source_name)}</span>'
  1401. if is_new:
  1402. rss_html += '<span class="rss-author" style="color: #dc2626;">NEW</span>'
  1403. rss_html += """
  1404. </div>
  1405. <div class="rss-title">"""
  1406. escaped_title = html_escape(item_title)
  1407. if url:
  1408. escaped_url = html_escape(url)
  1409. rss_html += f'<a href="{escaped_url}" target="_blank" class="rss-link">{escaped_title}</a>'
  1410. else:
  1411. rss_html += escaped_title
  1412. rss_html += """
  1413. </div>
  1414. </div>"""
  1415. rss_html += """
  1416. </div>"""
  1417. rss_html += """
  1418. </div>
  1419. </div>"""
  1420. return rss_html
  1421. # 生成独立展示区内容
  1422. def render_standalone_html(data: Optional[Dict]) -> str:
  1423. """渲染独立展示区 HTML(复用热点词汇统计区样式)
  1424. Args:
  1425. data: 独立展示数据,格式:
  1426. {
  1427. "platforms": [
  1428. {
  1429. "id": "zhihu",
  1430. "name": "知乎热榜",
  1431. "items": [
  1432. {
  1433. "title": "标题",
  1434. "url": "链接",
  1435. "rank": 1,
  1436. "ranks": [1, 2, 1],
  1437. "first_time": "08:00",
  1438. "last_time": "12:30",
  1439. "count": 3,
  1440. }
  1441. ]
  1442. }
  1443. ],
  1444. "rss_feeds": [
  1445. {
  1446. "id": "hacker-news",
  1447. "name": "Hacker News",
  1448. "items": [
  1449. {
  1450. "title": "标题",
  1451. "url": "链接",
  1452. "published_at": "2025-01-07T08:00:00",
  1453. "author": "作者",
  1454. }
  1455. ]
  1456. }
  1457. ]
  1458. }
  1459. Returns:
  1460. 渲染后的 HTML 字符串
  1461. """
  1462. if not data:
  1463. return ""
  1464. platforms = data.get("platforms", [])
  1465. rss_feeds = data.get("rss_feeds", [])
  1466. if not platforms and not rss_feeds:
  1467. return ""
  1468. # 计算总条目数
  1469. total_platform_items = sum(len(p.get("items", [])) for p in platforms)
  1470. total_rss_items = sum(len(f.get("items", [])) for f in rss_feeds)
  1471. total_count = total_platform_items + total_rss_items
  1472. if total_count == 0:
  1473. return ""
  1474. # 收集所有分组信息用于生成 tab
  1475. all_groups = []
  1476. for p in platforms:
  1477. items = p.get("items", [])
  1478. if items:
  1479. all_groups.append({"name": p.get("name", p.get("id", "")), "count": len(items)})
  1480. for f in rss_feeds:
  1481. items = f.get("items", [])
  1482. if items:
  1483. all_groups.append({"name": f.get("name", f.get("id", "")), "count": len(items)})
  1484. standalone_html = f"""
  1485. <div class="standalone-section">
  1486. <div class="standalone-section-header">
  1487. <div class="standalone-section-title">独立展示区</div>
  1488. <div class="standalone-section-count">{total_count} 条</div>
  1489. </div>"""
  1490. # 生成 tab 栏(2+ 分组时)
  1491. if len(all_groups) >= 2:
  1492. standalone_html += """
  1493. <div class="tab-bar standalone-tab-bar">"""
  1494. for idx, g in enumerate(all_groups):
  1495. active = ' active' if idx == 0 else ''
  1496. standalone_html += f"""
  1497. <button class="tab-btn{active}" data-standalone-tab="{idx}">{html_escape(g["name"])}<span class="tab-count">{g["count"]}</span></button>"""
  1498. standalone_html += f"""
  1499. <button class="tab-btn" data-standalone-tab="all">全部<span class="tab-count">{total_count}</span></button>
  1500. </div>"""
  1501. standalone_html += """
  1502. <div class="standalone-groups-grid">"""
  1503. group_idx = 0
  1504. # 渲染热榜平台(复用 word-group 结构)
  1505. for platform in platforms:
  1506. platform_name = platform.get("name", platform.get("id", ""))
  1507. items = platform.get("items", [])
  1508. if not items:
  1509. continue
  1510. standalone_html += f"""
  1511. <div class="standalone-group" data-standalone-tab="{group_idx}">
  1512. <div class="standalone-header">
  1513. <div class="standalone-name">{html_escape(platform_name)}</div>
  1514. <div class="standalone-count">{len(items)} 条</div>
  1515. </div>"""
  1516. # 渲染每个条目(复用 news-item 结构)
  1517. for j, item in enumerate(items, 1):
  1518. title = item.get("title", "")
  1519. url = item.get("url", "") or item.get("mobileUrl", "")
  1520. rank = item.get("rank", 0)
  1521. ranks = item.get("ranks", [])
  1522. first_time = item.get("first_time", "")
  1523. last_time = item.get("last_time", "")
  1524. count = item.get("count", 1)
  1525. standalone_html += f"""
  1526. <div class="news-item">
  1527. <div class="news-number">{j}</div>
  1528. <div class="news-content">
  1529. <div class="news-header">"""
  1530. # 排名显示(复用 rank-num 样式,无 # 前缀)
  1531. if ranks:
  1532. min_rank = min(ranks)
  1533. max_rank = max(ranks)
  1534. # 确定排名等级
  1535. if min_rank <= 3:
  1536. rank_class = "top"
  1537. elif min_rank <= 10:
  1538. rank_class = "high"
  1539. else:
  1540. rank_class = ""
  1541. if min_rank == max_rank:
  1542. rank_text = str(min_rank)
  1543. else:
  1544. rank_text = f"{min_rank}-{max_rank}"
  1545. standalone_html += f'<span class="rank-num {rank_class}">{rank_text}</span>'
  1546. elif rank > 0:
  1547. if rank <= 3:
  1548. rank_class = "top"
  1549. elif rank <= 10:
  1550. rank_class = "high"
  1551. else:
  1552. rank_class = ""
  1553. standalone_html += f'<span class="rank-num {rank_class}">{rank}</span>'
  1554. # 时间显示(复用 time-info 样式,将 HH-MM 转换为 HH:MM)
  1555. if first_time and last_time and first_time != last_time:
  1556. first_time_display = convert_time_for_display(first_time)
  1557. last_time_display = convert_time_for_display(last_time)
  1558. standalone_html += f'<span class="time-info">{html_escape(first_time_display)}~{html_escape(last_time_display)}</span>'
  1559. elif first_time:
  1560. first_time_display = convert_time_for_display(first_time)
  1561. standalone_html += f'<span class="time-info">{html_escape(first_time_display)}</span>'
  1562. # 出现次数(复用 count-info 样式)
  1563. if count > 1:
  1564. standalone_html += f'<span class="count-info">{count}次</span>'
  1565. standalone_html += """
  1566. </div>
  1567. <div class="news-title">"""
  1568. # 标题和链接(复用 news-link 样式)
  1569. escaped_title = html_escape(title)
  1570. if url:
  1571. escaped_url = html_escape(url)
  1572. standalone_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1573. else:
  1574. standalone_html += escaped_title
  1575. standalone_html += """
  1576. </div>
  1577. </div>
  1578. </div>"""
  1579. standalone_html += """
  1580. </div>"""
  1581. group_idx += 1
  1582. # 渲染 RSS 源(复用相同结构)
  1583. for feed in rss_feeds:
  1584. feed_name = feed.get("name", feed.get("id", ""))
  1585. items = feed.get("items", [])
  1586. if not items:
  1587. continue
  1588. standalone_html += f"""
  1589. <div class="standalone-group" data-standalone-tab="{group_idx}">
  1590. <div class="standalone-header">
  1591. <div class="standalone-name">{html_escape(feed_name)}</div>
  1592. <div class="standalone-count">{len(items)} 条</div>
  1593. </div>"""
  1594. for j, item in enumerate(items, 1):
  1595. title = item.get("title", "")
  1596. url = item.get("url", "")
  1597. published_at = item.get("published_at", "")
  1598. author = item.get("author", "")
  1599. standalone_html += f"""
  1600. <div class="news-item">
  1601. <div class="news-number">{j}</div>
  1602. <div class="news-content">
  1603. <div class="news-header">"""
  1604. # 时间显示(格式化 ISO 时间)
  1605. if published_at:
  1606. try:
  1607. from datetime import datetime as dt
  1608. if "T" in published_at:
  1609. dt_obj = dt.fromisoformat(published_at.replace("Z", "+00:00"))
  1610. time_display = dt_obj.strftime("%m-%d %H:%M")
  1611. else:
  1612. time_display = published_at
  1613. except:
  1614. time_display = published_at
  1615. standalone_html += f'<span class="time-info">{html_escape(time_display)}</span>'
  1616. # 作者显示
  1617. if author:
  1618. standalone_html += f'<span class="source-name">{html_escape(author)}</span>'
  1619. standalone_html += """
  1620. </div>
  1621. <div class="news-title">"""
  1622. escaped_title = html_escape(title)
  1623. if url:
  1624. escaped_url = html_escape(url)
  1625. standalone_html += f'<a href="{escaped_url}" target="_blank" class="news-link">{escaped_title}</a>'
  1626. else:
  1627. standalone_html += escaped_title
  1628. standalone_html += """
  1629. </div>
  1630. </div>
  1631. </div>"""
  1632. standalone_html += """
  1633. </div>"""
  1634. group_idx += 1
  1635. standalone_html += """
  1636. </div>
  1637. </div>"""
  1638. return standalone_html
  1639. # 生成 RSS 统计和新增 HTML
  1640. rss_stats_html = render_rss_stats_html(rss_items, "RSS 订阅更新") if rss_items else ""
  1641. rss_new_html = render_rss_stats_html(rss_new_items, "RSS 新增更新") if rss_new_items else ""
  1642. # 生成独立展示区 HTML
  1643. standalone_html = render_standalone_html(standalone_data)
  1644. # 生成 AI 分析 HTML
  1645. ai_html = render_ai_analysis_html_rich(ai_analysis) if ai_analysis else ""
  1646. # 准备各区域内容映射
  1647. region_contents = {
  1648. "hotlist": stats_html,
  1649. "rss": rss_stats_html,
  1650. "new_items": (new_titles_html, rss_new_html), # 元组,分别处理
  1651. "standalone": standalone_html,
  1652. "ai_analysis": ai_html,
  1653. }
  1654. def add_section_divider(content: str) -> str:
  1655. """为内容的外层 div 添加 section-divider 类"""
  1656. if not content or 'class="' not in content:
  1657. return content
  1658. first_class_pos = content.find('class="')
  1659. if first_class_pos != -1:
  1660. insert_pos = first_class_pos + len('class="')
  1661. return content[:insert_pos] + "section-divider " + content[insert_pos:]
  1662. return content
  1663. # 按 region_order 顺序组装内容,动态添加分割线
  1664. has_previous_content = False
  1665. for region in region_order:
  1666. content = region_contents.get(region, "")
  1667. if region == "new_items":
  1668. # 特殊处理 new_items 区域(包含热榜新增和 RSS 新增两部分)
  1669. new_html, rss_new = content
  1670. if new_html:
  1671. if has_previous_content:
  1672. new_html = add_section_divider(new_html)
  1673. html += new_html
  1674. has_previous_content = True
  1675. if rss_new:
  1676. if has_previous_content:
  1677. rss_new = add_section_divider(rss_new)
  1678. html += rss_new
  1679. has_previous_content = True
  1680. elif content:
  1681. if has_previous_content:
  1682. content = add_section_divider(content)
  1683. html += content
  1684. has_previous_content = True
  1685. html += """
  1686. </div>
  1687. <div class="footer">
  1688. <div class="footer-content">
  1689. 由 <span class="project-name">TrendRadar</span> 生成 ·
  1690. <a href="https://github.com/sansan0/TrendRadar" target="_blank" class="footer-link">
  1691. GitHub 开源项目
  1692. </a>"""
  1693. if update_info:
  1694. html += f"""
  1695. <br>
  1696. <span style="color: #ea580c; font-weight: 500;">
  1697. 发现新版本 {update_info['remote_version']},当前版本 {update_info['current_version']}
  1698. </span>"""
  1699. html += """
  1700. </div>
  1701. </div>
  1702. </div>
  1703. <div class="fab-bar">
  1704. <button class="fab-btn" onclick="window.scrollTo({top:0,behavior:'smooth'})" title="返回顶部">↑</button>
  1705. <button class="fab-btn fab-help">
  1706. <span>?</span>
  1707. <div class="fab-tooltip">
  1708. <div class="tip-row"><span>切换宽屏</span><span class="tip-key">W</span></div>
  1709. <div class="tip-row"><span>暗色模式</span><span class="tip-key">D</span></div>
  1710. <div class="tip-row"><span>搜索</span><span class="tip-key">/</span></div>
  1711. <div class="tip-row"><span>上一个 Tab</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>序号可复制</span><span class="tip-key">点击</span></div>
  1714. </div>
  1715. </button>
  1716. </div>
  1717. <script>
  1718. // ===== 浏览器增强功能 =====
  1719. function toggleWideMode() {
  1720. document.body.classList.toggle('wide-mode');
  1721. var isWide = document.body.classList.contains('wide-mode');
  1722. try { localStorage.setItem('trendradar-wide-mode', isWide ? '1' : '0'); } catch(e) {}
  1723. var btn = document.querySelector('.toggle-wide-btn');
  1724. if (btn) btn.textContent = isWide ? '⊡' : '⛶';
  1725. initTabVisibility();
  1726. initCollapseVisibility();
  1727. initStandaloneTabVisibility();
  1728. }
  1729. function toggleDarkMode() {
  1730. var isDark = document.body.classList.toggle('dark-mode');
  1731. try { localStorage.setItem('trendradar-dark-mode', isDark ? '1' : '0'); } catch(e) {}
  1732. var btn = document.querySelector('.toggle-dark-btn');
  1733. if (btn) btn.textContent = isDark ? '☀' : '☽';
  1734. }
  1735. function initTabs() {
  1736. var tabBar = document.querySelector('.tab-bar');
  1737. if (!tabBar) return;
  1738. var tabs = tabBar.querySelectorAll('.tab-btn');
  1739. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1740. initTabVisibility();
  1741. function activateTab(index) {
  1742. tabs.forEach(function(t) { t.classList.remove('active'); });
  1743. if (index === 'all') {
  1744. var allBtn = tabBar.querySelector('[data-tab-index="all"]');
  1745. if (allBtn) allBtn.classList.add('active');
  1746. groups.forEach(function(g) { g.style.display = ''; });
  1747. try { history.replaceState(null, '', '#all'); } catch(e) {}
  1748. return;
  1749. }
  1750. var idx = parseInt(index);
  1751. tabs.forEach(function(t) {
  1752. if (parseInt(t.dataset.tabIndex) === idx) t.classList.add('active');
  1753. });
  1754. if (document.body.classList.contains('wide-mode') && !tabBar.classList.contains('tab-hidden')) {
  1755. groups.forEach(function(g) {
  1756. g.style.display = (parseInt(g.dataset.tabIndex) === idx) ? '' : 'none';
  1757. });
  1758. }
  1759. try { history.replaceState(null, '', '#tab-' + idx); } catch(e) {}
  1760. }
  1761. tabs.forEach(function(tab) {
  1762. tab.addEventListener('click', function() {
  1763. var idx = tab.dataset.tabIndex;
  1764. activateTab(idx === 'all' ? 'all' : parseInt(idx));
  1765. });
  1766. });
  1767. tabBar.addEventListener('keydown', function(e) {
  1768. if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
  1769. var tabsArr = Array.from(tabs);
  1770. var ci = tabsArr.findIndex(function(t) { return t.classList.contains('active'); });
  1771. var dir = e.key === 'ArrowRight' ? 1 : -1;
  1772. var ni = Math.max(0, Math.min(tabsArr.length - 1, ci + dir));
  1773. var nt = tabsArr[ni];
  1774. activateTab(nt.dataset.tabIndex === 'all' ? 'all' : parseInt(nt.dataset.tabIndex));
  1775. nt.focus();
  1776. e.preventDefault();
  1777. }
  1778. });
  1779. var hash = window.location.hash;
  1780. if (hash === '#all') { activateTab('all'); }
  1781. else if (hash.indexOf('#tab-') === 0) { activateTab(parseInt(hash.replace('#tab-', ''))); }
  1782. else { activateTab(0); }
  1783. }
  1784. function initTabVisibility() {
  1785. var tabBar = document.querySelector('.tab-bar');
  1786. if (!tabBar) return;
  1787. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1788. var isWide = document.body.classList.contains('wide-mode');
  1789. if (!isWide || groups.length <= 2) {
  1790. tabBar.classList.add('tab-hidden');
  1791. groups.forEach(function(g) { g.style.display = ''; });
  1792. } else {
  1793. tabBar.classList.remove('tab-hidden');
  1794. var activeTab = tabBar.querySelector('.tab-btn.active');
  1795. if (activeTab) { activeTab.click(); }
  1796. else {
  1797. var firstTab = tabBar.querySelector('.tab-btn');
  1798. if (firstTab) firstTab.click();
  1799. }
  1800. }
  1801. }
  1802. function handleSearch(query) {
  1803. query = query.toLowerCase();
  1804. document.querySelectorAll('.news-item').forEach(function(item) {
  1805. var title = (item.querySelector('.news-title') || {}).textContent || '';
  1806. item.style.display = (!query || title.toLowerCase().indexOf(query) !== -1) ? '' : 'none';
  1807. });
  1808. document.querySelectorAll('.rss-item').forEach(function(item) {
  1809. var title = (item.querySelector('.rss-title') || {}).textContent || '';
  1810. item.style.display = (!query || title.toLowerCase().indexOf(query) !== -1) ? '' : 'none';
  1811. });
  1812. }
  1813. function initBackToTop() {
  1814. var fabBar = document.querySelector('.fab-bar');
  1815. if (!fabBar) return;
  1816. window.addEventListener('scroll', function() {
  1817. fabBar.classList.toggle('visible', window.scrollY > 300);
  1818. });
  1819. }
  1820. function initCollapse() {
  1821. document.querySelectorAll('.word-header').forEach(function(header) {
  1822. header.addEventListener('click', function() {
  1823. var tabBar = document.querySelector('.tab-bar');
  1824. if (document.body.classList.contains('wide-mode') && tabBar && !tabBar.classList.contains('tab-hidden')) return;
  1825. var group = header.closest('.word-group');
  1826. if (group) group.classList.toggle('collapsed');
  1827. });
  1828. });
  1829. initCollapseVisibility();
  1830. }
  1831. function initCollapseVisibility() {
  1832. var headers = document.querySelectorAll('.word-header');
  1833. var tabBar = document.querySelector('.tab-bar');
  1834. var isTabMode = document.body.classList.contains('wide-mode') && tabBar && !tabBar.classList.contains('tab-hidden');
  1835. headers.forEach(function(h) {
  1836. if (isTabMode) { h.classList.remove('collapsible'); }
  1837. else { h.classList.add('collapsible'); }
  1838. });
  1839. if (isTabMode) {
  1840. document.querySelectorAll('.word-group.collapsed').forEach(function(g) {
  1841. g.classList.remove('collapsed');
  1842. });
  1843. }
  1844. }
  1845. // 独立展示区 Tab 切换
  1846. function initStandaloneTabs() {
  1847. var tabBar = document.querySelector('.standalone-tab-bar');
  1848. if (!tabBar) return;
  1849. var groups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1850. var btns = tabBar.querySelectorAll('.tab-btn[data-standalone-tab]');
  1851. function activateStandaloneTab(val) {
  1852. btns.forEach(function(b) {
  1853. var bVal = b.getAttribute('data-standalone-tab');
  1854. b.classList.toggle('active', bVal === String(val));
  1855. });
  1856. groups.forEach(function(g) {
  1857. var gVal = g.getAttribute('data-standalone-tab');
  1858. g.style.display = (val === 'all' || gVal === String(val)) ? '' : 'none';
  1859. });
  1860. }
  1861. btns.forEach(function(btn) {
  1862. btn.addEventListener('click', function() {
  1863. activateStandaloneTab(btn.getAttribute('data-standalone-tab'));
  1864. });
  1865. });
  1866. // 初始状态
  1867. initStandaloneTabVisibility();
  1868. }
  1869. function initStandaloneTabVisibility() {
  1870. var tabBar = document.querySelector('.standalone-tab-bar');
  1871. if (!tabBar) return;
  1872. var groups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1873. var isWide = document.body.classList.contains('wide-mode');
  1874. if (!isWide || groups.length <= 1) {
  1875. tabBar.classList.add('tab-hidden');
  1876. groups.forEach(function(g) { g.style.display = ''; });
  1877. } else {
  1878. tabBar.classList.remove('tab-hidden');
  1879. var activeBtn = tabBar.querySelector('.tab-btn.active');
  1880. if (activeBtn) activeBtn.click();
  1881. else { var first = tabBar.querySelector('.tab-btn'); if (first) first.click(); }
  1882. }
  1883. }
  1884. function prepareForScreenshot() {
  1885. var state = {
  1886. wasWide: document.body.classList.contains('wide-mode'),
  1887. hiddenGroups: []
  1888. };
  1889. document.body.classList.remove('wide-mode');
  1890. state.wasDark = document.body.classList.contains('dark-mode');
  1891. document.body.classList.remove('dark-mode');
  1892. document.querySelectorAll('.word-group[data-tab-index]').forEach(function(g, i) {
  1893. if (g.style.display === 'none') {
  1894. state.hiddenGroups.push(i);
  1895. g.style.display = '';
  1896. }
  1897. });
  1898. state.hiddenStandaloneGroups = [];
  1899. document.querySelectorAll('.standalone-group[data-standalone-tab]').forEach(function(g, i) {
  1900. if (g.style.display === 'none') {
  1901. state.hiddenStandaloneGroups.push(i);
  1902. g.style.display = '';
  1903. }
  1904. });
  1905. document.querySelectorAll('.tab-bar, .standalone-tab-bar, .search-bar, .fab-bar, .toggle-wide-btn').forEach(function(el) {
  1906. el.dataset.prevDisplay = el.style.display || '';
  1907. el.style.display = 'none';
  1908. });
  1909. document.querySelectorAll('.toggle-dark-btn').forEach(function(el) {
  1910. el.dataset.prevDisplay = el.style.display || ''; el.style.display = 'none';
  1911. });
  1912. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = 'none'; });
  1913. document.querySelectorAll('.header-watermark').forEach(function(el) { el.style.display = 'none'; });
  1914. return state;
  1915. }
  1916. function restoreAfterScreenshot(state) {
  1917. if (state.wasWide) document.body.classList.add('wide-mode');
  1918. if (state.wasDark) document.body.classList.add('dark-mode');
  1919. var groups = document.querySelectorAll('.word-group[data-tab-index]');
  1920. state.hiddenGroups.forEach(function(i) {
  1921. if (groups[i]) groups[i].style.display = 'none';
  1922. });
  1923. var standaloneGroups = document.querySelectorAll('.standalone-group[data-standalone-tab]');
  1924. if (state.hiddenStandaloneGroups) {
  1925. state.hiddenStandaloneGroups.forEach(function(i) {
  1926. if (standaloneGroups[i]) standaloneGroups[i].style.display = 'none';
  1927. });
  1928. }
  1929. document.querySelectorAll('.tab-bar, .standalone-tab-bar, .search-bar, .fab-bar, .toggle-wide-btn').forEach(function(el) {
  1930. el.style.display = el.dataset.prevDisplay || '';
  1931. delete el.dataset.prevDisplay;
  1932. });
  1933. document.querySelectorAll('.toggle-dark-btn').forEach(function(el) {
  1934. el.style.display = el.dataset.prevDisplay || ''; delete el.dataset.prevDisplay;
  1935. });
  1936. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = ''; });
  1937. document.querySelectorAll('.reading-progress').forEach(function(el) { el.style.display = ''; });
  1938. document.querySelectorAll('.header-watermark').forEach(function(el) { el.style.display = ''; });
  1939. initTabVisibility();
  1940. initCollapseVisibility();
  1941. initStandaloneTabVisibility();
  1942. var fabBar = document.querySelector('.fab-bar');
  1943. if (fabBar && window.scrollY > 300) fabBar.classList.add('visible');
  1944. }
  1945. // ===== 截图功能 =====
  1946. async function saveAsImage() {
  1947. const button = event.target;
  1948. const originalText = button.textContent;
  1949. try {
  1950. button.textContent = '生成中...';
  1951. button.disabled = true;
  1952. window.scrollTo(0, 0);
  1953. // 等待页面稳定
  1954. await new Promise(resolve => setTimeout(resolve, 200));
  1955. // 截图前准备:切回窄屏布局
  1956. var screenshotState = prepareForScreenshot();
  1957. // 截图前隐藏按钮
  1958. const buttons = document.querySelector('.save-buttons');
  1959. buttons.style.visibility = 'hidden';
  1960. // 再次等待确保按钮完全隐藏
  1961. await new Promise(resolve => setTimeout(resolve, 100));
  1962. const container = document.querySelector('.container');
  1963. const canvas = await html2canvas(container, {
  1964. backgroundColor: '#ffffff',
  1965. scale: 1.5,
  1966. useCORS: true,
  1967. allowTaint: false,
  1968. imageTimeout: 10000,
  1969. removeContainer: false,
  1970. foreignObjectRendering: false,
  1971. logging: false,
  1972. width: container.offsetWidth,
  1973. height: container.offsetHeight,
  1974. x: 0,
  1975. y: 0,
  1976. scrollX: 0,
  1977. scrollY: 0,
  1978. windowWidth: window.innerWidth,
  1979. windowHeight: window.innerHeight
  1980. });
  1981. buttons.style.visibility = 'visible';
  1982. restoreAfterScreenshot(screenshotState);
  1983. const link = document.createElement('a');
  1984. const now = new Date();
  1985. 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`;
  1986. link.download = filename;
  1987. link.href = canvas.toDataURL('image/png', 1.0);
  1988. // 触发下载
  1989. document.body.appendChild(link);
  1990. link.click();
  1991. document.body.removeChild(link);
  1992. button.textContent = '保存成功!';
  1993. setTimeout(() => {
  1994. button.textContent = originalText;
  1995. button.disabled = false;
  1996. }, 2000);
  1997. } catch (error) {
  1998. const buttons = document.querySelector('.save-buttons');
  1999. buttons.style.visibility = 'visible';
  2000. restoreAfterScreenshot(screenshotState);
  2001. button.textContent = '保存失败';
  2002. setTimeout(() => {
  2003. button.textContent = originalText;
  2004. button.disabled = false;
  2005. }, 2000);
  2006. }
  2007. }
  2008. async function saveAsMultipleImages() {
  2009. const button = event.target;
  2010. const originalText = button.textContent;
  2011. const container = document.querySelector('.container');
  2012. const scale = 1.5;
  2013. const maxHeight = 5000 / scale;
  2014. var screenshotState2 = prepareForScreenshot();
  2015. try {
  2016. button.textContent = '分析中...';
  2017. button.disabled = true;
  2018. // 获取所有可能的分割元素
  2019. const newsItems = Array.from(container.querySelectorAll('.news-item'));
  2020. const wordGroups = Array.from(container.querySelectorAll('.word-group'));
  2021. const newSection = container.querySelector('.new-section');
  2022. const errorSection = container.querySelector('.error-section');
  2023. const header = container.querySelector('.header');
  2024. const footer = container.querySelector('.footer');
  2025. // 计算元素位置和高度
  2026. const containerRect = container.getBoundingClientRect();
  2027. const elements = [];
  2028. // 添加header作为必须包含的元素
  2029. elements.push({
  2030. type: 'header',
  2031. element: header,
  2032. top: 0,
  2033. bottom: header.offsetHeight,
  2034. height: header.offsetHeight
  2035. });
  2036. // 添加错误信息(如果存在)
  2037. if (errorSection) {
  2038. const rect = errorSection.getBoundingClientRect();
  2039. elements.push({
  2040. type: 'error',
  2041. element: errorSection,
  2042. top: rect.top - containerRect.top,
  2043. bottom: rect.bottom - containerRect.top,
  2044. height: rect.height
  2045. });
  2046. }
  2047. // 按word-group分组处理news-item
  2048. wordGroups.forEach(group => {
  2049. const groupRect = group.getBoundingClientRect();
  2050. const groupNewsItems = group.querySelectorAll('.news-item');
  2051. // 添加word-group的header部分
  2052. const wordHeader = group.querySelector('.word-header');
  2053. if (wordHeader) {
  2054. const headerRect = wordHeader.getBoundingClientRect();
  2055. elements.push({
  2056. type: 'word-header',
  2057. element: wordHeader,
  2058. parent: group,
  2059. top: groupRect.top - containerRect.top,
  2060. bottom: headerRect.bottom - containerRect.top,
  2061. height: headerRect.height
  2062. });
  2063. }
  2064. // 添加每个news-item
  2065. groupNewsItems.forEach(item => {
  2066. const rect = item.getBoundingClientRect();
  2067. elements.push({
  2068. type: 'news-item',
  2069. element: item,
  2070. parent: group,
  2071. top: rect.top - containerRect.top,
  2072. bottom: rect.bottom - containerRect.top,
  2073. height: rect.height
  2074. });
  2075. });
  2076. });
  2077. // 添加新增新闻部分
  2078. if (newSection) {
  2079. const rect = newSection.getBoundingClientRect();
  2080. elements.push({
  2081. type: 'new-section',
  2082. element: newSection,
  2083. top: rect.top - containerRect.top,
  2084. bottom: rect.bottom - containerRect.top,
  2085. height: rect.height
  2086. });
  2087. }
  2088. // 添加footer
  2089. const footerRect = footer.getBoundingClientRect();
  2090. elements.push({
  2091. type: 'footer',
  2092. element: footer,
  2093. top: footerRect.top - containerRect.top,
  2094. bottom: footerRect.bottom - containerRect.top,
  2095. height: footer.offsetHeight
  2096. });
  2097. // 计算分割点
  2098. const segments = [];
  2099. let currentSegment = { start: 0, end: 0, height: 0, includeHeader: true };
  2100. let headerHeight = header.offsetHeight;
  2101. currentSegment.height = headerHeight;
  2102. for (let i = 1; i < elements.length; i++) {
  2103. const element = elements[i];
  2104. const potentialHeight = element.bottom - currentSegment.start;
  2105. // 检查是否需要创建新分段
  2106. if (potentialHeight > maxHeight && currentSegment.height > headerHeight) {
  2107. // 在前一个元素结束处分割
  2108. currentSegment.end = elements[i - 1].bottom;
  2109. segments.push(currentSegment);
  2110. // 开始新分段
  2111. currentSegment = {
  2112. start: currentSegment.end,
  2113. end: 0,
  2114. height: element.bottom - currentSegment.end,
  2115. includeHeader: false
  2116. };
  2117. } else {
  2118. currentSegment.height = potentialHeight;
  2119. currentSegment.end = element.bottom;
  2120. }
  2121. }
  2122. // 添加最后一个分段
  2123. if (currentSegment.height > 0) {
  2124. currentSegment.end = container.offsetHeight;
  2125. segments.push(currentSegment);
  2126. }
  2127. button.textContent = `生成中 (0/${segments.length})...`;
  2128. // 隐藏保存按钮
  2129. const buttons = document.querySelector('.save-buttons');
  2130. buttons.style.visibility = 'hidden';
  2131. // 为每个分段生成图片
  2132. const images = [];
  2133. for (let i = 0; i < segments.length; i++) {
  2134. const segment = segments[i];
  2135. button.textContent = `生成中 (${i + 1}/${segments.length})...`;
  2136. // 创建临时容器用于截图
  2137. const tempContainer = document.createElement('div');
  2138. tempContainer.style.cssText = `
  2139. position: absolute;
  2140. left: -9999px;
  2141. top: 0;
  2142. width: ${container.offsetWidth}px;
  2143. background: white;
  2144. `;
  2145. tempContainer.className = 'container';
  2146. // 克隆容器内容
  2147. const clonedContainer = container.cloneNode(true);
  2148. // 移除克隆内容中的保存按钮
  2149. const clonedButtons = clonedContainer.querySelector('.save-buttons');
  2150. if (clonedButtons) {
  2151. clonedButtons.style.display = 'none';
  2152. }
  2153. tempContainer.appendChild(clonedContainer);
  2154. document.body.appendChild(tempContainer);
  2155. // 等待DOM更新
  2156. await new Promise(resolve => setTimeout(resolve, 100));
  2157. // 使用html2canvas截取特定区域
  2158. const canvas = await html2canvas(clonedContainer, {
  2159. backgroundColor: '#ffffff',
  2160. scale: scale,
  2161. useCORS: true,
  2162. allowTaint: false,
  2163. imageTimeout: 10000,
  2164. logging: false,
  2165. width: container.offsetWidth,
  2166. height: segment.end - segment.start,
  2167. x: 0,
  2168. y: segment.start,
  2169. windowWidth: window.innerWidth,
  2170. windowHeight: window.innerHeight
  2171. });
  2172. images.push(canvas.toDataURL('image/png', 1.0));
  2173. // 清理临时容器
  2174. document.body.removeChild(tempContainer);
  2175. }
  2176. // 恢复按钮显示
  2177. buttons.style.visibility = 'visible';
  2178. // 下载所有图片
  2179. const now = new Date();
  2180. 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')}`;
  2181. for (let i = 0; i < images.length; i++) {
  2182. const link = document.createElement('a');
  2183. link.download = `${baseFilename}_part${i + 1}.png`;
  2184. link.href = images[i];
  2185. document.body.appendChild(link);
  2186. link.click();
  2187. document.body.removeChild(link);
  2188. // 延迟一下避免浏览器阻止多个下载
  2189. await new Promise(resolve => setTimeout(resolve, 100));
  2190. }
  2191. button.textContent = `已保存 ${segments.length} 张图片!`;
  2192. restoreAfterScreenshot(screenshotState2);
  2193. setTimeout(() => {
  2194. button.textContent = originalText;
  2195. button.disabled = false;
  2196. }, 2000);
  2197. } catch (error) {
  2198. console.error('分段保存失败:', error);
  2199. const buttons = document.querySelector('.save-buttons');
  2200. buttons.style.visibility = 'visible';
  2201. restoreAfterScreenshot(screenshotState2);
  2202. button.textContent = '保存失败';
  2203. setTimeout(() => {
  2204. button.textContent = originalText;
  2205. button.disabled = false;
  2206. }, 2000);
  2207. }
  2208. }
  2209. document.addEventListener('DOMContentLoaded', function() {
  2210. window.scrollTo(0, 0);
  2211. // 自动检测宽屏模式
  2212. var savedMode = null;
  2213. try { savedMode = localStorage.getItem('trendradar-wide-mode'); } catch(e) {}
  2214. if (savedMode === '1' || (savedMode === null && window.innerWidth > 768)) {
  2215. document.body.classList.add('wide-mode');
  2216. var btn = document.querySelector('.toggle-wide-btn');
  2217. if (btn) btn.textContent = '⊡';
  2218. }
  2219. // 暗色模式恢复
  2220. var savedDark = null;
  2221. try { savedDark = localStorage.getItem('trendradar-dark-mode'); } catch(e) {}
  2222. if (savedDark === '1') {
  2223. document.body.classList.add('dark-mode');
  2224. var darkBtn = document.querySelector('.toggle-dark-btn');
  2225. if (darkBtn) darkBtn.textContent = '☀';
  2226. }
  2227. // 启用搜索栏
  2228. var searchBar = document.querySelector('.search-bar');
  2229. if (searchBar) searchBar.style.display = 'block';
  2230. // 初始化增强功能
  2231. initTabs();
  2232. initBackToTop();
  2233. initCollapse();
  2234. initStandaloneTabs();
  2235. // 键盘快捷键
  2236. document.addEventListener('keydown', function(e) {
  2237. if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
  2238. var helpBtn = document.querySelector('.fab-help');
  2239. switch(e.key) {
  2240. case '?':
  2241. if (helpBtn) {
  2242. helpBtn.classList.toggle('show-tip');
  2243. var fabBar = document.querySelector('.fab-bar');
  2244. if (fabBar) fabBar.classList.add('visible');
  2245. }
  2246. break;
  2247. case 'Escape':
  2248. if (helpBtn) helpBtn.classList.remove('show-tip');
  2249. break;
  2250. case 'w': case 'W': toggleWideMode(); break;
  2251. case 'd': case 'D': toggleDarkMode(); break;
  2252. case '/': e.preventDefault(); var si = document.querySelector('.search-input'); if (si) si.focus(); break;
  2253. }
  2254. });
  2255. // 阅读进度条
  2256. var progressBar = document.querySelector('.reading-progress');
  2257. if (progressBar) {
  2258. window.addEventListener('scroll', function() {
  2259. var h = document.documentElement.scrollHeight - window.innerHeight;
  2260. progressBar.style.width = (h > 0 ? (window.scrollY / h * 100) : 0) + '%';
  2261. });
  2262. }
  2263. // 一键复制:hover 时数字变复制图标
  2264. 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>';
  2265. 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>';
  2266. document.querySelectorAll('.news-item .news-number').forEach(function(numEl) {
  2267. var item = numEl.closest('.news-item');
  2268. var titleEl = item ? item.querySelector('.news-title a') : null;
  2269. if (!titleEl) return;
  2270. var numText = numEl.textContent.trim();
  2271. numEl.innerHTML = '<span class="num-text">' + numText + '</span><span class="copy-icon">' + copySvg + '</span>';
  2272. numEl.title = '点击复制标题和链接';
  2273. numEl.addEventListener('click', function(e) {
  2274. e.stopPropagation();
  2275. var text = titleEl.textContent.trim() + ' ' + titleEl.href;
  2276. navigator.clipboard.writeText(text).then(function() {
  2277. numEl.classList.add('copied');
  2278. numEl.querySelector('.copy-icon').innerHTML = checkSvg;
  2279. setTimeout(function() {
  2280. numEl.classList.remove('copied');
  2281. numEl.querySelector('.copy-icon').innerHTML = copySvg;
  2282. }, 1500);
  2283. });
  2284. });
  2285. });
  2286. // Header watermark 鼠标跟随揭示
  2287. (function() {
  2288. var header = document.querySelector('.header');
  2289. var watermark = document.querySelector('.header-watermark');
  2290. if (!header || !watermark) return;
  2291. var radius = 100;
  2292. header.addEventListener('mousemove', function(e) {
  2293. var rect = watermark.getBoundingClientRect();
  2294. var x = e.clientX - rect.left;
  2295. var y = e.clientY - rect.top;
  2296. 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%)';
  2297. watermark.style.webkitMaskImage = maskVal;
  2298. watermark.style.maskImage = maskVal;
  2299. watermark.style.color = 'rgba(255, 255, 255, 0.25)';
  2300. });
  2301. header.addEventListener('mouseleave', function() {
  2302. watermark.style.webkitMaskImage = 'radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%)';
  2303. watermark.style.maskImage = 'radial-gradient(circle 0px at 50% 50%, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 100%)';
  2304. watermark.style.color = 'rgba(255, 255, 255, 0.15)';
  2305. });
  2306. })();
  2307. });
  2308. </script>
  2309. </body>
  2310. </html>
  2311. """
  2312. return html