Sfoglia il codice sorgente

【新增】添加自定义 HTML 扩展并更新相关文档

- 新增 pageos-custom-html-extension.js 文件,实现自定义 HTML 元素显示功能
- 更新 README.md,添加新扩展的说明和示例
- 修改 pageos-env-extension.js,增加对 HTTPS 端口的支持
- 更新 .gitignore,排除不必要的文档文件夹
PJ568 1 anno fa
parent
commit
6a18e6286c

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/doc/turbowarp-extension
+
+.vscode

+ 4 - 2
README.md

@@ -2,7 +2,9 @@
 
 本仓库用于存放 [PageOS](https://github.com/swaybien/pageos) 项目相关的扩展:
 
-- [pageos-greet-extension.js](src/pageos-greet-extension.js):
-  用于和 [pageos-greet](https://github.com/swaybien/pageos-greet) 登录管理器 API 交互。
+- [pageos-custom-html-extension.js](src/pageos-custom-html-extension.js):
+  在舞台上显示自定义 HTML 元素内容。
 - [pageos-env-extension.js](src/pageos-env-extension.js):
   用于判断当前 [PageOS](https://github.com/swaybien/pageos) 环境处于什么状态。
+- [pageos-greet-extension.js](src/pageos-greet-extension.js):
+  用于和 [pageos-greet](https://github.com/swaybien/pageos-greet) 登录管理器 API 交互。

+ 261 - 0
src/pageos-custom-html-extension.js

@@ -0,0 +1,261 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+/*
+ * @fileoverview PageOS Custom HTML
+ * @author PJ568
+ * @license MPL-2.0
+ */
+
+// Description: Display custom HTML elements without sandbox restrictions.
+// Context: Unsandboxed extension for direct DOM manipulation.
+(function (Scratch) {
+  "use strict";
+
+  /** @type {HTMLDivElement|null} */
+  let customElement = null;
+  let overlay = null;
+
+  // 状态变量
+  let x = 0;
+  let y = 0;
+  let width = -1;
+  let height = -1;
+  let visible = true;
+  let htmlContent = "";
+
+  // 更新元素位置和尺寸
+  const updateElementAttributes = () => {
+    if (!customElement) return;
+
+    const { stageWidth, stageHeight } = Scratch.vm.runtime;
+    const effectiveWidth = width >= 0 ? width : stageWidth;
+    const effectiveHeight = height >= 0 ? height : stageHeight;
+
+    customElement.style.width = `${effectiveWidth}px`;
+    customElement.style.height = `${effectiveHeight}px`;
+    customElement.style.transform = `translate(${-effectiveWidth / 2 + x}px, ${
+      -effectiveHeight / 2 - y
+    }px)`;
+    customElement.style.display = visible ? "" : "none";
+  };
+
+  // 创建自定义元素
+  const createElement = (html) => {
+    closeElement();
+    htmlContent = html;
+
+    customElement = document.createElement("div");
+    customElement.style.position = "absolute";
+    customElement.style.overflow = "hidden";
+    customElement.style.pointerEvents = "auto";
+    customElement.style.top = "0";
+    customElement.style.left = "0";
+    customElement.innerHTML = html;
+
+    overlay = Scratch.renderer.addOverlay(customElement, "scale-centered");
+    updateElementAttributes();
+  };
+
+  // 关闭元素
+  const closeElement = () => {
+    if (customElement) {
+      Scratch.renderer.removeOverlay(customElement);
+      customElement = null;
+      overlay = null;
+    }
+  };
+
+  // 监听舞台变化
+  Scratch.vm.on("STAGE_SIZE_CHANGED", updateElementAttributes);
+  Scratch.vm.runtime.on("RUNTIME_DISPOSED", closeElement);
+
+  class CustomHTMLExtension {
+    getInfo() {
+      return {
+        name: "PageOS Custom HTML",
+        id: "pageosCustomHtml",
+        blocks: [
+          {
+            opcode: "display",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "show custom HTML [HTML]",
+            arguments: {
+              HTML: {
+                type: Scratch.ArgumentType.STRING,
+                defaultValue:
+                  '<div style="background:red;width:100%;height:100%">Hello!</div>',
+              },
+            },
+          },
+          "---",
+          {
+            opcode: "update",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "update HTML to [HTML]",
+            arguments: {
+              HTML: {
+                type: Scratch.ArgumentType.STRING,
+                defaultValue:
+                  '<div style="background:blue;width:100%;height:100%">Updated!</div>',
+              },
+            },
+          },
+          "---",
+          {
+            opcode: "show",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "show element",
+          },
+          {
+            opcode: "hide",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "hide element",
+          },
+          {
+            opcode: "close",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "close element",
+          },
+          "---",
+          {
+            opcode: "setX",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "set element x position to [X]",
+            arguments: {
+              X: {
+                type: Scratch.ArgumentType.NUMBER,
+                defaultValue: "0",
+              },
+            },
+          },
+          {
+            opcode: "setY",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "set element y position to [Y]",
+            arguments: {
+              Y: {
+                type: Scratch.ArgumentType.NUMBER,
+                defaultValue: "0",
+              },
+            },
+          },
+          {
+            opcode: "setWidth",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "set element width to [WIDTH]",
+            arguments: {
+              WIDTH: {
+                type: Scratch.ArgumentType.NUMBER,
+                defaultValue: "100",
+              },
+            },
+          },
+          {
+            opcode: "setHeight",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "set element height to [HEIGHT]",
+            arguments: {
+              HEIGHT: {
+                type: Scratch.ArgumentType.NUMBER,
+                defaultValue: "100",
+              },
+            },
+          },
+          {
+            opcode: "getProperty",
+            blockType: Scratch.BlockType.REPORTER,
+            text: "element [PROPERTY]",
+            arguments: {
+              PROPERTY: {
+                type: Scratch.ArgumentType.STRING,
+                menu: "propertyMenu",
+              },
+            },
+          },
+        ],
+        menus: {
+          propertyMenu: {
+            acceptReporters: true,
+            items: [
+              { text: "x position", value: "x" },
+              { text: "y position", value: "y" },
+              { text: "width", value: "width" },
+              { text: "height", value: "height" },
+              { text: "visibility", value: "visible" },
+              { text: "HTML content", value: "html" },
+            ],
+          },
+        },
+      };
+    }
+
+    display({ HTML }) {
+      createElement(Scratch.Cast.toString(HTML));
+    }
+
+    update({ HTML }) {
+      if (customElement) {
+        htmlContent = Scratch.Cast.toString(HTML);
+        customElement.innerHTML = htmlContent;
+      }
+    }
+
+    show() {
+      visible = true;
+      updateElementAttributes();
+    }
+
+    hide() {
+      visible = false;
+      updateElementAttributes();
+    }
+
+    close() {
+      closeElement();
+    }
+
+    setX({ X }) {
+      x = Scratch.Cast.toNumber(X);
+      updateElementAttributes();
+    }
+
+    setY({ Y }) {
+      y = Scratch.Cast.toNumber(Y);
+      updateElementAttributes();
+    }
+
+    setWidth({ WIDTH }) {
+      width = Scratch.Cast.toNumber(WIDTH);
+      updateElementAttributes();
+    }
+
+    setHeight({ HEIGHT }) {
+      height = Scratch.Cast.toNumber(HEIGHT);
+      updateElementAttributes();
+    }
+
+    getProperty(args) {
+      const property = Scratch.Cast.toString(args.PROPERTY);
+      switch (property) {
+        case "x":
+          return x;
+        case "y":
+          return y;
+        case "width":
+          return width;
+        case "height":
+          return height;
+        case "visible":
+          return visible;
+        case "html":
+          return htmlContent;
+        default:
+          return 0;
+      }
+    }
+  }
+
+  Scratch.extensions.register(new CustomHTMLExtension());
+})(Scratch);

+ 8 - 2
src/pageos-env-extension.js

@@ -2,12 +2,18 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
 
+/*
+ * @fileoverview PageOS 环境检测扩展
+ * @author PJ568
+ * @license MPL-2.0
+ */
+
 // 本扩展需要非沙盒环境运行:读取 window.location
 // This extension requires disabling sandboxing: access window.location
 class PageOSEnvironmentExtension {
   constructor(runtime) {
     this.runtime = runtime;
-    this.currentEnv = "未知环境";
+    this.currentEnv = "未知环境"; // '未知环境', '网页环境', '登录环境', '用户环境'
   }
 
   getInfo() {
@@ -69,7 +75,7 @@ class PageOSEnvironmentExtension {
       this.currentEnv = "登录环境";
     } else {
       const port = window.location.port;
-      if (!port || port === "80") {
+      if (!port || port === "80" || port === "443") {
         this.currentEnv = "网页环境";
       } else {
         this.currentEnv = "未知环境";

+ 6 - 0
src/pageos-greet-extension.js

@@ -2,6 +2,12 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
 
+/*
+ * @fileoverview PageOS 登录扩展
+ * @author PJ568
+ * @license MPL-2.0
+ */
+
 // 本扩展需要非沙盒环境运行:websocket
 // This extension requires disabling sandboxing: websocket
 class PageOSLoginExtension {