feat: 预览里的学生代码可以用 localStorage
Deploy / deploy (build, debian, 22) (push) Canceled after 0s

预览 iframe 没有 allow-same-origin,访问 localStorage 会抛异常。现在在学生代码之前
注入一个替身(支持常用方法、属性读写、Object.keys 等):

- 自己的编辑器(教程、挑战、自由创作):写入通过 postMessage 交给主页面,按“用户 + 任务”
  存到本站 localStorage 的 preview-storage:* 下,下次渲染注入回去,刷新后数据还在
- 看别人的作品:只存在内存里
- 单个任务最多 10 万字符,总共 50 万字符,超出时先删最久没用的,不挤占登录 token 的配额
- 下载的 index.html 不带替身

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-26 21:55:31 +08:00
1 parent dc91b9b5db
commit 40f3063b91
8 files changed
+230 -6

No files matched your search

@@ -241,6 +241,7 @@ import { computed, ref, watch } from "vue"
import { NPopconfirm, NButton, useMessage } from "naive-ui"
import { Icon } from "@iconify/vue"
import { renderMarkdown } from "../../utils/markdown"
import { storageShim } from "../../utils/previewStorage"
import { Prompt, Submission } from "../../api"
import type { PromptRound } from "../../utils/type"
import { user, roleSuper } from "../../store/user"
@@ -298,7 +299,7 @@ const selectedPageHtml = computed(() => {
if (!round?.html) return null
const style = round.css ? `<style>${round.css}</style>` : ""
const script = round.js ? `<script>${round.js}<\/script>` : ""
return `<!DOCTYPE html><html><head><meta charset="utf-8">${style}</head><body>${round.html}${script}</body></html>`
return `<!DOCTYPE html><html><head><meta charset="utf-8">${storageShim()}${style}</head><body>${round.html}${script}</body></html>`
})
function isPromptLong(text: string): boolean {