chore: 项目基线(基于 WeKnora 定制版,整理文件忽略规则)
This commit is contained in:
commit
9097ee699d
5769 files changed
+1446466
No files matched your search
@@ -0,0 +1,117 @@
|
||||
// This is a standalone script: it runs inside the opaque-origin iframe, never
|
||||
// in the application window. Keep it independent of imports and bundler helpers.
|
||||
// Each frame owns two ephemeral stores, discarded when the frame is reloaded.
|
||||
const storageBootstrap = `<script>
|
||||
(() => {
|
||||
function createStorage() {
|
||||
const entries = new Map();
|
||||
const api = Object.create(null);
|
||||
Object.defineProperties(api, {
|
||||
length: { configurable: true, get: () => entries.size },
|
||||
key: { configurable: true, value: (index) => Array.from(entries.keys())[Number(index) >>> 0] ?? null },
|
||||
getItem: { configurable: true, value: (key) => entries.get(String(key)) ?? null },
|
||||
setItem: { configurable: true, value: (key, value) => { entries.set(String(key), String(value)); } },
|
||||
removeItem: { configurable: true, value: (key) => { entries.delete(String(key)); } },
|
||||
clear: { configurable: true, value: () => { entries.clear(); } },
|
||||
[Symbol.toStringTag]: { configurable: true, value: 'Storage' },
|
||||
});
|
||||
return new Proxy(api, {
|
||||
get(target, key) {
|
||||
return Reflect.has(target, key) ? Reflect.get(target, key) : entries.get(key);
|
||||
},
|
||||
set(target, key, value) {
|
||||
if (typeof key === 'string') entries.set(key, String(value));
|
||||
return true;
|
||||
},
|
||||
deleteProperty(target, key) {
|
||||
entries.delete(key);
|
||||
return true;
|
||||
},
|
||||
has(target, key) {
|
||||
return Reflect.has(target, key) || entries.has(key);
|
||||
},
|
||||
ownKeys() {
|
||||
return Array.from(entries.keys());
|
||||
},
|
||||
getOwnPropertyDescriptor(target, key) {
|
||||
return Reflect.getOwnPropertyDescriptor(target, key) || (entries.has(key)
|
||||
? { configurable: true, enumerable: true, writable: true, value: entries.get(key) }
|
||||
: undefined);
|
||||
},
|
||||
});
|
||||
}
|
||||
for (const name of ['localStorage', 'sessionStorage']) {
|
||||
try {
|
||||
// Leave working browser storage alone. Sandboxed opaque origins throw
|
||||
// here before a generated page can even register its event handlers.
|
||||
void window[name].length;
|
||||
} catch {
|
||||
const storage = createStorage();
|
||||
Object.defineProperty(window, name, { configurable: true, enumerable: true, get: () => storage });
|
||||
}
|
||||
}
|
||||
})();
|
||||
</script>`
|
||||
|
||||
// wkui bridge: lets a generated UI (artifact HTML) send one-line prompts into
|
||||
// the chat and make read-only data requests, without ever receiving
|
||||
// credentials. The parent side (document-preview.vue) only accepts messages
|
||||
// whose `source` is this exact iframe, so a hostile page gains nothing by
|
||||
// including the bridge — it can only talk about its own session's artifacts.
|
||||
const bridgeBootstrap = `<script>
|
||||
(() => {
|
||||
const CHANNEL = 'wkui';
|
||||
const MAX_TEXT = 4000;
|
||||
let seq = 0;
|
||||
const pending = new Map();
|
||||
function post(message) {
|
||||
try { window.parent.postMessage(Object.assign({ channel: CHANNEL }, message), '*'); } catch {}
|
||||
}
|
||||
function onMessage(event) {
|
||||
const data = event && event.data;
|
||||
if (!data || data.channel !== CHANNEL) return;
|
||||
if (data.type === 'wkui:data' && data.requestId != null) {
|
||||
const resolve = pending.get(data.requestId);
|
||||
if (resolve) { pending.delete(data.requestId); resolve(data); }
|
||||
}
|
||||
}
|
||||
// Degrade silently in restricted hosts: the bridge is an enhancement, the
|
||||
// generated page must stay fully usable without it.
|
||||
try { window.addEventListener('message', onMessage); } catch {}
|
||||
window.wkui = {
|
||||
version: 1,
|
||||
sendChat(text) {
|
||||
const value = typeof text === 'string' ? text : String(text == null ? '' : text);
|
||||
post({ type: 'wkui:send-chat', text: value.slice(0, MAX_TEXT) });
|
||||
},
|
||||
requestData(request) {
|
||||
const requestId = ++seq;
|
||||
return new Promise((resolve) => {
|
||||
pending.set(requestId, resolve);
|
||||
post({ type: 'wkui:request-data', requestId, request: request || {} });
|
||||
setTimeout(() => {
|
||||
if (pending.has(requestId)) {
|
||||
pending.delete(requestId);
|
||||
resolve({ ok: false, error: 'timeout' });
|
||||
}
|
||||
}, 15000);
|
||||
});
|
||||
},
|
||||
};
|
||||
post({ type: 'wkui:ready' });
|
||||
})();
|
||||
</script>`
|
||||
|
||||
/** Add compatibility only to the rendered copy, preserving the original source. */
|
||||
export function buildHtmlPreview(html: string): string {
|
||||
// Keep the doctype first (standards mode), and use an existing head when
|
||||
// present. Only inspect the document prefix: text resembling tags inside
|
||||
// comments, scripts or the body must never become an insertion point.
|
||||
const trivia = String.raw`(?:\s|<!--[\s\S]*?-->)*`
|
||||
const attributes = String.raw`(?:[^<>"']|"[^"]*"|'[^']*')*`
|
||||
const prefix = new RegExp(
|
||||
`^${trivia}(?:<!doctype\\b${attributes}>${trivia})?(?:<html\\b${attributes}>${trivia})?(?:<head\\b${attributes}>)?`,
|
||||
'i',
|
||||
).exec(html)?.[0] ?? ''
|
||||
return prefix + storageBootstrap + bridgeBootstrap + html.slice(prefix.length)
|
||||
}
|
||||
Reference in new issue
Block a user