Browse Source

【新增】重构并增强 PageOS 环境检测扩展

- 重新组织代码结构,使用自执行函数封装扩展逻辑
- 添加新的事件块,用于检测到特定环境时触发
- 增加环境值获取块,支持获取不同环境的值
- 优化环境检测逻辑,提高准确性
- 更新块的文本和功能,使其更加直观易用
PJ568 1 năm trước cách đây
mục cha
commit
95339f9c3b
1 tập tin đã thay đổi với 114 bổ sung và 91 xóa
  1. 114 91
      src/pageos-env-extension.js

+ 114 - 91
src/pageos-env-extension.js

@@ -10,105 +10,128 @@
 
 
 // 本扩展需要非沙盒环境运行:读取 window.location
 // 本扩展需要非沙盒环境运行:读取 window.location
 // This extension requires disabling sandboxing: access window.location
 // This extension requires disabling sandboxing: access window.location
-class PageOSEnvironmentExtension {
-  constructor(runtime) {
-    this.runtime = runtime;
-    this.currentEnv = "未知环境"; // '未知环境', '网页环境', '登录环境', '用户环境'
-  }
+(function (Scratch) {
+  ("use strict");
+  const vm = Scratch.vm;
+  const runtime = vm.runtime;
+  const Cast = Scratch.Cast;
 
 
-  getInfo() {
-    return {
-      id: "pageosEnvDetector",
-      name: "PageOS 环境检测",
-      blocks: [
-        {
-          opcode: "detectEnvironment",
-          blockType: Scratch.BlockType.COMMAND,
-          text: "检测当前 PageOS 环境",
-        },
-        {
-          opcode: "onDetectionComplete",
-          blockType: Scratch.BlockType.EVENT,
-          text: "当检测当前 PageOS 环境执行完毕",
-        },
-        {
-          opcode: "getCurrentEnv",
-          blockType: Scratch.BlockType.REPORTER,
-          text: "当前 PageOS 环境",
-        },
-        {
-          opcode: "unknownEnv",
-          blockType: Scratch.BlockType.REPORTER,
-          text: "未知环境",
-          disableMonitor: true,
-        },
-        {
-          opcode: "webEnv",
-          blockType: Scratch.BlockType.REPORTER,
-          text: "网页环境",
-          disableMonitor: true,
-        },
-        {
-          opcode: "loginEnv",
-          blockType: Scratch.BlockType.REPORTER,
-          text: "登录环境",
-          disableMonitor: true,
-        },
-        {
-          opcode: "userEnv",
-          blockType: Scratch.BlockType.REPORTER,
-          text: "用户环境",
-          disableMonitor: true,
+  class PageOSEnvironmentExtension {
+    constructor() {
+      this.currentEnv = "未知环境"; // '未知环境', '网页环境', '登录环境', '用户环境'
+    }
+
+    getInfo() {
+      return {
+        id: "pageosEnvDetector",
+        name: "PageOS 环境检测",
+        blocks: [
+          {
+            opcode: "detectEnvironment",
+            blockType: Scratch.BlockType.COMMAND,
+            text: "检测当前 PageOS 环境",
+          },
+          {
+            blockType: Scratch.BlockType.EVENT,
+            opcode: "onEnvDetected",
+            text: "当检测到 PageOS 环境为[ENV]",
+            isEdgeActivated: false,
+            arguments: {
+              ENV: {
+                type: Scratch.ArgumentType.STRING,
+                menu: "environmentMenu",
+                defaultValue: "网页环境",
+              },
+            },
+          },
+          {
+            opcode: "getCurrentEnv",
+            blockType: Scratch.BlockType.REPORTER,
+            text: "当前 PageOS 环境",
+          },
+          {
+            opcode: "getState",
+            blockType: Scratch.BlockType.REPORTER,
+            text: "PageOS 环境值:[PROP]",
+            arguments: {
+              PROP: {
+                type: Scratch.ArgumentType.STRING,
+                menu: "environmentMenu",
+                defaultValue: "网页环境",
+              },
+            },
+          },
+        ],
+        menus: {
+          environmentMenu: {
+            acceptReporters: false,
+            items: [
+              {
+                text: "未知环境",
+                value: "未知环境",
+              },
+              {
+                text: "网页环境",
+                value: "网页环境",
+              },
+              {
+                text: "登录环境",
+                value: "登录环境",
+              },
+              {
+                text: "用户环境",
+                value: "用户环境",
+              },
+            ],
+          },
         },
         },
-      ],
-      // 声明为非沙盒扩展以访问window对象
-      unsandboxed: true,
-    };
-  }
+        unsandboxed: true,
+      };
+    }
 
 
-  detectEnvironment() {
-    const host = window.location.host;
+    detectEnvironment() {
+      const host = window.location.host;
 
 
-    if (host === "127.0.0.1:12800" || host === "localhost:12800") {
-      this.currentEnv = "用户环境";
-    } else if (host === "127.0.0.1:12801" || host === "localhost:12801") {
-      this.currentEnv = "登录环境";
-    } else {
-      const port = window.location.port;
-      if (!port || port === "80" || port === "443") {
-        this.currentEnv = "网页环境";
+      if (host === "127.0.0.1:12800" || host === "localhost:12800") {
+        this.currentEnv = "用户环境";
+      } else if (host === "127.0.0.1:12801" || host === "localhost:12801") {
+        this.currentEnv = "登录环境";
       } else {
       } else {
-        this.currentEnv = "未知环境";
+        const port = window.location.port;
+        if (!port || port === "80" || port === "443") {
+          this.currentEnv = "网页环境";
+        } else {
+          this.currentEnv = "未知环境";
+        }
       }
       }
-    }
-
-    // 触发完成事件
-    this.runtime.startHats("pageosEnvDetector_onDetectionComplete");
-  }
-
-  onDetectionComplete() {
-    return true;
-  }
-
-  getCurrentEnv() {
-    return this.currentEnv;
-  }
-
-  unknownEnv() {
-    return "未知环境";
-  }
 
 
-  webEnv() {
-    return "网页环境";
-  }
+      // 触发环境检测事件
+      // 使用完整 opcode: extensionid_opcode
+      runtime.startHats("pageosEnvDetector_onEnvDetected", {
+        ENV: this.currentEnv,
+      });
+    }
 
 
-  loginEnv() {
-    return "登录环境";
-  }
+    getCurrentEnv() {
+      return this.currentEnv;
+    }
 
 
-  userEnv() {
-    return "用户环境";
+    getState(args) {
+      const prop = Cast.toString(args.PROP);
+      switch (prop) {
+        case "未知环境":
+          return "未知环境";
+        case "网页环境":
+          return "网页环境";
+        case "登录环境":
+          return "登录环境";
+        case "用户环境":
+          return "用户环境";
+        default:
+          return "未知环境";
+      }
+    }
   }
   }
-}
 
 
-Scratch.extensions.register(new PageOSEnvironmentExtension());
+  Scratch.extensions.register(new PageOSEnvironmentExtension());
+})(Scratch);