index.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>PageOS 应用程序包仓库</title>
  7. <link
  8. rel="icon"
  9. type="image/svg+xml"
  10. href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj4KICA8IS0tIOWkluahhuefqeW9oiAtLT4KICA8cmVjdCB4PSIyIiB5PSIyIiB3aWR0aD0iMjgiIGhlaWdodD0iMjgiIHJ4PSIxMCIgcnk9IjEwIgogICAgZmlsbD0iIzQyNDI0MiIKICAgIHN0cm9rZT0iIzAwNjk1YyIKICAgIHN0cm9rZS13aWR0aD0iNCIvPgoKICA8IS0tIOW3puecvO+8muWwj+WchueCuSAtLT4KICA8Y2lyY2xlIGN4PSIxMiIgY3k9IjE0IiByPSIxLjUiIGZpbGw9IiNmNWY1ZjUiIC8+CgogIDwhLS0g5Y+z55y877ya5bCP5ZyG54K5IC0tPgogIDxjaXJjbGUgY3g9IjIwIiBjeT0iMTQiIHI9IjEuNSIgZmlsbD0iI2Y1ZjVmNSIgLz4KCiAgPCEtLSDpvLvlrZDvvJrnq5bnur8gLS0+CiAgPGxpbmUgeDE9IjE2IiB5MT0iMTYiIHgyPSIxNiIgeTI9IjE4IiBzdHJva2U9IiNmNWY1ZjUiIHN0cm9rZS13aWR0aD0iMS41IiAvPgoKICA8IS0tIOW+rueskeeahOWYtO+8muS4iuaJrOeahOW8p+e6vyAtLT4KICA8cGF0aCBkPSJNIDE0IDIwIFEgMTYgMjIgMTggMjAiIAogICAgICAgIGZpbGw9Im5vbmUiIAogICAgICAgIHN0cm9rZT0iI2Y1ZjVmNSIgCiAgICAgICAgc3Ryb2tlLXdpZHRoPSIxLjUiIAogICAgICAgIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgLz4KPC9zdmc+"
  11. />
  12. <style>
  13. /* 全局样式 */
  14. * {
  15. margin: 0;
  16. padding: 0;
  17. box-sizing: border-box;
  18. text-decoration: none;
  19. color: var(--secondary-font-color);
  20. transition: all 0.2s ease;
  21. }
  22. ::selection {
  23. background: var(--accent-bg-color);
  24. color: var(--accent-font-color);
  25. }
  26. body {
  27. font-family: "Maple Mono CN", "Courier New", Courier, monospace;
  28. background: #424242;
  29. height: 100vh;
  30. display: flex;
  31. flex-direction: column;
  32. --accent-color: #64ffda; /* 强调色:边框 */
  33. --secondary-color: #00695c; /* 次要色:未激活边框 */
  34. --danger-color: #ff6d00; /* 着重色:边框 */
  35. --accent-bg-color: #009688;
  36. --secondary-bg-color: #616161;
  37. --content-bg-color: #424242;
  38. --accent-font-color: #f5f5f5;
  39. --secondary-font-color: #eeeeee;
  40. --margin-padding: 0.5rem;
  41. --border-thickness: 0.2rem;
  42. --border-radius: 0.5rem;
  43. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  44. --focused-bg-color: rgba(255, 255, 255, 0.1);
  45. }
  46. .container {
  47. padding: var(--margin-padding);
  48. flex: 1;
  49. overflow-y: auto;
  50. }
  51. /* 搜索栏样式 */
  52. .search-container {
  53. display: flex;
  54. gap: var(--margin-padding);
  55. background: var(--secondary-bg-color);
  56. padding: var(--margin-padding);
  57. border-bottom: var(--border-thickness) solid var(--secondary-color);
  58. }
  59. .search-box {
  60. flex: 1;
  61. padding: var(--margin-padding);
  62. border: var(--border-thickness) solid var(--secondary-color);
  63. border-radius: var(--margin-padding);
  64. background: var(--content-bg-color);
  65. font-size: 1rem;
  66. }
  67. .search-box:focus {
  68. --secondary-bg-color: var(--accent-bg-color);
  69. --secondary-font-color: var(--accent-font-color);
  70. --secondary-color: var(--accent-color);
  71. }
  72. .top-buttons {
  73. display: flex;
  74. border: var(--border-thickness) solid var(--secondary-color);
  75. padding: var(--margin-padding);
  76. border-radius: var(--margin-padding);
  77. background: var(--content-bg-color);
  78. position: relative;
  79. }
  80. /* 应用程序网格样式 */
  81. .apps-grid {
  82. display: grid;
  83. grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  84. gap: calc(2 * var(--margin-padding));
  85. padding: var(--margin-padding) 0;
  86. }
  87. .app-card {
  88. background: var(--secondary-bg-color);
  89. border: var(--border-thickness) solid var(--secondary-color);
  90. border-radius: var(--margin-padding);
  91. padding: var(--margin-padding);
  92. text-align: center;
  93. cursor: pointer;
  94. & > * {
  95. pointer-events: none;
  96. }
  97. }
  98. a:hover,
  99. button:hover {
  100. border-color: var(--accent-color);
  101. }
  102. .app-card:hover {
  103. border-color: var(--accent-color);
  104. transform: scale(1.1);
  105. box-shadow: var(--shadow);
  106. .app-name {
  107. white-space: initial;
  108. }
  109. }
  110. /* 图标样式 */
  111. .app-icon {
  112. width: 64px;
  113. height: 64px;
  114. margin: 0 auto 0.5rem;
  115. background: var(--content-bg-color);
  116. border-radius: 0.5rem;
  117. display: flex;
  118. align-items: center;
  119. justify-content: center;
  120. font-size: 2rem;
  121. font-weight: bold;
  122. }
  123. /* 应用程序信息样式 */
  124. .app-name {
  125. font-weight: bold;
  126. margin-bottom: 0.25rem;
  127. white-space: nowrap;
  128. overflow: hidden;
  129. text-overflow: ellipsis;
  130. }
  131. .app-author {
  132. font-size: 0.8rem;
  133. color: #b0b0b0;
  134. }
  135. /* 加载状态 */
  136. .loading {
  137. text-align: center;
  138. padding: 2rem;
  139. font-style: italic;
  140. }
  141. /* 翻译按钮相关 */
  142. .translateSelectLanguage {
  143. background: inherit;
  144. color: inherit;
  145. opacity: 0;
  146. cursor: pointer;
  147. position: absolute;
  148. top: 0;
  149. right: 0;
  150. bottom: 0;
  151. left: 0;
  152. }
  153. </style>
  154. <script
  155. src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
  156. type="text/javascript"
  157. ></script>
  158. </head>
  159. <body>
  160. <div class="search-container">
  161. <input
  162. type="text"
  163. class="search-box"
  164. placeholder="搜索应用程序……"
  165. id="searchInput"
  166. />
  167. <a id="translate" class="top-buttons language-selector" title="语言">
  168. <svg
  169. xmlns="http://www.w3.org/2000/svg"
  170. width="24"
  171. height="24"
  172. viewBox="0 0 24 24"
  173. fill="none"
  174. stroke="currentColor"
  175. stroke-width="2"
  176. stroke-linecap="round"
  177. stroke-linejoin="round"
  178. class="icon icon-tabler icons-tabler-outline icon-tabler-world"
  179. >
  180. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  181. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  182. <path d="M3.6 9h16.8" />
  183. <path d="M3.6 15h16.8" />
  184. <path d="M11.5 3a17 17 0 0 0 0 18" />
  185. <path d="M12.5 3a17 17 0 0 1 0 18" /></svg
  186. ></a>
  187. <a
  188. href="https://github.com/swaybien/pageos-apps"
  189. target="_blank"
  190. class="top-buttons"
  191. title="仓库"
  192. >
  193. <svg
  194. xmlns="http://www.w3.org/2000/svg"
  195. width="24"
  196. height="24"
  197. viewBox="0 0 24 24"
  198. fill="none"
  199. stroke="currentColor"
  200. stroke-width="2"
  201. stroke-linecap="round"
  202. stroke-linejoin="round"
  203. class="icon icon-tabler icons-tabler-outline icon-tabler-brand-git"
  204. >
  205. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  206. <path d="M16 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  207. <path d="M12 8m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  208. <path d="M12 16m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  209. <path d="M12 15v-6" />
  210. <path d="M15 11l-2 -2" />
  211. <path d="M11 7l-1.9 -1.9" />
  212. <path
  213. d="M13.446 2.6l7.955 7.954a2.045 2.045 0 0 1 0 2.892l-7.955 7.955a2.045 2.045 0 0 1 -2.892 0l-7.955 -7.955a2.045 2.045 0 0 1 0 -2.892l7.955 -7.955a2.045 2.045 0 0 1 2.892 0z"
  214. /></svg
  215. ></a>
  216. </div>
  217. <div class="container">
  218. <div class="apps-grid" id="appsGrid">
  219. <div class="loading">加载中……</div>
  220. </div>
  221. </div>
  222. <script src="lib/pinyinjs/pinyin_dict_withtone.js"></script>
  223. <script src="lib/pinyinjs/pinyinUtil.js"></script>
  224. <script>
  225. document.addEventListener("DOMContentLoaded", async () => {
  226. const appsGrid = document.getElementById("appsGrid");
  227. const searchInput = document.getElementById("searchInput");
  228. let packages = [];
  229. // 加载应用程序数据
  230. async function loadPackages() {
  231. try {
  232. const response = await fetch("index.json");
  233. const data = await response.json();
  234. packages = (data.packages || []).map((pkg) => ({
  235. ...pkg,
  236. pinyin: pinyinUtil.getPinyin(pkg.name, "", false), // 添加拼音字段
  237. }));
  238. renderApps(packages);
  239. } catch (error) {
  240. appsGrid.innerHTML = `<div class="loading">加载失败: ${error.message}</div>`;
  241. }
  242. }
  243. // 渲染应用程序列表
  244. function renderApps(appList) {
  245. if (appList.length === 0) {
  246. appsGrid.innerHTML =
  247. '<div class="loading">未找到匹配的应用程序</div>';
  248. translate.execute([appsGrid]);
  249. return;
  250. }
  251. appsGrid.innerHTML = appList
  252. .map((pkg) => {
  253. // 处理图标显示
  254. let iconContent = "";
  255. if (pkg.icon) {
  256. // 如果有图标 URL,使用 img 标签
  257. iconContent = `<img src="${pkg.location}/${pkg.icon}" alt="${
  258. pkg.name || pkg.id
  259. }" style="width:100%;height:100%;object-fit:contain;">`;
  260. } else {
  261. // 如果没有图标,显示名称首字符
  262. iconContent = pkg.name.charAt(0).toUpperCase();
  263. }
  264. return `
  265. <button data-href="${
  266. pkg.location
  267. }/metadata.json" onclick="window.open('${
  268. pkg.location
  269. }/metadata.json', '_blank');" class="app-card" title="${
  270. pkg.description || pkg.name || pkg.id
  271. }" target="_blank" data-menu-options='${JSON.stringify([
  272. { label: "启动应用程序", action: "execute" }, // 新标签页打开 href 指向的 metadata.json 中的 entry 指向的文件
  273. { label: "查看元信息 ", action: "metadata" }, // 新标签页打开 href 指向的 metadata.json
  274. { label: "复制启动地址", action: "cpexec" }, // 复制 href 指向的 metadata.json 中的 entry 指向的文件地址
  275. { label: "复制元文件地址", action: "cpmeta" }, // 复制 href 指向的 metadata.json 的地址
  276. ])}'>
  277. <div class="app-icon">${iconContent}</div>
  278. <div class="app-name">${pkg.name || pkg.id}</div>
  279. <div class="app-author">作者: ${pkg.author}</div>
  280. </button>`;
  281. })
  282. .join("");
  283. translate.execute([appsGrid]);
  284. }
  285. // 搜索功能
  286. searchInput.addEventListener("input", () => {
  287. const query = searchInput.value.toLowerCase().trim();
  288. if (!query) {
  289. renderApps(packages);
  290. return;
  291. }
  292. const filtered = packages.filter(
  293. (pkg) =>
  294. pkg.name.toLowerCase().includes(query) ||
  295. (pkg.pinyin && pkg.pinyin.includes(query)) || // 支持拼音搜索
  296. pkg.author.toLowerCase().includes(query) ||
  297. (pkg.description &&
  298. pkg.description.toLowerCase().includes(query)) ||
  299. pkg.id.toLowerCase().includes(query)
  300. );
  301. renderApps(filtered);
  302. });
  303. // 初始化
  304. await loadPackages();
  305. });
  306. // 自定义右键菜单功能
  307. function initOptionMenu(doc) {
  308. // 隐藏菜单后,将菜单移动到屏幕内
  309. function inScreen(contextMenu) {
  310. contextMenu.style.left = "2px";
  311. contextMenu.style.top = "2px";
  312. }
  313. // 初始化样式
  314. var style = doc.createElement("style");
  315. style.type = "text/css";
  316. style.innerHTML = `
  317. #custom-context-menu {
  318. transition: all 0.2s ease;
  319. --accent-color: #64ffda; /* 强调色:边框 */
  320. --secondary-color: #00695c; /* 次要色:未激活边框 */
  321. --danger-color: #ff6d00; /* 着重色:边框 */
  322. --accent-bg-color: #009688;
  323. --secondary-bg-color: #616161;
  324. --content-bg-color: #424242;
  325. --accent-font-color: #f5f5f5;
  326. --secondary-font-color: #eeeeee;
  327. --margin-padding: 0.5rem;
  328. --border-thickness: 0.2rem;
  329. --border-radius: 0.5rem;
  330. --title-bar-height: 2rem;
  331. --title-bar-justify-content: left;
  332. --task-bar-height: 3rem;
  333. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  334. --focused-bg-color: rgba(255, 255, 255, 0.1);
  335. font-size: small;
  336. user-select: none;
  337. position: absolute;
  338. background-color: var(--content-bg-color);
  339. border: var(--border-thickness) solid var(--secondary-color);
  340. border-radius: var(--border-radius);
  341. padding: 0;
  342. opacity: 0;
  343. transform: scale(0.8);
  344. pointer-events: none;
  345. z-index: 4000; /* 确保在顶层 */
  346. min-width: 150px;
  347. &.show {
  348. display: block;
  349. opacity: 1;
  350. transform: scale(1);
  351. pointer-events: auto;
  352. }
  353. &:hover {
  354. box-shadow: var(--shadow);
  355. --secondary-color: var(--accent-color);
  356. }
  357. }
  358. #custom-context-menu ul {
  359. list-style: none;
  360. margin: 0;
  361. padding: 0;
  362. }
  363. #custom-context-menu li {
  364. padding: var(--margin-padding);
  365. cursor: pointer;
  366. list-style-type: none; /* 去除列表项标记 */
  367. }
  368. #custom-context-menu li:hover {
  369. background-color: var(--focused-bg-color);
  370. }`;
  371. style.id = "addStyle";
  372. doc.getElementsByTagName("HEAD").item(0).appendChild(style);
  373. // 初始化元素
  374. const contextMenu = doc.createElement("ul");
  375. contextMenu.id = "custom-context-menu";
  376. doc.body.appendChild(contextMenu);
  377. // 监听全局右键点击事件
  378. doc.addEventListener("contextmenu", function (e) {
  379. // 阻止默认右键菜单
  380. e.preventDefault();
  381. // 获取被点击的元素
  382. const target = e.target;
  383. const selection = window.getSelection();
  384. const hasSelection =
  385. selection && selection.toString().trim().length > 0;
  386. // 检查事件目标是否在选区内
  387. let inSelection = false;
  388. if (hasSelection && selection.rangeCount > 0) {
  389. const range = selection.getRangeAt(0);
  390. inSelection = range.intersectsNode(e.target);
  391. }
  392. // 如果有文本被选中且右键点击在选区内,显示默认文本操作菜单
  393. if (hasSelection && inSelection) {
  394. const defaultTextOptions = [
  395. { label: "复制", action: "copy" },
  396. { label: "剪切", action: "cut" },
  397. { label: "粘贴", action: "paste" },
  398. { label: "全选", action: "selectAll" },
  399. ];
  400. // 清空菜单
  401. contextMenu.innerHTML = "";
  402. // 生成菜单项
  403. defaultTextOptions.forEach((option) => {
  404. const li = doc.createElement("li");
  405. li.textContent = option.label;
  406. li.onclick = function () {
  407. if (option.action === "copy") {
  408. doc.execCommand("copy");
  409. } else if (option.action === "cut") {
  410. doc.execCommand("cut");
  411. } else if (option.action === "paste") {
  412. doc.execCommand("paste");
  413. } else if (option.action === "selectAll") {
  414. doc.execCommand("selectAll");
  415. }
  416. contextMenu.classList.remove("show");
  417. inScreen(contextMenu);
  418. };
  419. contextMenu.appendChild(li);
  420. });
  421. // 显示菜单(确保在屏幕内)
  422. translate.execute([contextMenu]);
  423. contextMenu.classList.add("show");
  424. // 获取菜单尺寸
  425. const menuWidth = contextMenu.offsetWidth;
  426. const menuHeight = contextMenu.offsetHeight;
  427. // 计算调整后的位置
  428. let adjustedLeft = e.pageX;
  429. let adjustedTop = e.pageY;
  430. // 如果靠近右边缘,向左偏移
  431. if (window.innerWidth - e.pageX < menuWidth) {
  432. adjustedLeft = e.pageX - menuWidth;
  433. }
  434. // 如果靠近下边缘,向上偏移
  435. if (window.innerHeight - e.pageY < menuHeight) {
  436. adjustedTop = e.pageY - menuHeight;
  437. }
  438. contextMenu.style.left = adjustedLeft + "px";
  439. contextMenu.style.top = adjustedTop + "px";
  440. }
  441. // 否则检查元素的 data-menu-options 属性
  442. else if (target.dataset.menuOptions) {
  443. try {
  444. // 解析JSON字符串
  445. const menuOptions = JSON.parse(target.dataset.menuOptions);
  446. // 清空菜单
  447. contextMenu.innerHTML = "";
  448. // 动态生成菜单项
  449. menuOptions.forEach((option) => {
  450. const li = doc.createElement("li");
  451. li.textContent = option.label;
  452. li.onclick = function () {
  453. // 执行action
  454. if (option.action) {
  455. // 触发自定义事件
  456. const event = new CustomEvent("custom-contextmenu-action", {
  457. detail: {
  458. action: option.action,
  459. target: target,
  460. },
  461. });
  462. doc.dispatchEvent(event);
  463. }
  464. // 隐藏菜单
  465. contextMenu.classList.remove("show");
  466. inScreen(contextMenu);
  467. };
  468. contextMenu.appendChild(li);
  469. });
  470. // 显示菜单(确保在屏幕内)
  471. translate.execute([contextMenu]);
  472. contextMenu.classList.add("show");
  473. // 获取菜单尺寸
  474. const menuWidth = contextMenu.offsetWidth;
  475. const menuHeight = contextMenu.offsetHeight;
  476. // 计算调整后的位置
  477. let adjustedLeft = e.pageX;
  478. let adjustedTop = e.pageY;
  479. // 如果靠近右边缘,向左偏移
  480. if (window.innerWidth - e.pageX < menuWidth) {
  481. adjustedLeft = e.pageX - menuWidth;
  482. }
  483. // 如果靠近下边缘,向上偏移
  484. if (window.innerHeight - e.pageY < menuHeight) {
  485. adjustedTop = e.pageY - menuHeight;
  486. }
  487. contextMenu.style.left = adjustedLeft + "px";
  488. contextMenu.style.top = adjustedTop + "px";
  489. } catch (error) {
  490. console.error("解析菜单选项失败", error);
  491. }
  492. } else {
  493. // 如果没有定义菜单选项,则隐藏菜单
  494. contextMenu.classList.remove("show");
  495. inScreen(contextMenu);
  496. }
  497. });
  498. // 点击其他地方隐藏菜单
  499. doc.addEventListener("click", function () {
  500. contextMenu.classList.remove("show");
  501. inScreen(contextMenu);
  502. });
  503. // 阻止菜单内部的点击事件冒泡
  504. contextMenu.addEventListener("click", function (e) {
  505. e.stopPropagation();
  506. });
  507. }
  508. // 处理右键菜单动作
  509. document.addEventListener("custom-contextmenu-action", function (e) {
  510. const { action, target } = e.detail;
  511. const metadataUrl = target.dataset.href;
  512. // 获取 metadata.json 内容
  513. async function getMetadata() {
  514. try {
  515. const response = await fetch(metadataUrl);
  516. return await response.json();
  517. } catch (error) {
  518. console.error("获取 metadata.json 失败:", error);
  519. return null;
  520. }
  521. }
  522. if (action === "execute") {
  523. // 启动应用程序:新标签页打开 metadata.json 中的 entry 指向的文件
  524. getMetadata().then((metadata) => {
  525. if (metadata && metadata.entry) {
  526. const entryUrl = new URL(
  527. metadata.entry,
  528. new URL(metadataUrl, window.location.href).href
  529. ).href;
  530. window.open(entryUrl, "_blank");
  531. }
  532. });
  533. } else if (action === "metadata") {
  534. // 查看元信息:新标签页打开 metadata.json
  535. window.open(metadataUrl, "_blank");
  536. } else if (action === "cpexec") {
  537. // 复制启动地址:复制 metadata.json 中的 entry 指向的文件地址
  538. getMetadata().then((metadata) => {
  539. if (metadata && metadata.entry) {
  540. const entryUrl = new URL(
  541. metadata.entry,
  542. new URL(metadataUrl, window.location.href).href
  543. ).href;
  544. navigator.clipboard.writeText(entryUrl).catch((err) => {
  545. console.error("复制失败:", err);
  546. });
  547. }
  548. });
  549. } else if (action === "cpmeta") {
  550. // 复制元文件地址:复制 metadata.json 的地址
  551. navigator.clipboard.writeText(metadataUrl).catch((err) => {
  552. console.error("复制失败:", err);
  553. });
  554. }
  555. });
  556. initOptionMenu(document);
  557. // 加载 CSS
  558. function loadCSS(url) {
  559. return new Promise((resolve, reject) => {
  560. //// 检查是否已加载
  561. const existingLinks = document.querySelectorAll(
  562. `link[href="${url}"]`
  563. );
  564. if (existingLinks.length > 0) {
  565. return resolve(url);
  566. }
  567. //// 创建新的 link 元素
  568. const link = document.createElement("link");
  569. link.rel = "stylesheet";
  570. link.type = "text/css";
  571. link.href = url;
  572. //// 成功加载处理
  573. link.onload = () => {
  574. link.onload = null;
  575. link.onerror = null;
  576. resolve(url);
  577. };
  578. //// 加载失败处理
  579. link.onerror = () => {
  580. link.onerror = null;
  581. link.onload = null;
  582. document.head.removeChild(link);
  583. reject(new Error(`无法加载 CSS 文件: ${url}`));
  584. };
  585. //// 添加到文档头部
  586. document.head.appendChild(link);
  587. });
  588. }
  589. // translate-js 翻译系统
  590. //// 初始化翻译系统
  591. function initTranslate() {
  592. try {
  593. translate.service.use("client.edge");
  594. translate.request.listener.start();
  595. translate.setAutoDiscriminateLocalLanguage();
  596. translate.language.setUrlParamControl();
  597. translate.ignore.class.push("notTranslate");
  598. translate.execute();
  599. } catch (e) {
  600. console.error("翻译系统出错:" + e);
  601. }
  602. }
  603. //// 刷新翻译
  604. function refreshTranslate() {
  605. try {
  606. translate.selectLanguageTag.refreshRender();
  607. } catch (e) {
  608. console.error("刷新翻译出错:" + e);
  609. }
  610. }
  611. // 初始化
  612. function initialize() {
  613. initTranslate(); //// 初始化页面翻译
  614. loadCSS(
  615. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  616. ); //// 加载字体 CSS
  617. }
  618. // 触发器
  619. //// 网页加载完毕后触发初始化
  620. window.addEventListener("DOMContentLoaded", () => initialize());
  621. </script>
  622. </body>
  623. </html>