pageos-env-extension.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This Source Code Form is subject to the terms of the Mozilla Public
  2. // License, v. 2.0. If a copy of the MPL was not distributed with this
  3. // file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. /*
  5. * @fileoverview PageOS 环境检测扩展
  6. * @author PJ568
  7. * @license MPL-2.0
  8. */
  9. // 本扩展需要非沙盒环境运行:读取 window.location
  10. // This extension requires disabling sandboxing: access window.location
  11. (function (Scratch) {
  12. ("use strict");
  13. const vm = Scratch.vm;
  14. const runtime = vm.runtime;
  15. const Cast = Scratch.Cast;
  16. class PageOSEnvironmentExtension {
  17. constructor() {
  18. this.currentEnv = "未知环境"; // '未知环境', '网页环境', '登录环境', '用户环境'
  19. }
  20. getInfo() {
  21. return {
  22. id: "pageosEnvDetector",
  23. name: "PageOS 环境检测",
  24. blocks: [
  25. {
  26. opcode: "detectEnvironment",
  27. blockType: Scratch.BlockType.COMMAND,
  28. text: "检测当前 PageOS 环境",
  29. },
  30. {
  31. blockType: Scratch.BlockType.EVENT,
  32. opcode: "onEnvDetected",
  33. text: "当检测到 PageOS 环境为[ENV]",
  34. isEdgeActivated: false,
  35. arguments: {
  36. ENV: {
  37. type: Scratch.ArgumentType.STRING,
  38. menu: "environmentMenu",
  39. defaultValue: "网页环境",
  40. },
  41. },
  42. },
  43. {
  44. opcode: "getCurrentEnv",
  45. blockType: Scratch.BlockType.REPORTER,
  46. text: "当前 PageOS 环境",
  47. },
  48. {
  49. opcode: "getState",
  50. blockType: Scratch.BlockType.REPORTER,
  51. text: "PageOS 环境值:[PROP]",
  52. arguments: {
  53. PROP: {
  54. type: Scratch.ArgumentType.STRING,
  55. menu: "environmentMenu",
  56. defaultValue: "网页环境",
  57. },
  58. },
  59. },
  60. ],
  61. menus: {
  62. environmentMenu: {
  63. acceptReporters: false,
  64. items: [
  65. {
  66. text: "未知环境",
  67. value: "未知环境",
  68. },
  69. {
  70. text: "网页环境",
  71. value: "网页环境",
  72. },
  73. {
  74. text: "登录环境",
  75. value: "登录环境",
  76. },
  77. {
  78. text: "用户环境",
  79. value: "用户环境",
  80. },
  81. ],
  82. },
  83. },
  84. unsandboxed: true,
  85. };
  86. }
  87. detectEnvironment() {
  88. const host = window.location.host;
  89. if (host === "127.0.0.1:12800" || host === "localhost:12800") {
  90. this.currentEnv = "用户环境";
  91. } else if (host === "127.0.0.1:12801" || host === "localhost:12801") {
  92. this.currentEnv = "登录环境";
  93. } else {
  94. const port = window.location.port;
  95. if (!port || port === "80" || port === "443") {
  96. this.currentEnv = "网页环境";
  97. } else {
  98. this.currentEnv = "未知环境";
  99. }
  100. }
  101. // 触发环境检测事件
  102. // 使用完整 opcode: extensionid_opcode
  103. runtime.startHats("pageosEnvDetector_onEnvDetected", {
  104. ENV: this.currentEnv,
  105. });
  106. }
  107. getCurrentEnv() {
  108. return this.currentEnv;
  109. }
  110. getState(args) {
  111. const prop = Cast.toString(args.PROP);
  112. switch (prop) {
  113. case "未知环境":
  114. return "未知环境";
  115. case "网页环境":
  116. return "网页环境";
  117. case "登录环境":
  118. return "登录环境";
  119. case "用户环境":
  120. return "用户环境";
  121. default:
  122. return "未知环境";
  123. }
  124. }
  125. }
  126. Scratch.extensions.register(new PageOSEnvironmentExtension());
  127. })(Scratch);