index.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>天气查询</title>
  7. <style>
  8. /* 全局样式 - 从风格参考继承 */
  9. * {
  10. color: var(--secondary-font-color);
  11. font-family: "Courier New", Courier, monospace;
  12. box-sizing: border-box;
  13. transition: all 0.2s ease;
  14. }
  15. ::selection {
  16. background: var(--accent-bg-color);
  17. color: var(--accent-font-color);
  18. }
  19. :root,
  20. body {
  21. height: 100%;
  22. margin: 0;
  23. background: var(--content-bg-color);
  24. padding: var(--margin-padding);
  25. --accent-color: #64ffda; /* 强调色:边框 */
  26. --secondary-color: #00695c; /* 次要色:未激活边框 */
  27. --accent-bg-color: #009688;
  28. --secondary-bg-color: #616161;
  29. --content-bg-color: #424242;
  30. --accent-font-color: #f5f5f5;
  31. --secondary-font-color: #eeeeee;
  32. --margin-padding: 0.5rem;
  33. --border-thickness: 0.2rem;
  34. --border-radius: 0.5rem;
  35. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  36. }
  37. body {
  38. max-width: 800px;
  39. margin: 0 auto;
  40. padding: 20px;
  41. }
  42. .container {
  43. display: flex;
  44. flex-direction: column;
  45. gap: 20px;
  46. }
  47. .search-box {
  48. display: flex;
  49. gap: 10px;
  50. }
  51. input {
  52. padding: 8px;
  53. flex-grow: 1;
  54. background: var(--secondary-bg-color);
  55. border: var(--border-thickness) solid var(--secondary-color);
  56. border-radius: var(--border-radius);
  57. color: var(--secondary-font-color);
  58. font-size: 1rem;
  59. outline: none;
  60. }
  61. input:focus {
  62. border-color: var(--accent-color);
  63. }
  64. button {
  65. padding: 8px 16px;
  66. background: var(--accent-bg-color);
  67. color: var(--accent-font-color);
  68. border: none;
  69. border-radius: var(--border-radius);
  70. cursor: pointer;
  71. font-weight: bold;
  72. }
  73. button:hover {
  74. opacity: 0.9;
  75. transform: translateY(-2px);
  76. box-shadow: var(--shadow);
  77. }
  78. .weather-info {
  79. background: var(--secondary-bg-color);
  80. border-radius: var(--border-radius);
  81. padding: 20px;
  82. box-shadow: var(--shadow);
  83. }
  84. .weather-detail {
  85. margin-bottom: 10px;
  86. padding-bottom: 8px;
  87. border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  88. }
  89. .weather-detail:last-child {
  90. border-bottom: none;
  91. margin-bottom: 0;
  92. }
  93. h1,
  94. h2 {
  95. color: var(--accent-color);
  96. border-bottom: var(--border-thickness) solid var(--accent-color);
  97. padding-bottom: 0.3rem;
  98. }
  99. /* 翻译按钮相关 */
  100. #translate {
  101. position: relative;
  102. }
  103. .translateSelectLanguage {
  104. background: inherit;
  105. color: inherit;
  106. opacity: 0;
  107. cursor: pointer;
  108. position: absolute;
  109. top: 0;
  110. right: 0;
  111. bottom: 0;
  112. left: 0;
  113. }
  114. </style>
  115. <script
  116. src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
  117. type="text/javascript"
  118. ></script>
  119. <link rel="icon" type="image/svg+xml" href="favicon.svg" />
  120. </head>
  121. <body>
  122. <div class="container">
  123. <h1>天气查询</h1>
  124. <div class="search-box">
  125. <input type="text" id="cityInput" placeholder="输入城市名称" />
  126. <button onclick="getWeather()">获取天气</button>
  127. <button
  128. onclick="window.open('https://github.com/swaybien/pageos-weather', '_blank')"
  129. >仓库</button
  130. >
  131. <button id="translate">语言</button>
  132. </div>
  133. <div id="weatherResult" class="weather-info">
  134. <p>正在获取当前位置天气……</p>
  135. </div>
  136. </div>
  137. <script>
  138. // 页面加载时获取当前位置天气
  139. document.addEventListener("DOMContentLoaded", function () {
  140. fetch("https://wttr.in/?format=j1")
  141. .then((response) => response.json())
  142. .then((data) => {
  143. updateWeatherUI(data);
  144. })
  145. .catch((error) => {
  146. const resultDiv = document.getElementById("weatherResult");
  147. resultDiv.innerHTML = `
  148. <p>无法获取当前位置天气</p>
  149. <p>请输入城市名称手动查询</p>
  150. `;
  151. });
  152. });
  153. function updateWeatherUI(data) {
  154. const current = data.current_condition[0];
  155. const area = data.nearest_area[0];
  156. let html = `
  157. <h2>${area.areaName[0].value}天气</h2>
  158. <div class="weather-detail">
  159. <strong>当前天气:</strong> ${current.weatherDesc[0].value}
  160. </div>
  161. <div class="weather-detail">
  162. <strong>温度:</strong> ${current.temp_C}°C (体感 ${current.FeelsLikeC}°C)
  163. </div>
  164. <div class="weather-detail">
  165. <strong>湿度:</strong> ${current.humidity}%
  166. </div>
  167. <div class="weather-detail">
  168. <strong>风速:</strong> ${current.windspeedKmph} km/h (${current.winddir16Point})
  169. </div>
  170. <div class="weather-detail">
  171. <strong>降水量:</strong> ${current.precipMM} mm
  172. </div>
  173. <div class="weather-detail">
  174. <strong>能见度:</strong> ${current.visibility} km
  175. </div>
  176. <div class="weather-detail">
  177. <strong>气压:</strong> ${current.pressure} hPa
  178. </div>
  179. <div class="weather-detail">
  180. <strong>更新时间:</strong> ${current.observation_time}
  181. </div>
  182. `;
  183. const res = document.getElementById("weatherResult");
  184. res.innerHTML = html;
  185. translate.execute([res]);
  186. refreshTranslate();
  187. }
  188. function getWeather() {
  189. const city = document.getElementById("cityInput").value.trim();
  190. if (!city) {
  191. alert("请输入城市名称");
  192. return;
  193. }
  194. const resultDiv = document.getElementById("weatherResult");
  195. resultDiv.innerHTML = "<p>加载中……</p>";
  196. fetch(`https://wttr.in/${city}?format=j1`)
  197. .then((response) => response.json())
  198. .then((data) => {
  199. updateWeatherUI(data);
  200. })
  201. .catch((error) => {
  202. resultDiv.innerHTML = `<p>获取天气信息失败: ${error.message}</p>`;
  203. });
  204. }
  205. // 加载 CSS
  206. function loadCSS(url) {
  207. return new Promise((resolve, reject) => {
  208. //// 检查是否已加载
  209. const existingLinks = document.querySelectorAll(
  210. `link[href="${url}"]`
  211. );
  212. if (existingLinks.length > 0) {
  213. return resolve(url);
  214. }
  215. //// 创建新的 link 元素
  216. const link = document.createElement("link");
  217. link.rel = "stylesheet";
  218. link.type = "text/css";
  219. link.href = url;
  220. //// 成功加载处理
  221. link.onload = () => {
  222. link.onload = null;
  223. link.onerror = null;
  224. resolve(url);
  225. };
  226. //// 加载失败处理
  227. link.onerror = () => {
  228. link.onerror = null;
  229. link.onload = null;
  230. document.head.removeChild(link);
  231. reject(new Error(`无法加载 CSS 文件: ${url}`));
  232. };
  233. //// 添加到文档头部
  234. document.head.appendChild(link);
  235. });
  236. }
  237. // translate-js 翻译系统
  238. //// 初始化翻译系统
  239. function initTranslate() {
  240. try {
  241. translate.service.use("client.edge");
  242. translate.request.listener.start();
  243. translate.setAutoDiscriminateLocalLanguage();
  244. translate.language.setUrlParamControl();
  245. translate.language.translateLocal = true;
  246. translate.ignore.class.push("notTranslate");
  247. translate.execute();
  248. } catch (e) {
  249. console.error("翻译系统出错:" + e);
  250. }
  251. }
  252. //// 刷新翻译
  253. function refreshTranslate() {
  254. try {
  255. translate.selectLanguageTag.refreshRender();
  256. } catch (e) {
  257. console.error("刷新翻译出错:" + e);
  258. }
  259. }
  260. // 初始化
  261. function initialize() {
  262. initTranslate(); //// 初始化页面翻译
  263. loadCSS(
  264. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  265. ); //// 加载字体 CSS
  266. }
  267. // 触发器
  268. //// 网页加载完毕后触发初始化
  269. window.addEventListener("DOMContentLoaded", () => initialize());
  270. </script>
  271. </body>
  272. </html>