2
0

index.html 26 KB

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