index.html 26 KB

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